Design a Windows Form C program for a car sale Assuming ther
Design a Windows Form C# program for a car sale. Assuming there are only a few car models (3 to 5).
(1) Prepare a text file to be read into the program for car information: Car model name, Car price, and Car image file name. Prepare also the car image files. Allow the choice of one of the cars to display its name, its price, and its image.
(2) Read in the OhioSalesTaxRates.txt file and allow the choice of one of the counties for a corresponding sale tax rate.
(3) Show the selected car model, car image, car price, tax rate, tax, and total price.
Solution
class Program
{
static int indx = 0;
static Car[] Catelog = new Car[20];
static void Main(string[] args)
{
string command = string.Empty;
while (command != \"6\")
{
Console.WriteLine(\"Enter the number of the command you want: \");
Console.WriteLine(\"1. Add \");
Console.WriteLine(\"2. List \");
Console.WriteLine(\"3. List By Price \");
Console.WriteLine(\"4. List By Year \");
Console.WriteLine(\"5. Search By Make\");
Console.WriteLine(\"6. Quit \");
command = Console.ReadLine();
switch (command)
{
case \"1\":
{
Console.Write(\"Year: \");
string year = Console.ReadLine();
Console.Write(\"Make: \");
string make = Console.ReadLine();
Console.Write(\"Model: \");
string model = Console.ReadLine();
Console.Write(\"Price: \");
string price = Console.ReadLine();
Add(year, make, model, Convert.ToInt32(price));
break;
}
case \"2\":
{
List();
break;
}
case \"3\":
{
ListByPrice();
break;
}
case \"4\":
{
//ListByYear();
break;
}
case \"5\":
{
Console.Write(\"Make: \");
string make = Console.ReadLine();
Search(make);
break;
}
case \"6\":
break;
}
}
}
private static void Search(string make)
{
for (int i = 0; i <= Catelog.Length - 1; i++)
{
if (Catelog[i].Make.ToLower() == make.ToLower())
{
Console.Write(\"{0}\\t\", Catelog[i].Year);
Console.Write(\"{0}\\t\", Catelog[i].Make);
Console.Write(\"{0}\\t\", Catelog[i].Model);
Console.Write(\"{0}\", Catelog[i].Price);
Console.WriteLine(\"\");
break;
}
}
}
public static void ListByYear()
{
Array.Sort(Catelog, (IComparer)new Car.SortByYearClass());
Console.WriteLine(\"Year Make Model Price\");
Console.WriteLine(\"=======================================================\");
for (int i = 0; i <= Catelog.Length - 1; i++)
{
if (Catelog[i] != null)
{
Console.Write(\"{0}\\t\", Catelog[i].Year);
Console.Write(\"{0}\\t\", Catelog[i].Make);
Console.Write(\"{0}\\t\", Catelog[i].Model);
Console.Write(\"{0}\", Catelog[i].Price);
Console.WriteLine(\"\");
}
}
}
public static void Add(string year, string make, string model, double price)
{
Catelog[indx] = new Car(year, make, model, price);
indx++;
}
public static void List()
{
Console.WriteLine(\"Year Make Model Price\");
Console.WriteLine(\"=======================================================\");
for (int i = 0; i <= Catelog.Length - 1; i++)
{
if (Catelog[i] != null)
{
Console.Write(\"{0}\\t\", Catelog[i].Year);
Console.Write(\"{0}\\t\", Catelog[i].Make);
Console.Write(\"{0}\\t\", Catelog[i].Model);
Console.Write(\"{0}\", Catelog[i].Price);
Console.WriteLine(\"\");
}
}
}
public static void ListByPrice()
{
Array.Sort(Catelog);
Console.WriteLine(\"Year Make Model Price\");
Console.WriteLine(\"=======================================================\");
for (int i = 0; i <= Catelog.Length - 1; i++)
{
if (Catelog[i] != null)
{
Console.Write(\"{0}\\t\", Catelog[i].Year);
Console.Write(\"{0}\\t\", Catelog[i].Make);
Console.Write(\"{0}\\t\", Catelog[i].Model);
Console.Write(\"{0}\", Catelog[i].Price);
Console.WriteLine(\"\");
}
}
}
}
public class Car : IComparable
{
public string sortby;
private string _make;
private string _model;
private double _price;
private string _year;
public Car(string year, string make, string model, double price)
{
Year = year;
Make = make;
Model = model;
Price = price;
}
public string Make
{
get
{
return _make;
}
set
{
_make = value;
}
}
public string Model
{
get
{
return _model;
}
set
{
_model = value;
}
}
public double Price
{
get
{
return _price;
}
set
{
_price = value;
}
}
public string Year
{
get
{
return _year;
}
set
{
_year = value;
}
}
int IComparable.CompareTo(Object c)
{
Car o = (Car)c;
int retval = this.Price.CompareTo(o.Price);
if (retval != 0)
{
return retval;
}
else
{
return this.Price.CompareTo(o.Price);
}
}
public class SortByYearClass : IComparer
{
public int Compare(object obj1, object obj2)
{
Car car1 = (Car)obj1;
Car car2 = (Car)obj2;
return (String.Compare(car1.Year, car2.Year));
}
}
}
}