From 15254cdfa3f4545b4175b61ece168d6b1a657c6b Mon Sep 17 00:00:00 2001 From: Reza Date: Tue, 28 Apr 2026 23:01:10 +0330 Subject: [PATCH] complete --- src/main/java/org/Main.java | 169 ++++++------------- src/main/java/org/model/Car.java | 50 +++--- src/main/java/org/model/ElectricCar.java | 53 +++--- src/main/java/org/model/GasCar.java | 52 +++--- src/main/java/org/model/HybridCar.java | 62 +++++++ src/main/java/org/model/SportsCar.java | 31 ++-- src/main/java/org/model/Vehicle.java | 31 ++-- src/main/java/org/util/VehicleInspector.java | 30 ++-- 8 files changed, 247 insertions(+), 231 deletions(-) create mode 100644 src/main/java/org/model/HybridCar.java diff --git a/src/main/java/org/Main.java b/src/main/java/org/Main.java index 7a3d8c8..d26b323 100644 --- a/src/main/java/org/Main.java +++ b/src/main/java/org/Main.java @@ -1,126 +1,55 @@ package org; +import org.behavior.Serviceable; +import org.model.ElectricCar; +import org.model.GasCar; +import org.model.HybridCar; +import org.model.SportsCar; +import org.model.Vehicle; +import org.util.VehicleInspector; + public class Main { public static void main(String[] args) { + ElectricCar tesla = new ElectricCar( + "Tesla", "Model 3", 2022, 9000, + 5, 75, 60 + ); + GasCar toyota = new GasCar( + "Toyota", "Corolla", 2018, 15000, + 5, 40, "Petrol" + ); + SportsCar ferrari = new SportsCar( + "Ferrari", "F8", 2021, 7000, + 2, 50, "Petrol", 2.9 + ); + HybridCar prius = new HybridCar( + "Toyota", "Prius", 2020, 11000, + 5, 45, 70 + ); + Vehicle[] vehicles = {tesla, toyota, ferrari, prius}; + System.out.println("=== Basic Info ==="); + for (Vehicle v : vehicles) { + v.basicInfo(); + System.out.println(); + } + System.out.println("=== Vehicle Inspection ==="); + for (Vehicle v : vehicles) { + VehicleInspector.inspect(v); + System.out.println(); + } + Serviceable[] serviceables = {tesla, toyota, ferrari, prius}; + System.out.println("=== Service Check ==="); + 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() + " does not need service"); + } + System.out.println(); + } - // ============================================================ - // 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 ==="); - // - // ============================================================ - - - - - + System.out.println("=== Workshop Completed ==="); } -} +} \ No newline at end of file diff --git a/src/main/java/org/model/Car.java b/src/main/java/org/model/Car.java index 74836b5..51e0e6f 100644 --- a/src/main/java/org/model/Car.java +++ b/src/main/java/org/model/Car.java @@ -1,13 +1,5 @@ package org.model; -/* - * Car extends Vehicle. - * This class SHOULD implement Serviceable. - * - * Service logic for Car: - * - needsService(): return true if mileage > 10,000 - * - performService(): print "General car service completed" - */ import org.behavior.Serviceable; @@ -17,25 +9,31 @@ public abstract class Car extends Vehicle implements Serviceable { public Car(String brand, String model, int year, double mileage, int seats) { - - // TODO: Call super constructor + super(brand, model, year, mileage); this.seats = seats; } + public int getSeats() { + return seats; + } - // TODO: Override getEnergyType() → return "Unknown" - - // TODO: Override showDetails() - // Print brand, model, year, mileage, and number of seats - - /* - * 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" - */ + @Override + public String getEnergyType() { + return "Unknown"; + } + @Override + public void showDetails() { + System.out.println("Brand: " + getBrand()); + System.out.println("Model: " + getModel()); + System.out.println("Year: " + getYear()); + System.out.println("Mileage: " + getMileage()); + System.out.println("Seats: " + seats); + } + @Override + public boolean needsService(Vehicle v) { + return v.getMileage() > 10000; + } + @Override + public void performService() { + System.out.println("General car service completed"); + } } diff --git a/src/main/java/org/model/ElectricCar.java b/src/main/java/org/model/ElectricCar.java index 2ee9bd8..da127a9 100644 --- a/src/main/java/org/model/ElectricCar.java +++ b/src/main/java/org/model/ElectricCar.java @@ -1,39 +1,48 @@ package org.model; -/* - * ElectricCar extends Car. - * It already inherits Serviceable from Car. - * - * Service logic for ElectricCar: - * - needsService(): return true if mileage > 8000 - * - performService(): - * Print "Battery system checked" - * Reset chargeLevel to 100 - */ - import org.behavior.Chargeable; public class ElectricCar extends Car implements Chargeable { - private int batteryCapacity; private double chargeLevel; public ElectricCar(String brand, String model, int year, double mileage, int seats, int batteryCapacity, double chargeLevel) { - super(brand, model, year, mileage, seats); this.batteryCapacity = batteryCapacity; this.chargeLevel = chargeLevel; } - // TODO: Override getEnergyType() → return "Electric" + @Override + public String getEnergyType() { + return "Electric"; + } + @Override + public void showDetails() { + super.showDetails(); + System.out.println("Battery Capacity: " + batteryCapacity); + System.out.println("Charge Level: " + chargeLevel); + } + @Override + public void charge(double amount) { + if (amount < 0) { + System.out.println("Charge amount cannot be negative"); + return; + } + chargeLevel += amount; - // TODO: Override showDetails() - // Call super.showDetails() - // Print battery capacity and charge level - - // TODO: Implement charge(double amount) - // Validate negative - // Cap at 100 -} + if (chargeLevel > 100) { + chargeLevel = 100; + } + } + @Override + public boolean needsService(Vehicle v) { + return v.getMileage() > 8000; + } + @Override + public void performService() { + System.out.println("Battery system checked"); + chargeLevel = 100; + } +} \ No newline at end of file diff --git a/src/main/java/org/model/GasCar.java b/src/main/java/org/model/GasCar.java index 2109f93..896e5a2 100644 --- a/src/main/java/org/model/GasCar.java +++ b/src/main/java/org/model/GasCar.java @@ -1,16 +1,4 @@ package org.model; - -/* - * GasCar extends Car. - * Inherits Serviceable from Car. - * - * Service logic for GasCar: - * - needsService(): return true if mileage > 12000 - * - performService(): - * Print "Engine oil changed" - * Reset fuelLevel to 100 - */ - import org.behavior.Refuelable; public class GasCar extends Car implements Refuelable { @@ -27,13 +15,35 @@ public class GasCar extends Car implements Refuelable { this.fuelType = fuelType; } - // TODO: Override getEnergyType() → return fuelType + " (Gas)" + @Override + public String getEnergyType() { + return fuelType + " (Gas)"; + } + @Override + public void refuel(double amount) { + if (amount < 0) { + System.out.println("Fuel amount cannot be negative"); + return; + } + fuelLevel += amount; - // TODO: Implement refuel(double amount) - // Validate amount - // Cap at 100 - - // TODO: Override showDetails() - // Call super.showDetails() - // Print fuel level and fuel type -} + if (fuelLevel > 100) { + fuelLevel = 100; + } + } + @Override + public void showDetails() { + super.showDetails(); + System.out.println("Fuel Level: " + fuelLevel); + System.out.println("Fuel Type: " + fuelType); + } + @Override + public boolean needsService(Vehicle v) { + return v.getMileage() > 12000; + } + @Override + public void performService() { + System.out.println("Engine oil changed"); + fuelLevel = 100; + } +} \ No newline at end of file diff --git a/src/main/java/org/model/HybridCar.java b/src/main/java/org/model/HybridCar.java new file mode 100644 index 0000000..a6e78c0 --- /dev/null +++ b/src/main/java/org/model/HybridCar.java @@ -0,0 +1,62 @@ +package org.model; + +import org.behavior.Chargeable; +import org.behavior.Refuelable; + +public class HybridCar extends Car implements Refuelable, Chargeable { + private double fuelLevel; + private double chargeLevel; + + public HybridCar(String brand, String model, int year, + double mileage, int seats, + double fuelLevel, double chargeLevel) { + super(brand, model, year, mileage, seats); + this.fuelLevel = fuelLevel; + this.chargeLevel = chargeLevel; + } + + @Override + public String getEnergyType() { + return "Hybrid"; + } + @Override + public void refuel(double amount) { + if (amount < 0) { + System.out.println("Fuel amount cannot be negative"); + return; + } + fuelLevel += amount; + + if (fuelLevel > 100) { + fuelLevel = 100; + } + } + @Override + public void charge(double amount) { + if (amount < 0) { + System.out.println("Charge amount cannot be negative"); + return; + } + chargeLevel += amount; + + if (chargeLevel > 100) { + chargeLevel = 100; + } + } + @Override + public boolean needsService(Vehicle v) { + return v.getMileage() > 10000; + } + @Override + public void performService() { + System.out.println("Engine oil changed and battery system checked"); + fuelLevel = 100; + chargeLevel = 100; + } + @Override + public void showDetails() { + super.showDetails(); + System.out.println("Fuel Level: " + fuelLevel); + System.out.println("Charge Level: " + chargeLevel); + } +} \ No newline at end of file diff --git a/src/main/java/org/model/SportsCar.java b/src/main/java/org/model/SportsCar.java index 25f5df9..d59ea1d 100644 --- a/src/main/java/org/model/SportsCar.java +++ b/src/main/java/org/model/SportsCar.java @@ -1,30 +1,27 @@ package org.model; -/* - * SportsCar extends GasCar. - * - * Special service logic for SportsCar: - * - needsService(): return true if mileage > 5000 - * - performService(): - * Print "High-performance brake system checked" - */ - public class SportsCar extends GasCar { - private double zeroToHundred; public SportsCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType, double zeroToHundred) { - super(brand, model, year, mileage, seats, fuelLevel, fuelType); - this.zeroToHundred = zeroToHundred; } - - // TODO: Override showDetails() - // Call super.showDetails() - // Print acceleration time -} + @Override + public void showDetails() { + super.showDetails(); + System.out.println("0-100 Time: " + zeroToHundred + " seconds"); + } + @Override + public boolean needsService(Vehicle v) { + return v.getMileage() > 5000; + } + @Override + public void performService() { + System.out.println("High-performance brake system checked"); + } +} \ No newline at end of file diff --git a/src/main/java/org/model/Vehicle.java b/src/main/java/org/model/Vehicle.java index 70615a9..ff773c6 100644 --- a/src/main/java/org/model/Vehicle.java +++ b/src/main/java/org/model/Vehicle.java @@ -1,11 +1,5 @@ package org.model; -/* - * Abstract base class for all vehicles. - * This class DOES NOT implement Serviceable. - * Service behavior will be defined in concrete subclasses. - */ - public abstract class Vehicle { private String brand; @@ -20,13 +14,28 @@ public abstract class Vehicle { this.mileage = mileage; } - // TODO: Create getters for all fields + public String getBrand() { + return brand; + } + public String getModel() { + return model; + } + public int getYear() { + return year; + } + public double getMileage() { + return mileage; + } - // TODO: Implement addMileage(double km) - // If km is negative → print error - // Otherwise increase mileage + public void addMileage(double km) { + if (km < 0) { + System.out.println("Mileage cannot be negative"); + } else { + mileage += km; + } + } - // TODO: Declare abstract method getEnergyType() + 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..06e8533 100644 --- a/src/main/java/org/util/VehicleInspector.java +++ b/src/main/java/org/util/VehicleInspector.java @@ -1,22 +1,24 @@ package org.util; -import org.*; +import org.model.ElectricCar; +import org.model.GasCar; +import org.model.HybridCar; +import org.model.SportsCar; import org.model.Vehicle; -/* - * Utility class to inspect vehicles. - * Demonstrates instanceof and polymorphism. - */ - public class VehicleInspector { - public static void inspect(Vehicle v) { - 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 ElectricCar) { + System.out.println("Check battery system"); + } else if (v instanceof SportsCar) { + System.out.println("Check brakes and performance"); + } else if (v instanceof HybridCar) { + System.out.println("Check engine oil and battery system"); + } else if (v instanceof GasCar) { + System.out.println("Check engine and oil"); + } else { + System.out.println("General vehicle inspection"); + } } -} +} \ No newline at end of file