complete all workshop TODOs
This commit is contained in:
+38
-116
@@ -1,126 +1,48 @@
|
||||
package org;
|
||||
import org.model.*;
|
||||
import org.behavior.Serviceable;
|
||||
import org.util.*;
|
||||
|
||||
public class Main {
|
||||
public class Main{
|
||||
public static void main(String[] args) {
|
||||
// Creating instances for each car type with realistic values
|
||||
ElectricCar tesla =new ElectricCar("Tesla", "Model S", 2022, 9000, 5, 100, 45);
|
||||
GasCar toyota= new GasCar("Toyota", "Corolla", 2018, 15000, 5, 60, "Petrol");
|
||||
SportsCar ferrari =new SportsCar("Ferrari", "F8 Tributo", 2021, 6000, 2, 40, "Petrol", 2.9);
|
||||
//Demonstrating polymorphism by storing different subclasses in a Vehicle array
|
||||
Vehicle[] vehicles = { tesla, toyota, ferrari };
|
||||
|
||||
// ============================================================
|
||||
// TODO 1:
|
||||
// Create one instance of each vehicle type:
|
||||
// - ElectricCar
|
||||
// - GasCar
|
||||
// - SportsCar
|
||||
//
|
||||
// Requirements:
|
||||
// • Use the constructors you created in each class.
|
||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
||||
// • Store them in variables such as:
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 2:
|
||||
// Create an array of Vehicle:
|
||||
// Vehicle[] vehicles = { ... };
|
||||
//
|
||||
// This array MUST contain the objects created above.
|
||||
// This demonstrates **polymorphism** because all different
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 3:
|
||||
// Loop through the Vehicle[] array and:
|
||||
// • Call v.basicInfo();
|
||||
// • Print a blank line after each.
|
||||
//
|
||||
// Purpose:
|
||||
// Demonstrate that all subclasses share the same Vehicle
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 4:
|
||||
// Use VehicleInspector to inspect each vehicle:
|
||||
//
|
||||
// VehicleInspector.inspect(v);
|
||||
//
|
||||
// Purpose:
|
||||
// • Demonstrate the use of `instanceof`
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 5:
|
||||
// Create an array of Serviceable:
|
||||
// Serviceable[] serviceables = { ... };
|
||||
//
|
||||
// This demonstrates **interface‑based polymorphism**.
|
||||
// Every Car subtype implements Serviceable.
|
||||
//
|
||||
// IMPORTANT:
|
||||
// • You must cast to Vehicle when needed:
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 6:
|
||||
// Loop through the Serviceable[] array:
|
||||
//
|
||||
// Steps inside loop:
|
||||
//
|
||||
// 1. Cast the current object to Vehicle:
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// 2. Check:
|
||||
// if (s.needsService(v)) { ... }
|
||||
//
|
||||
// 3. If true:
|
||||
// - Print a message showing brand + model
|
||||
// - Call s.performService();
|
||||
//
|
||||
// 4. Otherwise:
|
||||
// - Print that the car does not need service
|
||||
//
|
||||
// Add blank lines between each item for readability.
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 7 (Optional):
|
||||
// After everything is done, print a final message:
|
||||
//
|
||||
// System.out.println("=== Workshop Completed ===");
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
// Looping through vehicles to display basic inherited info
|
||||
System.out.println("--- Basic Vehicle Info ---");
|
||||
for (Vehicle v : vehicles) {
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
//Using the inspector utility to perform type-checking with instanceof
|
||||
System.out.println("--- Vehicle Inspection ---");
|
||||
for (Vehicle v : vehicles) {
|
||||
VehicleInspector.inspect(v);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// Demonstrating interface-based polymorphism by storing cars in a Serviceable array
|
||||
Serviceable[] serviceables = { tesla, toyota, ferrari };
|
||||
|
||||
//Iterating through serviceables, casting to Vehicle to check mileage limits, and performing services
|
||||
System.out.println("--- Service Management System ---");
|
||||
for (Serviceable s : serviceables) {
|
||||
Vehicle v = (Vehicle) s;
|
||||
|
||||
if (s.needsService(v)) {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " needs service.");
|
||||
s.performService();
|
||||
} else {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " is in good condition. No service needed.");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
//Final workshop completion message
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user