implement HybridCar class and add inspect for it in VehicleInspector class

This commit is contained in:
2026-04-28 11:32:12 +03:30
parent 13e41f5f21
commit 4cff34b20a
5 changed files with 64 additions and 10 deletions
+2 -1
View File
@@ -1,5 +1,7 @@
package org;
import org.model.HybridCar;
public class Main {
public static void main(String[] args) {
@@ -20,7 +22,6 @@ public class Main {
// ============================================================
// TODO 2:
// Create an array of Vehicle:
-8
View File
@@ -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;
+57
View File
@@ -0,0 +1,57 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Refuelable, Chargeable {
private double batteryLevel;
private double fuelLevel;
public HybridCar(String brand, String model, int year,
double mileage, int seats, double batteryLevel, double fuelLevel) {
super(brand, model, year, mileage, seats);
this.batteryLevel = batteryLevel;
this.fuelLevel = fuelLevel;
}
@Override
public String getEnergyType() {
return "Hybrid";
}
@Override
public void showDetails() {
super.showDetails();
System.out.println(
"Battery Level: " + this.batteryLevel + "\n"
+ "Fuel Level: " + this.fuelLevel
);
}
@Override
public void charge(double amount) {
if (amount < 0) {
throw new IllegalArgumentException("Amount must be positive");
}
if (this.batteryLevel + amount > 100) {this.batteryLevel = 100;}
this.batteryLevel += amount;
}
@Override
public void refuel(double amount) {
if (amount < 0) {
throw new IllegalArgumentException("Amount must be positive");
}
if (this.fuelLevel + amount > 100) {this.fuelLevel = 100;}
this.fuelLevel += amount;
}
@Override
public void performService() {
System.out.println("Engine oil changed and batter system checked");
this.batteryLevel = 100;
this.fuelLevel = 100;
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred;
}
@Override
public void showDetails() {
super.showDetails();
@@ -3,6 +3,7 @@ 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;
@@ -23,5 +24,8 @@ public class VehicleInspector {
else if (v instanceof GasCar) {
System.out.println("Check engine and oil");
}
else if (v instanceof HybridCar) {
System.out.println("Check engine and oil and battery service");
}
}
}