complete all TODO sections in ElectricCar class
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user