implement HybridCar class and add inspect for it in VehicleInspector class
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user