Answer to Question #241240 in C# for Gayatri

Question #241240

Q1) Create a class with Customer with the following properties CustomerID and CustomerName and in that class only write functions:

a. List<Customer> retriveCustomers() to fill the customer class with objects and retrieve filled details of the customer .

b. FindCustomer (List<Customer> clist1,int custid1) find the customer from the above list of customers by fetching id from the user.

c. Updatecustomer(List<Customer> clist1, int custid1) to update a particular customer when id is given

d. Deletecustomer (List<Customer> clist1,int custid1) to delete the particular customer when id is given .


1
Expert's answer
2021-09-23T12:12:54-0400
using System;
using System.Collections.Generic;


namespace App
{




    class Customer
    {
        public int CustomerID { get; set; }
        public string CustomerName { get; set; }


        /// <summary>
        /// to fill the customer class with objects and retrieve filled details of the customer.
        /// </summary>
        /// <returns></returns>
        public static List<Customer> retriveCustomers()
        {
            List<Customer> clist1 = new List<Customer>();
            Console.Write("Enter a number of customers: ");
            int n  = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                Customer cus = new Customer();
                Console.Write("Enter Customer ID: ");
                cus.CustomerID = int.Parse(Console.ReadLine());
                Console.Write("Enter Customer name: ");
                cus.CustomerName = Console.ReadLine();
                clist1.Add(cus);
                
                
            }
            
            return clist1;
        }
        /// <summary>
        /// find the customer from the above list of customers by fetching id from the user.
        /// </summary>
        /// <param name="clist1"></param>
        /// <param name="custid1"></param>
        public static int FindCustomer(List<Customer> clist1, int custid1)
        {


            for (int i = 0; i < clist1.Count; i++)
            {
                if (clist1[i].CustomerID == custid1)
                {
                    return i;
                }
            }
            return -1;
        }


        /// <summary>
        /// Updatecustomer(List<Customer> clist1, int custid1) to update a particular customer when id is given
        /// </summary>
        /// <param name="clist1"></param>
        /// <param name="custid1"></param>
        public static void Updatecustomer(List<Customer> clist1, int custid1)
        {
            for (int i = 0; i < clist1.Count; i++)
            {
                if (clist1[i].CustomerID == custid1)
                {
                    Console.Write("Enter a new Customer ID: ");
                    clist1[i].CustomerID=int.Parse(Console.ReadLine());
                    Console.Write("Enter a new Customer name: ");
                    clist1[i].CustomerName = Console.ReadLine();
                    break;
                }
            }
        }


        /// <summary>
        /// to delete the particular customer when id is given .
        /// </summary>
        /// <param name="clist1"></param>
        /// <param name="custid1"></param>
        public static void Deletecustomer(List<Customer> clist1, int custid1)
        {
            for (int i = 0; i < clist1.Count; i++) {
                if (clist1[i].CustomerID == custid1) {
                    clist1.RemoveAt(i);
                    break;
                }
            }
        }


    }


    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> clist1=Customer.retriveCustomers();
            Console.Write("Enter a new Customer ID to search: ");
            int CustomerID = int.Parse(Console.ReadLine());
            int index = Customer.FindCustomer(clist1, CustomerID);
            Console.WriteLine("Customer ID: {0}", clist1[index].CustomerID);
            Console.WriteLine("Customer Name: {0}", clist1[index].CustomerName);


            Console.Write("Enter a new Customer ID to edit: ");
            CustomerID = int.Parse(Console.ReadLine());
            Customer.Updatecustomer(clist1, CustomerID);
            index = Customer.FindCustomer(clist1, CustomerID);
            if (index != -1) {
                Console.WriteLine("Customer ID: {0}", clist1[index].CustomerID);
                Console.WriteLine("Customer Name: {0}", clist1[index].CustomerName);
            }
            


            Console.Write("Enter a new Customer ID to delete: ");
            CustomerID = int.Parse(Console.ReadLine());
            Customer.Deletecustomer(clist1, CustomerID);






            Console.ReadLine();
        }
    }
}

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