complete all TODO sections in ElectricCar class

This commit is contained in:
2026-04-28 10:51:59 +03:30
parent 26e6d9b82d
commit 46939f6f38
2 changed files with 40 additions and 19 deletions
+38 -17
View File
@@ -1,15 +1,5 @@
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;
@@ -27,13 +17,44 @@ public class ElectricCar extends Car implements Chargeable {
this.chargeLevel = chargeLevel;
}
// TODO: Override getEnergyType() → return "Electric"
public int getBatteryCapacity() { return this.batteryCapacity; }
public void setBatteryCapacity(int batteryCapacity) { this.batteryCapacity = batteryCapacity; }
public double getChargeLevel() { return this.chargeLevel; }
public void setChargeLevel(double chargeLevel) { this.chargeLevel = chargeLevel; }
// TODO: Override showDetails()
// Call super.showDetails()
// Print battery capacity and charge level
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 100
@Override
public String getEnergyType() {
return "Electric";
}
@Override
public void showDetails() {
super.showDetails();
System.out.println(
"battery capacity: " + getBatteryCapacity() + "\n"
+ " charge level: " + getChargeLevel());
}
@Override
public void charge(double amount) {
if (amount < 0) {
throw new IllegalArgumentException("Amount must be positive");
}
if ( getChargeLevel() + amount > 100 ) {
setChargeLevel(100);
}
setChargeLevel(getChargeLevel() + amount);
}
@Override
public boolean needsService(Vehicle v) {
return getMileage() > 8000;
}
@Override
public void performService() {
System.out.println("Battery system checked");
setChargeLevel(100);
}
}
+2 -2
View File
@@ -41,8 +41,8 @@ public class GasCar extends Car implements Refuelable {
public void showDetails() {
super.showDetails();
System.out.println(
"fuel level: " + this.fuelLevel + "\n"
+ " fuel type: " + this.fuelType);
"fuel level: " + getFuelLevel() + "\n"
+ " fuel type: " + getFuelType());
}
@Override