Answer to Question #188612 in C# for CHANDRASENA REDDY

Question #188612

You need to write a program to manage the Inventory of the used cars.


Task 1: Create a simple text-based “Console Application” in C# to maintain a catalog of used cars. The catalog keeps track of each car's make, model, year, and sale price. The program begins with an empty catalog. The program can perform the following operations:

• Adding a new car

• Modify the details of a particular car

• Search for a particular car in the Catalog

• List all the cars in the Catalog

• Delete a car from the Catalog

• Quit


If an unknown command is entered, the user should be informed and asked to enter another command. Hint:

1) Create a class called as Car. Create appropriate constructors (Default and Parameterized), Properties for the Car class.

2) Use Array to store the Objects of a car.

Once the code is ready, get a peer review done. Maintain the list of issues / bugs identified during the review. You are supposed to fix those issues.


1
Expert's answer
2021-05-05T00:04:33-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace C_SHARP
{
    class Program
    {


        class Car
        {
            //make, model, year, and sale price
            public string Make { get; set; }
            public string Model{get;set;}
            public int Year{get;set;}
            public double SalePrice{get;set;}
            /// <summary>
            /// Default Constructor
            /// </summary>
            public Car() { }
            /// <summary>
            /// Parameterized Constructor
            /// </summary>
            /// <param name="make"></param>
            /// <param name="model"></param>
            /// <param name="year"></param>
            /// <param name="salePrice"></param>
            public Car(string make, string model, int year, double salePrice)
            {
                this.Make = make;
                this.Model = model;
                this.Year = year;
                this.SalePrice = salePrice;
            }


        }


        static void Main(string[] args)
        {
            int operation = 0;
            Car[] allCars = new Car[20];
            int nCars = 0;
            
            while (operation!=6)
            {
                Console.WriteLine("1. Adding a new car");
                Console.WriteLine("2. Modify the details of a particular car");
                Console.WriteLine("3. Search for a particular car in the Catalog");
                Console.WriteLine("4. List all the cars in the Catalog");
                Console.WriteLine("5. Delete a car from the Catalog");
                Console.WriteLine("6. Quit");


                Console.Write("Select one menu item: ");
                operation = int.Parse(Console.ReadLine());
                switch (operation) { 
                    case 1:
                        AddNewCar(allCars,ref nCars);
                        break;
                    case 2:
                        //Modify the details of a particular car
                        ModifyDetailsParticularCar(allCars, nCars);
                        break;
                    case 3:
                        //Search for a particular car in the Catalog
                        SearchParticularCarCatalog(allCars, nCars);
                        break;
                    case 4:
                        //List all the cars in the Catalog
                        ListAllCarsInCatalog(allCars, nCars);
                        break;
                    case 5:
                        DeleteCarFromCatalog(allCars,ref nCars);
                        break;
                    case 6:
                        //close the program
                        break;
                    default:
                        Console.WriteLine("Wrong menu item.");
                        break;
                }


            }
        }
        /// <summary>
        /// Adding a new car
        /// </summary>
        /// <param name="allCars"></param>
        /// <param name="nCars"></param>
        private static void AddNewCar(Car[] allCars, ref int nCars)
        {
            allCars[nCars] = new Car();
            Console.Write("Enter make of a car: ");
            allCars[nCars].Make = Console.ReadLine();
            Console.Write("Enter model of a car: ");
            allCars[nCars].Model = Console.ReadLine();
            Console.Write("Enter year of a car: ");
            allCars[nCars].Year = int.Parse(Console.ReadLine());
            Console.Write("Enter sale price of a car: ");
            allCars[nCars].SalePrice = double.Parse(Console.ReadLine());
            nCars++;
        }
        
        /// <summary>
        /// List all the cars in the Catalog
        /// </summary>
        /// <param name="allCars"></param>
        /// <param name="nCars"></param>
        private static void ListAllCarsInCatalog(Car[] allCars, int nCars)
        {
            if (nCars > 0)
            {
                Console.WriteLine("{0,10}{1,20}{2,20}{3,20}{4,20}", "Car ID", "Make", "Model", "Year", "Sale Price");
                for (int i = 0; i < nCars; i++)
                {
                    Console.WriteLine("{0,10}{1,20}{2,20}{3,20}{4,20}", (i + 1), allCars[i].Make, allCars[i].Model, allCars[i].Year, allCars[i].SalePrice);
                }
            }
            else
            {
                Console.WriteLine("\nThe catalog is empty.\n");
            }
        }
        /// <summary>
        /// Modify the details of a particular car
        /// </summary>
        /// <param name="allCars"></param>
        /// <param name="nCars"></param>
        private static void ModifyDetailsParticularCar(Car[] allCars, int nCars)
        {
            if (nCars > 0)
            {
                ListAllCarsInCatalog(allCars, nCars);
                Console.Write("Enter ID of a car you want to edit: ");
                int selectedCar = int.Parse(Console.ReadLine());
                selectedCar--;
                if (selectedCar >= 0 && selectedCar < nCars)
                {
                    Console.Write("Enter a new make of a car: ");
                    allCars[selectedCar].Make = Console.ReadLine();
                    Console.Write("Enter a new model of a car: ");
                    allCars[selectedCar].Model = Console.ReadLine();
                    Console.Write("Enter a new year of a car: ");
                    allCars[selectedCar].Year = int.Parse(Console.ReadLine());
                    Console.Write("Enter a new sale price of a car: ");
                    allCars[selectedCar].SalePrice = double.Parse(Console.ReadLine());
                    Console.WriteLine("\nSelected car has been updated.\n");
                }
                else
                {
                    Console.WriteLine("\nWrong ID.\n");
                }
            }
            else
            {
                Console.WriteLine("\nThe catalog is empty.\n");
            }
        }
        /// <summary>
        /// Search for a particular car in the Catalog
        /// </summary>
        /// <param name="allCars"></param>
        /// <param name="nCars"></param>
        private static void SearchParticularCarCatalog(Car[] allCars, int nCars)
        {
            if (nCars > 0)
            {
                Console.Write("Enter make of a car to search: ");
                string make = Console.ReadLine();
                Console.WriteLine("{0,10}{1,20}{2,20}{3,20}{4,20}", "Car ID", "Make", "Model", "Year", "Sale Price");
                for (int i = 0; i < nCars; i++)
                {
                    bool exist = false;
                    if (allCars[i].Make.CompareTo(make) == 0 || allCars[i].Model.CompareTo(make) == 0)
                    {
                        Console.WriteLine("{0,10}{1,20}{2,20}{3,20}{4,20}", (i + 1), allCars[i].Make, allCars[i].Model, allCars[i].Year, allCars[i].SalePrice);
                        exist = true;
                    }
                    if (!exist)
                    {
                        Console.WriteLine("\nThe car with the make does not exist.\n");
                    }
                }
            }
            else {
                Console.WriteLine("\nThe catalog is empty.\n");
            }
        }


        /// <summary>
        /// Delete a car from the Catalog
        /// </summary>
        /// <param name="allCars"></param>
        /// <param name="nCars"></param>
        private static void DeleteCarFromCatalog(Car[] allCars, ref int nCars)
        {
            
            if (nCars > 0)
            {
                ListAllCarsInCatalog(allCars, nCars);
                Console.Write("Enter ID of a car to delete: ");
                int selectedCar = int.Parse(Console.ReadLine());
                selectedCar--;
                if (selectedCar >= 0 && selectedCar < nCars)
                {
                    for (int i = selectedCar; i < nCars - 1; i++)
                    {
                        allCars[i] = allCars[i + 1];
                    }
                    nCars--;
                    Console.WriteLine("\nThe car has been deleted.\n");
                }
                else
                {
                    Console.WriteLine("\nWrong ID.\n");
                }
            }
            else
            {
                Console.WriteLine("\nThe catalog is empty.\n");
            }
        }
        
    }
}

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