this is prac 3:
Task You need to implement a program for a car salesman who wants to keep record of the cars he has for sale. For each car he keeps the following: Registration Number (always unique), Model,Colour and Price Implement the following classes Class Car It must have a constructor, the necessary Get and Set methods, as well as the following methods:public boolean isCheap()// returns TRUE if the car is selling for under R10000.00 public void decreasePrice(double Amount)// decreases the selling price by Amount public void Display()// displays the Registration Number and Price of a car Class CarList It must contain the standard methods you would expect in a list class. The class must be able to interact with a data file (in other words, get initial data from a data file, and write the final data to a data file), and uses ArrayLists to implement a list Application Class The application class must have methods that do the following
using System;
using System.Collections.Generic;
namespace App
{
class Car
{
private int registrationNumber;
public int RegistrationNumber
{
get { return registrationNumber; }
set { registrationNumber = value; }
}
private string model;
public string Model
{
get { return model; }
set { model = value; }
}
private string colour;
public string Colour
{
get { return colour; }
set { colour = value; }
}
private double price;
public double Price
{
get { return price; }
set { price = value; }
}
public Car(int registrationNumber, string model, string colour, double price)
{
this.registrationNumber = registrationNumber;
this.model = model;
this.colour = colour;
this.price = price;
}
/// <summary>
/// Cars should be classified as cheap if the selling price is < R10000.00
/// </summary>
/// <returns></returns>
public bool isCheap()
{
if (this.price < 10000.00)
{
return true;
}
return false;
}
public void decreasePrice(double Amount) {
this.price -= Amount;
}
public void Display()
{
Console.WriteLine("Registration number: " + registrationNumber.ToString() + Environment.NewLine +
"Model: " + model + Environment.NewLine +
"Colour: " + colour + Environment.NewLine +
"Price: R" + price.ToString() + Environment.NewLine);
}
}
class CarList
{
private List<Car> cars = new List<Car>();
public CarList()
{
}
public void displayCheapestCars() {
Console.WriteLine("The details of the cheapest cars: ");
for (int i = 0; i < cars.Count; i++)
{
if (cars[i].isCheap()) {
cars[i].Display();
}
}
}
public void addCar(Car car)
{
this.cars.Add(car);
}
public void display()
{
for (int i = 0; i < cars.Count; i++)
{
cars[i].Display();
}
}
public void decreasePrice(double Amount)
{
for (int i = 0; i < cars.Count; i++)
{
cars[i].decreasePrice(Amount);
}
}
}
class Program
{
static void Main(string[] args)
{
CarList carList = new CarList();
carList.addCar(new Car(5646321, "Ford X5", "Red", 45000));
carList.addCar(new Car(7894546, "Ford X8", "Blue", 5000));
carList.addCar(new Car(8745565, "Reno X45", "Yellow", 95050));
carList.addCar(new Car(4575457, "Ford X2", "Green", 9000));
carList.addCar(new Car(2454477, "Ford S2", "Black", 25000));
carList.display();
carList.displayCheapestCars();
Console.WriteLine("decrease price 2000");
carList.decreasePrice(2000);
carList.display();
Console.ReadLine();
}
}
}
Comments
Leave a comment