develop #1
+39
-113
@@ -1,125 +1,51 @@
|
||||
package org;
|
||||
|
||||
import org.model.HybridCar;
|
||||
import org.behavior.Serviceable;
|
||||
import org.model.*;
|
||||
import org.util.VehicleInspector;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// ============================================================
|
||||
// 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(...);
|
||||
// ============================================================
|
||||
ElectricCar tesla = new ElectricCar("Tesla","3",2020,7000,5,100,60);
|
||||
GasCar pride = new GasCar("Saipa","132",2002,200000,5,50,"Gasoline");
|
||||
HybridCar prius = new HybridCar("Toyota","Prius",2025,3000,5,70,90);
|
||||
SportsCar ferrari = new SportsCar("Ferrari","488 GTB",2015,80000,2,20,"Gasoline",2.9);
|
||||
|
||||
|
||||
Vehicle[] vehicles = {tesla,pride,prius,ferrari};
|
||||
System.out.println("=".repeat(20) + " Show Vehicles " + "=".repeat(20));
|
||||
for (Vehicle vehicle : vehicles) {
|
||||
vehicle.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println("=".repeat(20) + " Inspect Vehicles " + "=".repeat(20));
|
||||
for (Vehicle vehicle : vehicles) {
|
||||
VehicleInspector.inspect(vehicle);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
Serviceable[] serviceables = {prius,ferrari,tesla,pride};
|
||||
|
||||
System.out.println("=".repeat(20) + " Check Services " + "=".repeat(20));
|
||||
for (Serviceable serviceable : serviceables) {
|
||||
Vehicle v = (Vehicle) serviceable;
|
||||
if (serviceable.needsService(v)) {
|
||||
System.out.println("brand: " + v.getBrand() + " | model: " + v.getModel());
|
||||
serviceable.performService();
|
||||
System.out.println();
|
||||
}
|
||||
else {
|
||||
System.out.println("brand: " + v.getBrand() + " | model: " + v.getModel() + " doesn't needs service");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// 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 ===");
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
System.out.println("=".repeat(20) + " Workshop Completed " + "=".repeat(20));
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user