سورس دفترچه تلفن به زبان سی شارپ (Console Application)

#C

توسط reza-irdev در 6 سال پیش
0 47.5k 2 5 سال پیش
reza-irdev ir-sakhtafzar
0

سلام سورس دفترچه تلفن به زبان سی شارپ رو براتون گذاشتم

محتویات کلاس ها: PhoneBookApp.cs

[CSHARP]// PhoneBookApp.cs // // Written by Liong Ng

using System;

namespace PhoneBook { class PhoneBookApp { static void Main(string[] args) { PhoneBookAccess phoneBookAccess = new PhoneBookAccess(); for(;;) {
//--- Display menu --- Console.WriteLine( "" ); Console.WriteLine( "Enter 1 to enter a new phone book entry" ); Console.WriteLine( "Enter 2 to view the number for a name" ); Console.WriteLine( "Enter 3 to list all the entries" ); Console.WriteLine( "Enter 4 to delete an entry" ); Console.WriteLine( "Enter 5 to quit." );

        //--- Get input ---
        byte byInput;
        try
        {
           byInput = Convert.ToByte( Console.ReadLine() );
        }
        catch
        {
           Console.WriteLine( "Invalid selection, try again." );
           continue;
        }

        switch( byInput )
        {
           case 1:  // New entry
              Person person = Person.ReadPerson();
              PhoneNumber number = PhoneNumber.ReadNumber();
              
              BookEntry bookEntry = new BookEntry( person, number );
              phoneBookAccess.AddEntry( bookEntry );
              break;
           case 2:  // View entry
              Person someone = Person.ReadPerson();
              BookEntry entry = phoneBookAccess.GetEntry( someone );
              if( entry == null )
                 Console.WriteLine( someone + " was not found. ");
              else           
                 Console.WriteLine( "The number for "+ someone +
                                    " is " + phoneBookAccess.GetEntry(someone).GetNumber());
              break;
           case 3:  // list entries
              phoneBookAccess.List();
              break;
           case 4:  // Delete entry
              person = Person.ReadPerson();
              phoneBookAccess.DeleteEntry( person );
              break;
           case 5:  // quit
              phoneBookAccess.Save();
              Console.WriteLine("Ending program.");
              return;
           default:
              Console.WriteLine( "Invalid selection, try again." );
              break;
        }
     }                  	
	}
}

} [/CSHARP]

BookEntry.cs

[CSHARP]// BookEntry.cs // // Written by Liong Ng

using System; using System.Runtime.Serialization;

[Serializable] class BookEntry { public BookEntry( Person person, PhoneNumber number ) { this.person = person; this.number = number; }

public Person GetPerson() { return person; }

public PhoneNumber GetNumber() { return number; }

public override string ToString() { return person.ToString() + "\t" + number.ToString(); }

private Person person; private PhoneNumber number; }[/CSHARP]

Person.cs

[CSHARP]// Person.cs // // Written by Liong Ng

using System; using System.Runtime.Serialization;

[Serializable] class Person { public Person( string firstName, string lastName ) { this.firstName = firstName; this.lastName = lastName; }

public string FirstName { get { return firstName; } }

public string LastName { get { return lastName; } }

public override string ToString() { return firstName + " " + lastName; }

public override Boolean Equals( Object obj ) { if ( obj == null ) return false;

  if ( this.GetType() != obj.GetType() ) return false;

  Person person = (Person) obj;

  if ( !Object.Equals( firstName, person.FirstName ) ) return false;
  if ( !Object.Equals( lastName, person.LastName ) ) return false;

  return true;

}

public override int GetHashCode() { return firstName.GetHashCode() ^ lastName.GetHashCode(); }

//--- Read a person from the keyboard --- public static Person ReadPerson() { Console.Write( "Enter last name: " ); string readLastName = Console.ReadLine(); Console.Write( "Enter first name: " ); string readFirstName = Console.ReadLine();

  return new Person( readLastName, readFirstName );

}

private string firstName; private string lastName;

}[/CSHARP]

PhoneBookAccess.cs

[CSHARP]// PhoneBookAccess.cs // // Written by Liong Ng

using System; using System.IO; using System.Collections; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;

[Serializable] class PhoneBookAccess { public PhoneBookAccess() { Stream s = null; try { s = File.Open("Phone.bin", FileMode.Open, FileAccess.Read); BinaryFormatter b = new BinaryFormatter(); table = (Hashtable) b.Deserialize(s); } catch( FileNotFoundException ) { table = new Hashtable(); } finally { if ( s != null ) s.Close(); } }

public void AddEntry(BookEntry entry) { table.Add( entry.GetPerson(), entry ); }

public BookEntry GetEntry(Person key) { return (BookEntry) table[key]; }

public void DeleteEntry(Person key) { table.Remove( key ); }

public PhoneNumber GetNumber(Person key) { return GetEntry(key).GetNumber(); }

public void List() { foreach( DictionaryEntry d in table ) Console.WriteLine( "{0}", d.Value ); }

public void Save() { Stream s = null; try { s = File.Open("Phone.bin", FileMode.Create, FileAccess.ReadWrite); BinaryFormatter b = new BinaryFormatter(); b.Serialize(s, table); } finally { if ( s != null ) s.Close(); } }

private Hashtable table; }[/CSHARP]

PhoneNumber.cs

[CSHARP]// PhoneNumber.cs // // Written by Liong Ng

using System; using System.Runtime.Serialization;

[Serializable] class PhoneNumber { public PhoneNumber( string areaCode, string number ) { this.areaCode = areaCode; this.number = number; }

public string AreaCode { get { return areaCode; } set { areaCode = value; } }

public string Number { get { return number; } set { number = value; } }

public override string ToString() { return areaCode + " " + number; }

// Read a person from the keyboard public static PhoneNumber ReadNumber() { Console.Write( "Enter area code: " ); string readAreaCode = Console.ReadLine(); Console.Write( "Enter number: " ); string readNumber = Console.ReadLine();

  return new PhoneNumber( readAreaCode, readNumber );

}

string areaCode; string number; }[/CSHARP]

[color=#008000]آپدیت شد[/color] [color=#FF0000]دانلود پروژه با لینک مستقیم[/color] موفق باشید

0

با سلام خدمت شما استاد گرامی لینک دانلود پروژه کار نمیکنه لطفا پیگیری کنید

0

> با سلام خدمت شما استاد گرامی لینک دانلود پروژه کار نمیکنه لطفا پیگیری کنید
سلام عزیز سورس کد به صورت کامل گذاشته شده لینک هم اصلاح شد میتونید دانلود کنید.

خوش آمدید

برای طرح سوال، ایجاد بحث و فعالیت در سایت نیاز است ابتدا وارد حساب کاربری خود شوید. در صورتی که هنوز عضو سایت نیستید میتوانید در عرض تنها چند ثانیه ثبت نام کنید.

لطفا ابتدا لاگین کنید