Complete vehicle management exercise
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package org.model;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Vehicle[] vehicles = new Vehicle[3];
|
||||
|
||||
vehicles[0] = new ElectricCar("Tesla", "Model 3", 2024, 5000, 5, 75, 80);
|
||||
vehicles[1] = new GasCar("Toyota", "Corolla", 2022, 13000, 5, 60, "Petrol");
|
||||
vehicles[2] = new SportsCar("BMW", "M4", 2023, 6000, 4, 70, "Petrol", 3.8);
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
System.out.println("----- Basic Info -----");
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
|
||||
System.out.println("----- Details -----");
|
||||
v.showDetails();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println("----- Inspection -----");
|
||||
for (Vehicle v : vehicles) {
|
||||
if (v instanceof SportsCar) {
|
||||
System.out.println(v.getModel() + " is a SportsCar.");
|
||||
} else if (v instanceof ElectricCar) {
|
||||
System.out.println(v.getModel() + " is an ElectricCar.");
|
||||
} else if (v instanceof GasCar) {
|
||||
System.out.println(v.getModel() + " is a GasCar.");
|
||||
} else {
|
||||
System.out.println(v.getModel() + " is an unknown vehicle type.");
|
||||
}
|
||||
}
|
||||
|
||||
Serviceable[] serviceables = new Serviceable[3];
|
||||
serviceables[0] = (Serviceable) vehicles[0];
|
||||
serviceables[1] = (Serviceable) vehicles[1];
|
||||
serviceables[2] = (Serviceable) vehicles[2];
|
||||
|
||||
System.out.println("----- Service -----");
|
||||
for (Serviceable s : serviceables) {
|
||||
s.performService();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user