+92 332 4229 857 99ProjectIdeas@Gmail.com

Phonebook (C#.net)



Phonebook Through Serialization:
Record is the Serialized Class contains the name , home number , mobile number and address of one person.
addNewRecord() function adds the new users to arrayList then in file.
loadRecord() function load all the save records from file to arrayList for further usage.
saveRecord() function saving the data in file updating.
enterInPhonebook() function is the main function which let you enter your home number or mobile number to access your stored information and then you can update your home number, mobile number and address.

Code

using System.Collections.Generic;

using System.Runtime.Serialization.Formatters.Binary; //namespace to use Serialization
 
[Serializable]
public class Record
{     
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
       
    private double homeNumber;

    public double HomeNumber
    {
        get { return homeNumber; }
        set { homeNumber = value; }
    }

    private double mobNumber;

    public double MobNumber
    {
        get { return mobNumber; }
        set { mobNumber = value; }
    }

    private string address;

    public string Address
    {
       get { return address; }
       set { address = value; }
    }

    // Default Constructor
    public Record()
    {
       this.name = String.Empty;
       this.homeNumber =  0000;
       this.MobNumber = 0000;
       this.address = "Fake";
    }

    // Parameterized Constructor
    public Record(string n, double hNum, double mobNum, string add)
    {
       this.name = n;
       this.homeNumber = hNum;
       this.mobNumber = mobNum;
       this.address = add;
    }

    public void print()
    {
       Console.WriteLine("The Name Is : " + this.name
               + Environment.NewLine + " Home Phone No. Is : " + this.homeNumber
               + Environment.NewLine + " Mobile Phone No. Is : " + this.mobNumber
               + Environment.NewLine + " Address Is : " + this.address);
    }
}

// Add a new Record first to ArrayList(recordList) and then in file
public void addNewRecord()
{
  
   recordList = new ArrayList();
   record = new Record();

   // Here Adding Record to ArrayList         
   recordList.Add(new Record("osama", 36887676, 3333003212,"Lahore"));
   recordList.Add(new Record("saad", 4234234, 33330065656, "Ali Park"));
   recordList.Add(new Record("bilal", 2123456, 0456123232, "Multan"));
   recordList.Add(new Record("maryam", 1312312, 33330032121,"Karachi"));
             
   FileStream fs = new FileStream("data.txt", FileMode.Create, FileAccess.Write);
   BinaryFormatter bf = new BinaryFormatter();
           
   for (int i = 0; i < recordList.Count; i++)
   {
     // here getting every Record object from arrayList
     record = (Record)recordList[i];
     bf.Serialize(fs, record);// Serialize the whole Record Object to save in file
     fs.Flush();
   }
         
  fs.Close();

}

// Loads the all reocrds from file and add to ArrayList(recordList)
public void loadRecord()
{           
   
recordList = new ArrayList();
FileStream fs = new FileStream("data.txt", FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
    try
      {
        while (fs.CanRead)//Reading the FileStream
        {
           Record r = (Record)bf.Deserialize(fs);//De-Serialize the each object
           recordList.Add(r); //And Add to ArrayList
           r.print(); //Printing the each record from the file to console
        }
      }
       catch (Exception e) {}              
   fs.Close();
}

// Saving the record to file.
public void saveRecord()
{
    
   FileStream fs = new FileStream("data.txt", FileMode.Create, FileAccess.Write);
   BinaryFormatter bf = new BinaryFormatter();

   for (int i = 0; i < recordList.Count; i++)
   {
       record = (Record)recordList[i];
       bf.Serialize(fs, record);
       fs.Flush();
   }
           
 fs.Close();    

     
}

public void enterInPhoneDirectory()
{
  
   record = new Record();
   loadRecord();

   Console.WriteLine("Enter Your Home Number/ Mobile Number : ");
   double number = double.Parse(Console.ReadLine()); // User to enter his/her Home/mobile no.

   for (int j = 0; j < recordList.Count; j++)
   {
       record = (Record)recordList[j];
    
        if (record.HomeNumber == number || record.MobNumber == number)
         {
            Console.WriteLine("Welcome , " + record.Name.ToUpper());
            record.print();
       
            Console.WriteLine("Press 1 to Change Home Number");
            Console.WriteLine("Press 2 to Change Mobile Number");
            Console.WriteLine("Press 3 to Change Address");
            Console.WriteLine("Press 4 to exit this menu");
            int option = int.Parse(Console.ReadLine());
                  
              switch (option)
               {
                     case 1:
                       Console.WriteLine("Enter Your New Home Number");
                       double hNum = double.Parse(Console.ReadLine());
                       record.HomeNumber = hNum;
                     break;

                     case 2:
                       Console.WriteLine("Enter Your New Mobile Number");
                       double mNum = double.Parse(Console.ReadLine());
                       record.MobNumber = mNum;
                     break;
                            
                     case 3:
                       Console.WriteLine("Enter Your New Address");
                       string add = Console.ReadLine();
                       record.Address = add;
                     break;
                           
                     case 4:
                       Console.Clear();
                       Main(new string[]{"s"});
                     break;
              }
                                   
          saveRecord();
        }             
    }
 }

public static void Main(string[] args)
{


    // Front end menu
    Console.WriteLine("----------Menu----------");
    Console.WriteLine("Press 1 to enter number (home / mobile)");
    Console.WriteLine("Press 2 to exit");

    int input = int.Parse(Console.ReadLine());

    switch (input)
    {
         case 1:
            enterInPhoneDirectory();
         break;

         case 2:
            Environment.Exit(0);
         break;
    }
 }

From the previous example of ATM (C#.net), you have seen that we used the simple file writing , file reading technique. And lot's of headache to save the file in a particular format and while reading the whole file splitting the data. But in this example you don't have to do that. Save the whole object through serialization and then retrieve the object, manipulate the object according to your need and then saving back in file. So its very easy.

Related Articles:


   How to write a object in file (Introduction to Serialization)
   How to read a object from file (Introduction to Serialization)
   How to write a object in file (Introduction to XmlSerialization)
   How to read a object from file (Introduction to XmlSerialization)

0 comments: