public static void DisplayListOfMotors(List <Vehicle> vehicles) { foreach (Vehicle vehicle in vehicles) { if (vehicle is MotorCycle) { //property must be explicitly cast back to a flat to access its fields and methods from //the Car Class; MotorCycle motorCycle = vehicle as MotorCycle; motorCycle.DisplayMotorCycleInfo(); } } }
public static void CalculateValueOfStock(List <Vehicle> vehicles) { int totatlValueOfStock = 0; foreach (Vehicle vehicle in vehicles) { if (vehicle is MotorCycle) { MotorCycle motorCycle = vehicle as MotorCycle; if (!vehicle.sold) { totatlValueOfStock += totatlValueOfStock + vehicle.price; } } } Console.WriteLine("Total value of motorcycle stock " + totatlValueOfStock); }
static void Main(string[] args) { Console.WriteLine("Inside the Vehicle showroom"); Console.WriteLine(""); List <Vehicle> vehicles = new List <Vehicle>(); Car fiesta = new Car("FES-UIT8", "60m/h", 200000, false); vehicles.Add(fiesta); Car ferrari = new Car("FER-IU8", "80m/h", 500000, false); vehicles.Add(ferrari); Console.WriteLine(""); Console.WriteLine("********Cars info before sell***********"); Car.DisplayListOfCars(vehicles); Console.WriteLine("***************************************"); fiesta.sellCar(120000); Console.WriteLine(""); Console.WriteLine("********Cars info after selling***********"); Car.DisplayListOfCars(vehicles); Console.WriteLine("*****************************************"); MotorCycle rxSporty = new MotorCycle("MRX-98HJ", "45m/h", 100000, false, 0); vehicles.Add(rxSporty); MotorCycle bullet = new MotorCycle("MBT-897HJ", "40m/h", 120000, false, 0); vehicles.Add(bullet); Console.WriteLine("**********MotoryCycles info before sell"); MotorCycle.DisplayListOfMotors(vehicles); Console.WriteLine("***************************************"); rxSporty.sellMotorCycle(55000); Console.WriteLine("*********MotorCycles info after sell"); MotorCycle.DisplayListOfMotors(vehicles); Console.WriteLine("***************************************"); Console.WriteLine(""); Car.CalculateValueOfStock(vehicles); MotorCycle.CalculateValueOfStock(vehicles); MotorCycle.TotalValueOfSalesMotorCylce(); Car.TotalValueOfSalesCars(); Console.WriteLine(""); fiesta.DisplayVehicleInfo(); vehicles.Clear(); Vehicle vehicle1 = new Car(); Car car1 = new Car(); Vehicle vehicle2 = new MotorCycle(); vehicles.Add(vehicle1); vehicles.Add(vehicle2); vehicles.Add(car1); foreach (Vehicle vehicle in vehicles) { Console.WriteLine("Type : " + vehicle.GetType()); vehicle.NumberOfWheels(); } }