TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Prime b
NA
810
345.9k
Yet another problem with classes and Icompare
Feb 4 2012 7:34 PM
Create a class named Friend. Its auto-implemented properties
include those for the Friend's name, phone number,
and three integers that together represent the Friend's
birthday—month, day, and year. Write a program that
declares an array of eight Friend objects and prompts the
user to enter data about eight friends. Display the Friend
objects in alphabetical order by fi rst name. Save the
program as FriendList.cs.
b. Modify the program created in Exercise 9a so that after
the list of Friend objects is displayed, the program
prompts the user for a specifi c Friend's name and the
program displays the Friend's birthday. Display an
appropriate message if the friend the user requests is not
found. Save the program as FriendBirthday.cs.
Questions: If I wanted to switch from array to list (thingy) how would I do that? Also if my program looks bad tell me what looks not good, or what I could have made better. I know, I declared 3 students instead of 8, I just didn't feel like filling in extra 15 rows of info
Also highlighted in red color, what does that method overrides?
My solution:
Friend[] friends = new Friend[3];
string friendName;
int friendNumber;
int friendDateOfBirth;
Console.WriteLine("Please enter information about your 8 friends:\n\n ");
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);
}
}
Reply
Answers (
2
)
Classes and Icompare
For and Foreach loop in C#