Answer to Question #189405 in C# for CHANDRASENA REDDY

Question #189405

You need to maintain a Contact List in a generic List Collection. You need to perform the following tasks: i. AddContact() – To add contact detail to List

ii. DisplayContact() – To display particular contact detail from List

iii. EditContact() – To modiy particular contact detail from List

iv. ShowAllContacts() – To display all contact details from List


Task 1: Create the Contact class with the following properties:

public int ContactNo{get;set;}

public string ContactName{get;set;}

public string CellNo{get;set;}

Task 2: Create a Console application and write the Code for the required functionality mentioned above.


Hint: a. There is a loop in Main() function which accepts the selection option

b. There are additional 4 static functions to perform required tasks which are called based on selection c. Use the List generic collection to maintain the list.


1
Expert's answer
2021-05-11T23:23:38-0400
using System;
using System.Collections.Generic;


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. Display contract
3. Edit contract
4. Show all contracts
5. Quit");
                Console.Write("Choose an option (1-5): ");
                var input = Console.ReadLine();


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


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


                    case 2:
                        DisplayContract();
                        break;


                    case 3:
                        EditContract();
                        break;


                    case 4:
                        ShowAllContracts();
                        break;


                    case 5:
                        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 DisplayContract()
        {
            Console.Write("Input contract no: ");
            var contractNo = int.Parse(Console.ReadLine());


            foreach (var item in contracts)
            {
                if (item.ContractNo == contractNo)
                {
                    Console.WriteLine(item);
                    return;
                }
            }


            Console.WriteLine("Error: contract with entered number does not exist!");
        }


        private static void EditContract()
        {
            Console.Write("Input contract no: ");
            var contractNo = int.Parse(Console.ReadLine());


            Contract contract = null;
            foreach (var item in contracts)
            {
                if (item.ContractNo == contractNo)
                {
                    contract = item;
                    break;
                }
            }


            if (contract == null)
            {
                Console.WriteLine("Error: contract with entered number does not exist!");
                return;
            }


            Console.Write("Input new contract no: ");
            contract.ContractNo = int.Parse(Console.ReadLine());


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


            Console.Write("Input new cell no: ");
            contract.CellNo = Console.ReadLine();
            
            Console.WriteLine("Contract has been updated");
            Console.WriteLine();
        }


        private static void ShowAllContracts()
        {
            if (contracts.Count == 0)
            {
                Console.WriteLine("No contracts");
                return;
            }
                
            for (var i = 0; i < contracts.Count; i++)
                Console.WriteLine($"{i + 1}. {contracts[i]}");
        }
    }


    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
New on Blog
APPROVED BY CLIENTS