From 22726ea54d93fe150a87738f26573922c9f1d52f Mon Sep 17 00:00:00 2001 From: Amir mohammad Date: Sun, 24 May 2026 22:48:22 +0330 Subject: [PATCH] Complete VMS with polymorphism demonstration --- .idea/misc.xml | 2 +- src/main/java/org/Main.java | 144 ++++++------------- src/main/java/org/model/Car.java | 49 ++++--- src/main/java/org/model/ElectricCar.java | 49 ++++++- src/main/java/org/model/GasCar.java | 48 ++++++- src/main/java/org/model/SportsCar.java | 25 +++- src/main/java/org/model/Vehicle.java | 33 ++++- src/main/java/org/util/VehicleInspector.java | 17 ++- 8 files changed, 218 insertions(+), 149 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fdc35ea..0c04b52 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/src/main/java/org/Main.java b/src/main/java/org/Main.java index 7a3d8c8..a1d9f85 100644 --- a/src/main/java/org/Main.java +++ b/src/main/java/org/Main.java @@ -1,124 +1,62 @@ package org; +import org.model.Vehicle; +import org.model.ElectricCar; +import org.model.GasCar; +import org.model.SportsCar; +import org.util.VehicleInspector; +import org.behavior.Serviceable; + 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", "Model S", 2022, 1000, + 100, 550, 50 + ); + GasCar toyota = new GasCar( + "Toyota", "Corolla", 2018, 85000, + 5, 50, "Petrol" + ); + SportsCar ferrari = new SportsCar( + "Ferrari", "F8 Tributo", 2021, 12000, + 2, 3.9, "oil", 710 + ); + Vehicle[] vehicles = {tesla , toyota , ferrari}; - // ============================================================ - // 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). - // ============================================================ + for (Vehicle v : vehicles) { + v.basicInfo(); + System.out.println(); + } + for (Vehicle v : vehicles) { + VehicleInspector.inspect(v); + } + Serviceable[] serviceables = {tesla, toyota, ferrari}; + for (Serviceable s : serviceables) { + Vehicle v = (Vehicle) s; - // ============================================================ - // 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. - // ============================================================ + v.basicInfo(); + if (s.needsService(v)) { + System.out.println(v.getBrand() + " " + v.getModel()); + System.out.print("Performing service: "); + s.performService(); + } else { + System.out.println(v.getBrand() + " " + v.getModel() + " does NOT need service"); + } + System.out.println(); + } - - - // ============================================================ - // 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("=== Workshop Completed ==="); diff --git a/src/main/java/org/model/Car.java b/src/main/java/org/model/Car.java index 74836b5..d0b1d40 100644 --- a/src/main/java/org/model/Car.java +++ b/src/main/java/org/model/Car.java @@ -11,31 +11,44 @@ package org.model; import org.behavior.Serviceable; +import java.lang.classfile.Superclass; + public abstract class Car extends Vehicle implements Serviceable { private int seats; - public Car(String brand, String model, int year, - double mileage, int seats) { - - // TODO: Call super constructor + public Car(String brand, String model, int year, double mileage, int seats) { + super(brand, model, year, mileage); this.seats = seats; } - // TODO: Override getEnergyType() → return "Unknown" + @Override + public String getEnergyType(){ + return "Unknown"; + } - // TODO: Override showDetails() - // Print brand, model, year, mileage, and number of seats + @Override + public void showDetails(){ + System.out.print( + getYear() + " " + getBrand() + " " + getModel() + + " | Mileage: " + getMileage() + + " | Energy: " + getEnergyType() + + " | Seats: " + seats + ); + System.out.println(); + } + + public boolean needsService(Vehicle v) { + if(getMileage() > 10000) { + return true; + } + else { + return false; + } + } + + public void performService() { + System.out.println("General car service completed"); + } - /* - * Implement Serviceable methods: - * - * needsService(Vehicle v): - * - Check the mileage of the current car - * - If mileage > 10000 return true - * - Otherwise return false - * - * performService(): - * - Print: "General car service completed" - */ } diff --git a/src/main/java/org/model/ElectricCar.java b/src/main/java/org/model/ElectricCar.java index 2ee9bd8..21b74f3 100644 --- a/src/main/java/org/model/ElectricCar.java +++ b/src/main/java/org/model/ElectricCar.java @@ -26,14 +26,49 @@ public class ElectricCar extends Car implements Chargeable { this.batteryCapacity = batteryCapacity; this.chargeLevel = chargeLevel; } + + @Override + public String getEnergyType() { + return "Electric"; + } - // TODO: Override getEnergyType() → return "Electric" + @Override + public void showDetails() { + super.showDetails(); + System.out.print( + " | battery Capacity: " + batteryCapacity + + " | charge Level: " + chargeLevel + ); + System.out.println(); + } + @Override + public void charge(double amount) { + if (amount <= 0) { + System.out.println("Error"); + } - // TODO: Override showDetails() - // Call super.showDetails() - // Print battery capacity and charge level + if (this.chargeLevel + amount > 100) { + this.chargeLevel = 100; + } - // TODO: Implement charge(double amount) - // Validate negative - // Cap at 100 + else { + this.chargeLevel += amount; + } + } + + @Override + public boolean needsService(Vehicle v) { + if(getMileage() > 8000) { + return true; + } + else { + return false; + } + } + + @Override + public void performService() { + System.out.println("Battery system checked"); + this.chargeLevel = 100; + } } diff --git a/src/main/java/org/model/GasCar.java b/src/main/java/org/model/GasCar.java index 2109f93..2e21096 100644 --- a/src/main/java/org/model/GasCar.java +++ b/src/main/java/org/model/GasCar.java @@ -27,13 +27,47 @@ public class GasCar extends Car implements Refuelable { this.fuelType = fuelType; } - // TODO: Override getEnergyType() → return fuelType + " (Gas)" + @Override + public String getEnergyType() { + return fuelType + " (Gas)"; + } - // TODO: Implement refuel(double amount) - // Validate amount - // Cap at 100 + public void refuel(double amount) { + if (amount <= 0) { + System.out.println("Error"); + return; + } - // TODO: Override showDetails() - // Call super.showDetails() - // Print fuel level and fuel type + if (this.fuelLevel + amount > 100) { + this.fuelLevel = 100; + } else { + this.fuelLevel += amount; + } + } + + @Override + public void showDetails() { + super.showDetails(); + System.out.print( + " | fuel type: " + fuelType + + " | fuel level: " + fuelLevel + ); + System.out.println(); + } + + @Override + public boolean needsService(Vehicle v) { + if(getMileage() > 12000) { + return true; + } + else { + return false; + } + } + + @Override + public void performService() { + System.out.println("Engine oil changed"); + this.fuelLevel = 100; + } } diff --git a/src/main/java/org/model/SportsCar.java b/src/main/java/org/model/SportsCar.java index 25f5df9..b1f5956 100644 --- a/src/main/java/org/model/SportsCar.java +++ b/src/main/java/org/model/SportsCar.java @@ -23,8 +23,27 @@ public class SportsCar extends GasCar { this.zeroToHundred = zeroToHundred; } + @Override + public void showDetails() { + super.showDetails(); + System.out.print( + " | acceleration time: " + this.zeroToHundred + ); + System.out.println(); + } - // TODO: Override showDetails() - // Call super.showDetails() - // Print acceleration time + @Override + public boolean needsService(Vehicle v) { + if(getMileage() > 5000) { + return true; + } + else { + return false; + } + } + + @Override + public void performService() { + System.out.println("High-performance brake system checked"); + } } diff --git a/src/main/java/org/model/Vehicle.java b/src/main/java/org/model/Vehicle.java index 70615a9..6c6a0ad 100644 --- a/src/main/java/org/model/Vehicle.java +++ b/src/main/java/org/model/Vehicle.java @@ -20,13 +20,36 @@ public abstract class Vehicle { this.mileage = mileage; } - // TODO: Create getters for all fields + public String getBrand() { - // TODO: Implement addMileage(double km) - // If km is negative → print error - // Otherwise increase mileage + return brand; + } - // TODO: Declare abstract method getEnergyType() + public String getModel() { + + return model; + } + + public int getYear() { + + return year; + } + + public double getMileage() { + + return mileage; + } + + + public void addMileage(double km) { + if ( km < 0 ) { + System.out.println("Error: Mileage cannot be negative!"); + return; + } + this.mileage += km; + } + + public abstract String getEnergyType(); public void basicInfo() { System.out.println( diff --git a/src/main/java/org/util/VehicleInspector.java b/src/main/java/org/util/VehicleInspector.java index c83c420..c6b3292 100644 --- a/src/main/java/org/util/VehicleInspector.java +++ b/src/main/java/org/util/VehicleInspector.java @@ -2,7 +2,9 @@ package org.util; import org.*; import org.model.Vehicle; - +import org.model.ElectricCar; +import org.model.SportsCar; +import org.model.GasCar; /* * Utility class to inspect vehicles. * Demonstrates instanceof and polymorphism. @@ -14,9 +16,14 @@ public class VehicleInspector { v.basicInfo(); - // TODO: - // If ElectricCar → print "Check battery system" - // If SportsCar → print "Check brakes and performance" - // If GasCar → print "Check engine and oil" + if (v instanceof SportsCar) { + System.out.println("→ Check brakes and performance"); + } + else if (v instanceof ElectricCar) { + System.out.println("→ Check battery system"); + } + else if (v instanceof GasCar) { + System.out.println("→ Check engine and oil"); + } } }