Compare commits
1
Commits
6cb8f9a8d8
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6296160060 |
@@ -1,5 +1,12 @@
|
||||
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;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -18,6 +25,12 @@ public class Main {
|
||||
// ============================================================
|
||||
|
||||
|
||||
ElectricCar tesla = new ElectricCar("Tesla", "Model S", 2022, 1000, 100, 550, 50);
|
||||
|
||||
GasCar toyota = new GasCar("Toyota", "Corolla", 2018, 85000, 5, 50, "Petrol");
|
||||
|
||||
SportsCar ferrari = new SportsCar("Ferrari", "F8 Tributo", 2021, 12000, 2, 3.9, "oil", 710);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -32,6 +45,8 @@ public class Main {
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
Vehicle[] vehicles = {tesla, toyota, ferrari};
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -47,7 +62,11 @@ public class Main {
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
System.out.println();
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,6 +81,10 @@ public class Main {
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
VehicleInspector.inspect(tesla);
|
||||
VehicleInspector.inspect(toyota);
|
||||
VehicleInspector.inspect(ferrari);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -80,6 +103,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
Serviceable[] serviceables = {tesla, toyota, ferrari};
|
||||
|
||||
|
||||
|
||||
@@ -106,6 +130,18 @@ public class Main {
|
||||
// Add blank lines between each item for readability.
|
||||
// ============================================================
|
||||
|
||||
for (Serviceable s : serviceables) {
|
||||
Vehicle v = (Vehicle) s;
|
||||
|
||||
if (s.needsService(v)) {
|
||||
System.out.println("brand: " + v.getBrand() + "\nmodel: " + v.getModel());
|
||||
s.performService();
|
||||
}
|
||||
else {
|
||||
System.out.println("the car does not need service!");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -118,6 +154,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -15,17 +15,27 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
|
||||
private int seats;
|
||||
|
||||
public Car(String brand, String model, int year,
|
||||
double mileage, int seats) {
|
||||
public Car(String brand, String model, int year, double mileage, int seats) {
|
||||
|
||||
// TODO: Call super constructor
|
||||
super(brand, model, year, mileage);
|
||||
this.seats = seats;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return "Unknown"
|
||||
public String getEnergyType() { return "Unknown";}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Print brand, model, year, mileage, and number of seats
|
||||
public void showDetails() {
|
||||
|
||||
System.out.println("brand: " + getBrand());
|
||||
System.out.println("model: " + getModel());
|
||||
System.out.println("year: " + getYear());
|
||||
System.out.println("mileage: " + getMileage());
|
||||
System.out.println("number of sets: " + seats);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
@@ -38,4 +48,16 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
|
||||
public boolean needsService(Vehicle v) {
|
||||
if (v.getMileage() > 10000) {
|
||||
return true;
|
||||
}
|
||||
else { return false; }
|
||||
|
||||
}
|
||||
|
||||
public void performService() {
|
||||
System.out.println("General car service completed.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,50 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
|
||||
// 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);
|
||||
System.out.println("charge level: " + chargeLevel);
|
||||
}
|
||||
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
public void charge(double amount) {
|
||||
if (amount <= 0) {
|
||||
System.out.println("error");
|
||||
}
|
||||
else if ((chargeLevel + amount) <= 100 ) {
|
||||
chargeLevel += amount;
|
||||
}
|
||||
else {
|
||||
chargeLevel = amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if (v.getMileage() > 8000) {
|
||||
return true;
|
||||
}
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Battery system checked.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,12 +28,47 @@ public class GasCar extends Car implements Refuelable {
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
public String getEnergyType() { return fuelType + " (Gas)"; }
|
||||
|
||||
// TODO: Implement refuel(double amount)
|
||||
// Validate amount
|
||||
// Cap at 100
|
||||
public void refuel(double amount) {
|
||||
if (amount <= 0) {
|
||||
System.out.println("error");
|
||||
}
|
||||
else if ((fuelLevel + amount) <= 100 ) {
|
||||
fuelLevel += amount;
|
||||
}
|
||||
else {
|
||||
fuelLevel = amount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println("fuel level: " + fuelLevel);
|
||||
System.out.println("fuel type: " + fuelType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if (v.getMileage() > 12000) {
|
||||
return true;
|
||||
}
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -27,4 +27,25 @@ public class SportsCar extends GasCar {
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println("acceleration time: " + zeroToHundred);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if (v.getMileage() > 5000) {
|
||||
return true;
|
||||
}
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("High-performance brake system checked.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,14 +21,29 @@ public abstract class Vehicle {
|
||||
}
|
||||
|
||||
// TODO: Create getters for all fields
|
||||
public String getBrand() { return brand; }
|
||||
public String getModel() { return model; }
|
||||
public int getYear() { return year; }
|
||||
public double getMileage() { return mileage; }
|
||||
|
||||
// TODO: Implement addMileage(double km)
|
||||
// If km is negative → print error
|
||||
// Otherwise increase mileage
|
||||
public void addMileage(double km) {
|
||||
if (km < 0) {
|
||||
System.out.println("error");
|
||||
}
|
||||
else {
|
||||
mileage++;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public abstract String getEnergyType();
|
||||
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println();
|
||||
System.out.println(
|
||||
year + " " + brand + " " + model +
|
||||
" | Mileage: " + mileage +
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.Vehicle;
|
||||
import org.model.*;
|
||||
|
||||
/*
|
||||
* Utility class to inspect vehicles.
|
||||
@@ -18,5 +18,15 @@ public class VehicleInspector {
|
||||
// If ElectricCar → print "Check battery system"
|
||||
// If SportsCar → print "Check brakes and performance"
|
||||
// If GasCar → print "Check engine and oil"
|
||||
|
||||
if (v instanceof ElectricCar) {
|
||||
System.out.println("Check battery system.");
|
||||
}
|
||||
if (v instanceof SportsCar) {
|
||||
System.out.println("Check brakes and performance.");
|
||||
}
|
||||
if (v instanceof GasCar) {
|
||||
System.out.println("Check engine and oil.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user