a.Create a program named FriendList that declares an array of eight Friend objec
ID: 3777966 • 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.
Explanation / Answer
Friend[] friends = new Friend[3];
string friendName;
int friendNumber;
int friendDateOfBirth;
Console.WriteLine("Please enter information about your 8 friends: ");
for (int index = 0; index < friends.Length; index++)
{
Console.WriteLine("Details for each friend: ", index + 1);
Console.WriteLine("Enter friends name: ");
friendName = Console.ReadLine();
Console.WriteLine("Enter his phone number: ");
friendNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter his date of birth in the format of mm/dd/yy: ");
friendDateOfBirth = Convert.ToInt32(Console.ReadLine());
friends[index] = new Friend(friendName, friendNumber, friendDateOfBirth);
}
Console.WriteLine("Enter friends name you looking for: ");
string userInput = Console.ReadLine();
for(int index = 0 ; index < friends.Length; index++)
{
if(userInput == friends[index].FriendName)
{
Console.WriteLine(friends[index].DateOfBirth);
}
}
Console.ReadKey();
public string FriendName { get; set; }
public int PhoneNumber { get; set; }
public int DateOfBirth { get; set; }
public Friend(string FriendName, int PhoneNumber, int DateOfBirth)
{
this.FriendName = FriendName;
this.PhoneNumber = PhoneNumber;
this.DateOfBirth = DateOfBirth;
}
public override string ToString()
{
return string.Format("Friends name: {0}, phone number: {1}, date of birth: {2}.", FriendName, PhoneNumber, DateOfBirth);
}
public int CompareTo(Friend otherFriend)
{
return this.FriendName.CompareTo(otherFriend.FriendName);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.