Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Yet another problem with classes and Icompare
       
Author Reply
Prime b
posted 420 posts
since Dec 12, 2011 
from

Yet another problem with classes and Icompare

  Posted on: 04 Feb 2012       
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);
  }
  }
Practice makes it perfect && All science is either computer programming or stamp collecting
Maha
posted  816 posts
since  Aug 13, 2006 
from 

 Re: Yet another problem with classes and Icompare
  Posted on: 04 Feb 2012        0  
http://www.c-sharpcorner.com/Forums/Thread/156331/month-in-C-Sharp.aspx

Please see the above link.
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Yet another problem with classes and Icompare
  Posted on: 05 Feb 2012   Accepted Answer     0  
Apart from a little tidying up of the input code, you program looks pretty good so far.

Of course, you still need to do the following:

1. Change the phone number so it's a string - unless it's a very simple phone system you have there :)

2. Change the date of birth code so that there are 3 integers to store instead of one.

3. Sort the array into alphabetical order.

4. Once the array is sorted, you could use the Array.BinarySearch method to find a friend instead of looking through them individually. For large arrays, this method is much more efficient but it hardly matters for 8 folks.

5. A message needs to be displayed if the friend is not found. You might need to consider here whether to make the search case-insensitive or not.

This isn't a suitable example for using List<T> instead of an array because you're told in advance how many friends there are going to be. However, I've changed the code to demonstrate how you'd do it if you needed to.

The ToString() method overrides the virtual method of that name in System.Object from which all .NET types ultimately inherit. If you don't override it then, by default, ToString() returns the type name.

using System;
using System.Collections.Generic;

class FriendBirthday
{
   static void Main()
   {
      List<Friend> friends = new List<Friend>();
      string friendName;
      int friendNumber;
      int friendDateOfBirth;

      Console.WriteLine("Please enter information about your 3 friends:\n\n ");

      for (int index = 0; index < 3; index++)
      {
         Console.WriteLine("Details for friend {0}: ", 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.Add(new Friend(friendName, friendNumber, friendDateOfBirth));

         Console.WriteLine(); // space out the input a bit
      }

      Console.WriteLine("Enter friends name you looking for: ");
      string userInput = Console.ReadLine();

      for(int index = 0 ; index < friends.Count; index++)
      {
         if(userInput == friends[index].FriendName)
         {
            Console.WriteLine(friends[index].DateOfBirth);
            break; // no need to go on
         }
      }

      Console.ReadKey();
   }
}

class Friend
{
   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);
   }
}

       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved