72 lines
1.8 KiB
Java
72 lines
1.8 KiB
Java
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";
|
|
}
|
|
|
|
// TODO: Override showDetails()
|
|
// Call super.showDetails()
|
|
// Print battery capacity and charge level
|
|
|
|
@Override
|
|
public void showDetails() {
|
|
super.showDetails();
|
|
System.out.println("| battery capacity: " + batteryCapacity + " | charge level: " + chargeLevel);
|
|
}
|
|
|
|
// TODO: Implement charge(double amount)
|
|
// Validate negative
|
|
// Cap at 100
|
|
|
|
public void charge(double amount){
|
|
if(amount < 0)
|
|
System.out.println("amount unacceptable");
|
|
else{
|
|
if (chargeLevel + amount > 100)
|
|
chargeLevel = 100;
|
|
else
|
|
chargeLevel += amount;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void performService() {
|
|
super.performService();
|
|
System.out.println("Electric engine and oil checked");
|
|
this.chargeLevel = 100;
|
|
}
|
|
|
|
@Override
|
|
public boolean needsService(Vehicle v) {
|
|
return getMileage() > 10000;
|
|
}
|
|
}
|