Answer to Question #189802 in C# for CHANDRASENA REDDY

Question #189802

lab-13:-

Using Serialization to persist business data


Q1. You have already created the Contact class. You need to store List of Contacts in binary format on disk. Perform Binary Serialization to store the List. To Do: Write the Program to Accept the data for multiple contacts, store them in a List. Serialize the List using Binary formatter.


Q2. The Client has suggest an Enhancement to the above code. You need to write the code to Deserialize the data from the Binary file, and print the details.


1
Expert's answer
2021-05-15T13:53:24-0400
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace ContractList
{
    class Program
    {
        private static List<Contract> contracts = new List<Contract>();
            
        private static void Main(string[] args)
        {            
            while (true)
            {
                Console.WriteLine(@"Options:
1. Add contract
2. Serialize added contracts to file
3. Deserialize contracts from from file
4. Quit");
                Console.Write("Choose an option (1-4): ");
                var input = Console.ReadLine();


                if (!int.TryParse(input, out var option))
                {
                    Console.WriteLine();
                    continue;
                }                    


                switch (option)
                {
                    case 1:
                        AddContract();
                        break;


                    case 2:
                        SerializeContracts();
                        break;


                    case 3:
                        DeserializeContracts();
                        break;
                    
                    case 4:
                        return;


                    default:
                        break;
                }


                Console.WriteLine();
            }
        }


        private static void AddContract()
        {
            var contract = new Contract();
            
            Console.Write("Input contract no: ");
            contract.ContractNo = int.Parse(Console.ReadLine());


            Console.Write("Input contract name: ");
            contract.ContractName = Console.ReadLine();


            Console.Write("Input cell no: ");
            contract.CellNo = Console.ReadLine();


            contracts.Add(contract);
            Console.WriteLine("Contract has been added");            
        }


        private static void SerializeContracts()
        {            
            Console.Write("Input filename: ");
            var filename = Console.ReadLine();


            var serializer = new ContractSerializer();
            serializer.Serialize(filename, contracts);


            Console.WriteLine($"Contracts has been serialized to file {filename}");
        }


        private static void DeserializeContracts()
        {
            Console.Write("Input filename: ");
            var filename = Console.ReadLine();


            var serializer = new ContractSerializer();
            var deserialized = serializer.Deserialize(filename);


            if (deserialized.Count == 0)
            {
                Console.WriteLine("No contracts");
                return;
            }


            Console.WriteLine("Deserialized contracts:");
            for (var i = 0; i < deserialized.Count; i++)
                Console.WriteLine($"{i + 1}. {deserialized[i]}");
        }
    }


    public class ContractSerializer
    {
        public void Serialize(string filename, List<Contract> contracts)
        {            
            var formatter = new BinaryFormatter();
            
            using (var fs = new FileStream(filename, FileMode.OpenOrCreate))
            {
                formatter.Serialize(fs, contracts);
            }            
        }


        public List<Contract> Deserialize(string filename)
        {
            var formatter = new BinaryFormatter();


            using (var fs = new FileStream(filename, FileMode.OpenOrCreate))
            {
                return (List<Contract>) formatter.Deserialize(fs);
            }
        }
    }


    [Serializable]
    public class Contract
    {
        public int ContractNo { get; set; }
        public string ContractName { get; set; }
        public string CellNo { get; set; }
        
        public override string ToString() => $"Contract (No: {ContractNo}, Name: {ContractName}, Cell No: {CellNo})";
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS