Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1d6560876 | ||
|
|
c65c28290b | ||
|
|
54f2e13683 | ||
|
|
01a3340431 | ||
|
|
16f4df1165 | ||
|
|
e9168db124 | ||
|
|
3d38464d34 | ||
|
|
543ea063a7 | ||
|
|
86e0ce218a | ||
|
|
6ea0a1f5bd |
+49
-10
@@ -1,8 +1,30 @@
|
|||||||
package org;
|
package org;
|
||||||
|
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
|
import org.model.Vehicle;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
ElectricCar tesla = new ElectricCar(
|
||||||
|
"Tesla", "Model S", 2022, 1000,
|
||||||
|
100, 550, 50 // batteryCapacity, range
|
||||||
|
);
|
||||||
|
|
||||||
|
GasCar toyota = new GasCar(
|
||||||
|
"Toyota", "Corolla", 2018, 85000,
|
||||||
|
5, 50, "Petrol" // seats, fuelCapacity, fuelType
|
||||||
|
);
|
||||||
|
|
||||||
|
SportsCar ferrari = new SportsCar(
|
||||||
|
"Ferrari", "F8 Tributo", 2021, 12000,
|
||||||
|
2, 3.9, "oil", 710 // seats, engineSize, horsepower
|
||||||
|
);
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 1:
|
// TODO 1:
|
||||||
// Create one instance of each vehicle type:
|
// Create one instance of each vehicle type:
|
||||||
@@ -18,9 +40,10 @@ public class Main {
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
|
ArrayList<Vehicle> vehicles = new ArrayList<>();
|
||||||
|
vehicles.add(tesla);
|
||||||
|
vehicles.add(toyota);
|
||||||
|
vehicles.add(ferrari);
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 2:
|
// TODO 2:
|
||||||
// Create an array of Vehicle:
|
// Create an array of Vehicle:
|
||||||
@@ -35,7 +58,10 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (Vehicle v: vehicles) {
|
||||||
|
v.basicInfo();
|
||||||
|
System.out.print("\n");
|
||||||
|
}
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 3:
|
// TODO 3:
|
||||||
// Loop through the Vehicle[] array and:
|
// Loop through the Vehicle[] array and:
|
||||||
@@ -50,7 +76,9 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (Vehicle v: vehicles) {
|
||||||
|
VehicleInspector.inspect(v);
|
||||||
|
}
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 4:
|
// TODO 4:
|
||||||
// Use VehicleInspector to inspect each vehicle:
|
// Use VehicleInspector to inspect each vehicle:
|
||||||
@@ -65,7 +93,10 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ArrayList<Serviceable> serviceables= new ArrayList<>();
|
||||||
|
serviceables.add(tesla);
|
||||||
|
serviceables.add(toyota);
|
||||||
|
serviceables.add(ferrari);
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 5:
|
// TODO 5:
|
||||||
// Create an array of Serviceable:
|
// Create an array of Serviceable:
|
||||||
@@ -83,7 +114,17 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (Serviceable s: serviceables) {
|
||||||
|
Vehicle v = (Vehicle) s;
|
||||||
|
if(s.needsService(v)) {
|
||||||
|
System.out.println("Brand: " + v.getBrand() +", model: " +
|
||||||
|
v.getModel());
|
||||||
|
s.performService();
|
||||||
|
} else {
|
||||||
|
System.out.println("The car does not need service");
|
||||||
|
}
|
||||||
|
System.out.print("\n");
|
||||||
|
}
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 6:
|
// TODO 6:
|
||||||
// Loop through the Serviceable[] array:
|
// Loop through the Serviceable[] array:
|
||||||
@@ -107,9 +148,7 @@ public class Main {
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("=== Workshop Completed ===");
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 7 (Optional):
|
// TODO 7 (Optional):
|
||||||
// After everything is done, print a final message:
|
// After everything is done, print a final message:
|
||||||
|
|||||||
@@ -17,16 +17,29 @@ public abstract class Car extends Vehicle implements Serviceable {
|
|||||||
|
|
||||||
public Car(String brand, String model, int year,
|
public Car(String brand, String model, int year,
|
||||||
double mileage, int seats) {
|
double mileage, int seats) {
|
||||||
|
super(brand, model, year, mileage);
|
||||||
// TODO: Call super constructor
|
// TODO: Call super constructor
|
||||||
this.seats = seats;
|
this.seats = seats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
// TODO: Override getEnergyType() → return "Unknown"
|
// TODO: Override getEnergyType() → return "Unknown"
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
System.out.println("Car details: brand: " + this.getBrand() + ", model: "
|
||||||
|
+ this.getModel() + ", year: " + this.getYear() + ", mileage: "
|
||||||
|
+ this.getMileage() + ", number of seats: " + this.seats);
|
||||||
|
}
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Print brand, model, year, mileage, and number of seats
|
// Print brand, model, year, mileage, and number of seats
|
||||||
|
|
||||||
|
public boolean needService(Vehicle v) {
|
||||||
|
return v.getMileage() > 10000;
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
* Implement Serviceable methods:
|
* Implement Serviceable methods:
|
||||||
*
|
*
|
||||||
@@ -34,8 +47,11 @@ public abstract class Car extends Vehicle implements Serviceable {
|
|||||||
* - Check the mileage of the current car
|
* - Check the mileage of the current car
|
||||||
* - If mileage > 10000 return true
|
* - If mileage > 10000 return true
|
||||||
* - Otherwise return false
|
* - Otherwise return false
|
||||||
*
|
*/
|
||||||
* performService():
|
public void performService() {
|
||||||
|
System.out.println("General car service completed");
|
||||||
|
}
|
||||||
|
/* performService():
|
||||||
* - Print: "General car service completed"
|
* - Print: "General car service completed"
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,13 +26,36 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
this.batteryCapacity = batteryCapacity;
|
this.batteryCapacity = batteryCapacity;
|
||||||
this.chargeLevel = chargeLevel;
|
this.chargeLevel = chargeLevel;
|
||||||
}
|
}
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return v.getMileage() > 8000;
|
||||||
|
}
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
this.chargeLevel = 100;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return "Electric";
|
||||||
|
}
|
||||||
// TODO: Override getEnergyType() → return "Electric"
|
// TODO: Override getEnergyType() → return "Electric"
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println("Battery capacity: " + this.batteryCapacity + ", Charge level: " +
|
||||||
|
this.chargeLevel);
|
||||||
|
}
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Call super.showDetails()
|
// Call super.showDetails()
|
||||||
// Print battery capacity and charge level
|
// Print battery capacity and charge level
|
||||||
|
|
||||||
|
public void charge(double amount) {
|
||||||
|
if (amount > 0 && this.chargeLevel + amount <= 100) {
|
||||||
|
this.chargeLevel += amount;
|
||||||
|
} else if (amount > 0 && this.chargeLevel + amount > 100) {
|
||||||
|
this.chargeLevel = 100; //if input amount causes the battery to charge pass 100%, we set it to 100%.
|
||||||
|
}
|
||||||
|
}
|
||||||
// TODO: Implement charge(double amount)
|
// TODO: Implement charge(double amount)
|
||||||
// Validate negative
|
// Validate negative
|
||||||
// Cap at 100
|
// Cap at 100
|
||||||
|
|||||||
@@ -27,12 +27,37 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
this.fuelType = fuelType;
|
this.fuelType = fuelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return v.getMileage() > 8000;
|
||||||
|
}
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
this.fuelLevel = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return this.fuelType + " (Gas)";
|
||||||
|
}
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||||
|
|
||||||
|
|
||||||
|
public void refuel(double amount) {
|
||||||
|
if (amount > 0 && this.fuelLevel + amount <= 100) {
|
||||||
|
this.fuelLevel += amount;
|
||||||
|
} else if (amount > 0 && this.fuelLevel + amount > 100) {
|
||||||
|
this.fuelLevel = 100; //if input amount causes the fuel to fill pass 100%, we set it to 100%.
|
||||||
|
}
|
||||||
|
}
|
||||||
// TODO: Implement refuel(double amount)
|
// TODO: Implement refuel(double amount)
|
||||||
// Validate amount
|
// Validate amount
|
||||||
// Cap at 100
|
// Cap at 100
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println("Feul level: " + this.fuelLevel + ", Feul type: " + this.fuelType);
|
||||||
|
}
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Call super.showDetails()
|
// Call super.showDetails()
|
||||||
// Print fuel level and fuel type
|
// Print fuel level and fuel type
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package org.model;
|
||||||
|
|
||||||
|
import org.behavior.Chargeable;
|
||||||
|
import org.behavior.Refuelable;
|
||||||
|
|
||||||
|
public class HybridCar extends Car implements Chargeable, Refuelable {
|
||||||
|
private int batteryCapacity;
|
||||||
|
private double chargeLevel;
|
||||||
|
private double fuelLevel;
|
||||||
|
private String fuelType;
|
||||||
|
|
||||||
|
public HybridCar(String brand, String model, int year, double mileage,
|
||||||
|
int seats, int batteryCapacity, double chargeLevel,
|
||||||
|
double fuelLevel, String fuelType) {
|
||||||
|
super(brand, model, year, mileage, seats);
|
||||||
|
this.batteryCapacity = batteryCapacity;
|
||||||
|
this.chargeLevel = chargeLevel;
|
||||||
|
this.fuelLevel = fuelLevel;
|
||||||
|
this.fuelType = fuelType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return v.getMileage() > 10000;
|
||||||
|
}
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed and battery system checked");
|
||||||
|
this.chargeLevel = 100;
|
||||||
|
this.fuelLevel = 100;
|
||||||
|
}
|
||||||
|
public void charge(double amount) {
|
||||||
|
if (amount > 0 && this.chargeLevel + amount <= 100) {
|
||||||
|
this.chargeLevel += amount;
|
||||||
|
} else if (amount > 0 && this.chargeLevel + amount > 100) {
|
||||||
|
this.chargeLevel = 100; //if input amount causes the battery to charge pass 100%, we set it to 100%.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void refuel(double amount) {
|
||||||
|
if (amount > 0 && this.fuelLevel + amount <= 100) {
|
||||||
|
this.fuelLevel += amount;
|
||||||
|
} else if (amount > 0 && this.fuelLevel + amount > 100) {
|
||||||
|
this.fuelLevel = 100; //if input amount causes the fuel to fill pass 100%, we set it to 100%.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,7 +23,17 @@ public class SportsCar extends GasCar {
|
|||||||
|
|
||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return v.getMileage() > 5000;
|
||||||
|
}
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println("Acceleration time: " + this.zeroToHundred);
|
||||||
|
}
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Call super.showDetails()
|
// Call super.showDetails()
|
||||||
// Print acceleration time
|
// Print acceleration time
|
||||||
|
|||||||
@@ -20,14 +20,37 @@ public abstract class Vehicle {
|
|||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getBrand() {
|
||||||
|
return brand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getYear() {
|
||||||
|
return year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMileage() {
|
||||||
|
return mileage;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Create getters for all fields
|
// TODO: Create getters for all fields
|
||||||
|
|
||||||
|
public void addMileage(double km) {
|
||||||
|
if (km < 0) {
|
||||||
|
System.out.println("error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.mileage += km;
|
||||||
|
}
|
||||||
// TODO: Implement addMileage(double km)
|
// TODO: Implement addMileage(double km)
|
||||||
// If km is negative → print error
|
// If km is negative → print error
|
||||||
// Otherwise increase mileage
|
// Otherwise increase mileage
|
||||||
|
|
||||||
|
public abstract String getEnergyType();
|
||||||
// TODO: Declare abstract method getEnergyType()
|
// TODO: Declare abstract method getEnergyType()
|
||||||
|
|
||||||
public void basicInfo() {
|
public void basicInfo() {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
year + " " + brand + " " + model +
|
year + " " + brand + " " + model +
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package org.util;
|
package org.util;
|
||||||
|
|
||||||
import org.*;
|
import org.*;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
import org.model.Vehicle;
|
import org.model.Vehicle;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -14,6 +17,13 @@ public class VehicleInspector {
|
|||||||
|
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
|
||||||
|
if (v instanceof ElectricCar) {
|
||||||
|
System.out.println("Check battery system");
|
||||||
|
} else if (v instanceof SportsCar) {
|
||||||
|
System.out.println("Check brakes and performance");
|
||||||
|
} else if (v instanceof GasCar) {
|
||||||
|
System.out.println("Check engine and oil");
|
||||||
|
}
|
||||||
// TODO:
|
// TODO:
|
||||||
// If ElectricCar → print "Check battery system"
|
// If ElectricCar → print "Check battery system"
|
||||||
// If SportsCar → print "Check brakes and performance"
|
// If SportsCar → print "Check brakes and performance"
|
||||||
|
|||||||
Reference in New Issue
Block a user