a. Create a program named FriendList that declares an array of eight Friend obje
ID: 665798 • Letter: A
Question
a. Create a program named FriendList that declares an array of eight Friend objects and prompts the user to enter data about the friends. Display the Friend objects in alphabetical order by first name. The Friend class includes auto-implemented properties for the Friend’s name, phone number, and three integers that together represent the Friend’s birthday—month, day, and year.
b. Create a FriendBirthday program that modifies the FriendList program created in Exercise 9a so that after the list of Friend objects is displayed, the program prompts the user for a specific Friend’s name and the program returns the Friend’s phone number and birthday. Display an appropriate message if the friend requested by the user is not found.
c. Create a program named AllFriendsInSameMonth that modifies the program in Exercise 9b so that after the requested Friend’s birthday is displayed, the program also displays a list of every Friend who has a birthday in the same month. (Farrell 433)
C# console application
Explanation / Answer
a. Below is solution. It has two files one is Friend.cs and another Priendrogram.cs
---------------------------------------------------------------------------------
Friend.cs
----------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FriendList
{
public class Friend
{
public string Name { get; set; }
public string PhoneNumber { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public int Year { get; set; }
}
}
---------------------------------------------------------------------------------------
Program.cs
--------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FriendList
{
class Program
{
static void Main(string[] args)
{
Friend[] friends = new Friend[8];
Console.WriteLine("Please enter details for 8 friends one by one...");
for (int index = 0; index < 8; index ++ )
{
Friend friend = new Friend();
Console.WriteLine("Please enter details for Friend " + (index + 1));
Console.WriteLine("Please enter Name");
friend.Name = Console.ReadLine();
Console.WriteLine("Please enter Phone number");
friend.PhoneNumber = Console.ReadLine();
Console.WriteLine("Please enter Month of birth");
friend.Month = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter Day of birth");
friend.Day = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter Year of birth");
friend.Year = Convert.ToInt32(Console.ReadLine());
friends[index] = friend;
}
Console.WriteLine("Press [Enter] to display all the friends...");
var orderedFriends = friends.OrderBy((fr) => fr.Name);
int counter = 1;
foreach (var friend in orderedFriends)
{
Console.WriteLine("{0} :: Name : [{1}], Phone Number [{2}], DOB : [{3}]",
counter, friend.Name, friend.PhoneNumber, friend.Month + "//" + friend.Day + "//" + friend.Year);
counter++;
}
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
}
}
}
b)
Modified Program.cs as per the question is as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FriendBirthday
{
class Program
{
static void Main(string[] args)
{
Friend[] friends = new Friend[8];
Console.WriteLine("Please enter details for 8 friends one by one...");
for (int index = 0; index < 8; index++)
{
Friend friend = new Friend();
Console.WriteLine("Please enter details for Friend " + (index + 1));
Console.WriteLine("Please enter Name");
friend.Name = Console.ReadLine();
Console.WriteLine("Please enter Phone number");
friend.PhoneNumber = Console.ReadLine();
Console.WriteLine("Please enter Month of birth");
friend.Month = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter Day of birth");
friend.Day = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter Year of birth");
friend.Year = Convert.ToInt32(Console.ReadLine());
friends[index] = friend;
}
Console.WriteLine("Press [Enter] to display all the friends...");
var orderedFriends = friends.OrderBy((fr) => fr.Name);
int counter = 1;
foreach (var friend in orderedFriends)
{
Console.WriteLine("{0} :: Name : [{1}], Phone Number [{2}], DOB : [{3}]",
counter, friend.Name, friend.PhoneNumber, friend.Month + "//" + friend.Day + "//" + friend.Year);
counter++;
}
Console.WriteLine("Please enter the desired friend name to get his/her phone number and birthday...");
string name = Console.ReadLine();
var desiredFriend= friends.Where((fr) => fr.Name == name).Select((fr) => fr).FirstOrDefault();
if (desiredFriend != null)
{
Console.WriteLine("Phone Number : [{0}], Birthday : [{1}]", desiredFriend.PhoneNumber,
desiredFriend.Month + "//" + desiredFriend.Day + "//" + desiredFriend.Year);
}
else
{
Console.WriteLine("Desired Friend : [{0}] not found", name);
}
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
}
}
}
c)
The modified Program.cs is as below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AllFriendsInSameMonth
{
class Program
{
static void Main(string[] args)
{
Friend[] friends = new Friend[8];
Console.WriteLine("Please enter details for 8 friends one by one...");
for (int index = 0; index < 8; index++)
{
Friend friend = new Friend();
Console.WriteLine("Please enter details for Friend " + (index + 1));
Console.WriteLine("Please enter Name");
friend.Name = Console.ReadLine();
Console.WriteLine("Please enter Phone number");
friend.PhoneNumber = Console.ReadLine();
Console.WriteLine("Please enter Month of birth");
friend.Month = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter Day of birth");
friend.Day = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter Year of birth");
friend.Year = Convert.ToInt32(Console.ReadLine());
friends[index] = friend;
}
Console.WriteLine("Press [Enter] to display all the friends...");
var orderedFriends = friends.OrderBy((fr) => fr.Name);
int counter = 1;
foreach (var friend in orderedFriends)
{
Console.WriteLine("{0} :: Name : [{1}], Phone Number [{2}], DOB : [{3}]",
counter, friend.Name, friend.PhoneNumber, friend.Month + "//" + friend.Day + "//" + friend.Year);
counter++;
}
Console.WriteLine("Please enter the desired friend name to get his/her phone number and birthday...");
string name = Console.ReadLine();
var desiredFriend = friends.Where((fr) => fr.Name == name).Select((fr) => fr).FirstOrDefault();
if (desiredFriend != null)
{
Console.WriteLine("Phone Number : [{0}], Birthday : [{1}]", desiredFriend.PhoneNumber,
desiredFriend.Month + "//" + desiredFriend.Day + "//" + desiredFriend.Year);
Console.WriteLine("List of friends having birthday in the same month...");
var listOfFriends = friends.Where((fr) => fr.Month == desiredFriend.Month).Select((fr) => fr);
foreach (var friend in listOfFriends)
{
Console.WriteLine("{0} :: Name : [{1}], Phone Number [{2}], DOB : [{3}]",
counter, friend.Name, friend.PhoneNumber, friend.Month + "//" + friend.Day + "//" + friend.Year);
counter++;
}
}
else
{
Console.WriteLine("Desired Friend : [{0}] not found", name);
}
Console.WriteLine("Press [Enter] to exit");
Console.ReadLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.