Write a C#.Net program to accept and display ‘n’ customer’s details such as customer_no, Name, address, item_no, quantity, and price. Display the total price of all items.

Answer:

using System;

namespace CustomerDetails
{
    // Class to store customer and item details
    class Customer
    {
        public int CustomerNo;
        public string Name;
        public string Address;
        public int ItemNo;
        public int Quantity;
        public double Price;

        // Method to calculate total price for this item
        public double CalculateTotalPrice()
        {
            return Quantity * Price;
        }

        // Method to display customer and item details
        public void DisplayDetails()
        {
            Console.WriteLine("\nCustomer Details:");
            Console.WriteLine("Customer No: " + CustomerNo);
            Console.WriteLine("Name: " + Name);
            Console.WriteLine("Address: " + Address);
            Console.WriteLine("Item No: " + ItemNo);
            Console.WriteLine("Quantity: " + Quantity);
            Console.WriteLine("Price: " + Price);
            Console.WriteLine("Total Price: " +CalculateTotalPrice());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the number of customers: ");
            int n = int.Parse(Console.ReadLine()); // Number of customers

            // Array to store customer objects
            Customer[] customers = new Customer[n];

            double grandTotal = 0; // Variable to keep track of total price of all items

            // Loop to input details for each customer
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("\nEnter details for Customer" + (i + 1) + ":");

                // Create new Customer object for each entry
                customers[i] = new Customer();

                // Accept customer details
                Console.Write("Customer No: ");
                customers[i].CustomerNo = int.Parse(Console.ReadLine());

                Console.Write("Name: ");
                customers[i].Name = Console.ReadLine();

                Console.Write("Address: ");
                customers[i].Address = Console.ReadLine();

                Console.Write("Item No: ");
                customers[i].ItemNo = int.Parse(Console.ReadLine());

                Console.Write("Quantity: ");
                customers[i].Quantity = int.Parse(Console.ReadLine());

                Console.Write("Price per item: ");
                customers[i].Price = double.Parse(Console.ReadLine());

                // Display the details and calculate total price for the current customer
                customers[i].DisplayDetails();

                // Add the total price of this customer to the grand total
                grandTotal += customers[i].CalculateTotalPrice();
            }

            // Display the grand total price of all items
            Console.WriteLine("\nGrand Total Price of all items:" + grandTotal);
            Console.ReadLine();
        }
    }
}
Scroll to Top