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.
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
private string make;
public string Make
{
get { return make; }
set { make = value; }
}
private string model;
public string Model
{
get { return model; }
set { model = value; }
}
private int year;
public int Year
{
get { return year; }
set { year = value; }
}
private double salePrice;
public double SalePrice
{
get { return salePrice; }
set { salePrice = value; }
}
/// <summary>
/// Constructor
/// </summary>
public Car() { }
/// <summary>
/// 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){
string choice = " ";
Car[] cars = new Car[100];
int totalCarsInCatalog=0;
string make;
string model;
int year;
double salePrice;
int ID;
while (choice.CompareTo("6")!=0)
{
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: ");
choice = Console.ReadLine();
if (choice.CompareTo("1") == 0)
{
//Adding a new car
Console.Write("Enter make of a car: ");
make = Console.ReadLine();
Console.Write("Enter model of a car: ");
model = Console.ReadLine();
Console.Write("Enter year of a car: ");
year =int.Parse(Console.ReadLine());
Console.Write("Enter sale price of a car: ");
salePrice = double.Parse(Console.ReadLine());
cars[totalCarsInCatalog] = new Car(make, model, year, salePrice);
totalCarsInCatalog++;
}
else if (choice.CompareTo("2") == 0)
{
//Modify the details of a particular car
if (totalCarsInCatalog > 0)
{
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", "ID", "Make", "Model", "Year", "Sale Price");
for (int i = 0; i < totalCarsInCatalog; i++)
{
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", (i + 1), cars[i].Make, cars[i].Model, cars[i].Year, cars[i].SalePrice);
}
Console.Write("Enter ID of a car you want to edit: ");
ID = int.Parse(Console.ReadLine());
ID--;
if (ID >= 0 && ID < totalCarsInCatalog)
{
Console.Write("Enter a new make of a car: ");
make = Console.ReadLine();
Console.Write("Enter a new model of a car: ");
model = Console.ReadLine();
Console.Write("Enter a new year of a car: ");
year = int.Parse(Console.ReadLine());
Console.Write("Enter a new sale price of a car: ");
salePrice = double.Parse(Console.ReadLine());
cars[ID] = new Car(make, model, year, salePrice);
Console.WriteLine("\nSelected car has been updated.\n");
}
else
{
Console.WriteLine("\nSelect correct ID.\n");
}
}
}
else if (choice.CompareTo("3") == 0)
{
//Search for a particular car in the Catalog
if (totalCarsInCatalog > 0)
{
Console.Write("Enter make or model of a car you want to search: ");
make = Console.ReadLine();
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", "ID", "Make", "Model", "Year", "Sale Price");
for (int i = 0; i < totalCarsInCatalog; i++)
{
bool isFound = false;
if (cars[i].Make.CompareTo(make) == 0 || cars[i].Model.CompareTo(make) == 0)
{
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", (i + 1), cars[i].Make, cars[i].Model, cars[i].Year, cars[i].SalePrice);
isFound = true;
}
if (!isFound)
{
Console.WriteLine("\nThe car with such make or model does not exist.\n");
}
}
}
}
else if (choice.CompareTo("4") == 0)
{
//List all the cars in the Catalog
if (totalCarsInCatalog > 0)
{
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", "ID", "Make", "Model", "Year", "Sale Price");
for (int i = 0; i < totalCarsInCatalog; i++)
{
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", (i + 1), cars[i].Make, cars[i].Model, cars[i].Year, cars[i].SalePrice);
}
}
}
else if (choice.CompareTo("5") == 0)
{
// Delete a car from the Catalog
if (totalCarsInCatalog > 0)
{
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", "ID", "Make", "Model", "Year", "Sale Price");
for (int i = 0; i < totalCarsInCatalog; i++)
{
Console.WriteLine("{0,10}{1,15}{2,15}{3,15}{4,15}", (i + 1), cars[i].Make, cars[i].Model, cars[i].Year, cars[i].SalePrice);
}
Console.Write("Enter ID of a car you want to delete: ");
ID = int.Parse(Console.ReadLine());
ID--;
if (ID >= 0 && ID < totalCarsInCatalog)
{
for (int i = ID; i < totalCarsInCatalog - 1; i++)
{
cars[i] = cars[i + 1];
}
totalCarsInCatalog--;
Console.WriteLine("\nSelected car has been deleted.\n");
}
else
{
Console.WriteLine("\nSelect correct ID.\n");
}
}
}
else if (choice.CompareTo("6") == 0)
{
//exit
}
else
{
Console.WriteLine("Select correct menu item.");
}
}
}
}
}
Output
Comments
Leave a comment