Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
25c3583841 | ||
|
|
2e1dcd4a16 | ||
|
|
b39befac19 | ||
|
|
3aa2bd648b | ||
|
|
81b635df0c |
+31
-12
@@ -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) {
|
||||
|
||||
@@ -17,9 +24,9 @@ public class Main {
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
ElectricCar nissan = new ElectricCar("Nissan", "Leaf", 2022, 850, 5, 62, 88.5);
|
||||
GasCar ford = new GasCar("Ford", "Mustang", 2023, 12000, 4, 55.0, "Petrol");
|
||||
SportsCar porsche = new SportsCar("Porsche", "911 Turbo S", 2023, 5000, 2, 55.0, "Petrol", 2.6);
|
||||
|
||||
// ============================================================
|
||||
// TODO 2:
|
||||
@@ -32,6 +39,7 @@ public class Main {
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
Vehicle[] vehicles = {nissan, ford, porsche};
|
||||
|
||||
|
||||
|
||||
@@ -47,7 +55,10 @@ public class Main {
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
|
||||
for(Vehicle v: vehicles){
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,6 +73,9 @@ public class Main {
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
VehicleInspector.inspect(v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -80,9 +94,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
Serviceable[] serviceables = {nissan, ford, porsche};
|
||||
|
||||
// ============================================================
|
||||
// TODO 6:
|
||||
@@ -106,6 +118,17 @@ 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(v.getBrand() + " " + v.getModel() + " needs service.");
|
||||
s.performService();
|
||||
System.out.println();
|
||||
} else {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " does not need service.");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -117,10 +140,6 @@ public class Main {
|
||||
// System.out.println("=== Workshop Completed ===");
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,16 +15,30 @@ 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"
|
||||
|
||||
@Override
|
||||
public String getEnergyType() {
|
||||
return "Unknown";
|
||||
}
|
||||
public int getSeats() {
|
||||
return seats;
|
||||
}
|
||||
// TODO: Override showDetails()
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
System.out.println(getBrand() + " " + getModel() + getYear() + " | mileage: " + getMileage() + " | seats: " + getSeats());
|
||||
}
|
||||
|
||||
|
||||
// Print brand, model, year, mileage, and number of seats
|
||||
|
||||
/*
|
||||
@@ -38,4 +52,16 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if (this.getMileage() > 10000)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,7 @@ 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) {
|
||||
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;
|
||||
@@ -29,11 +27,45 @@ 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 + " | 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,44 @@ public class GasCar extends Car implements Refuelable {
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
|
||||
@Override
|
||||
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("amount unacceptable");
|
||||
else{
|
||||
if (fuelLevel + amount > 100)
|
||||
fuelLevel = 100;
|
||||
else
|
||||
fuelLevel += amount;
|
||||
}
|
||||
}
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println(" | fuelLevel: " + fuelLevel + " | fuel type: " + fuelType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return getMileage() > 12000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
super.performService();
|
||||
System.out.println("engine and oil checked");
|
||||
this.fuelLevel = 100;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.model;
|
||||
|
||||
import org.behavior.Chargeable;
|
||||
import org.behavior.Refuelable;
|
||||
|
||||
public class HybridCar extends Car implements Refuelable, Chargeable {
|
||||
|
||||
private double fuelLevel;
|
||||
private String fuelType;
|
||||
private int batteryCapacity;
|
||||
private double chargeLevel;
|
||||
|
||||
public HybridCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType, int batteryCapacity, double chargeLevel) {
|
||||
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.fuelLevel = fuelLevel;
|
||||
this.fuelType = fuelType;
|
||||
this.batteryCapacity = batteryCapacity;
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
@Override
|
||||
public String getEnergyType() {
|
||||
return "Hybrid (Gas + Gasoline)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println(" | fuel level: " + fuelLevel + " | charge level: " + chargeLevel);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 10000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed and battery system checked");
|
||||
|
||||
this.fuelLevel = 100;
|
||||
this.chargeLevel = 100;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
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 refuel(double amount) {
|
||||
if(amount < 0)
|
||||
System.out.println("amount unacceptable");
|
||||
else{
|
||||
if (fuelLevel + amount > 100)
|
||||
fuelLevel = 100;
|
||||
else
|
||||
fuelLevel += amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,13 +13,9 @@ public class SportsCar extends GasCar {
|
||||
|
||||
private double zeroToHundred;
|
||||
|
||||
public SportsCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
double fuelLevel, String fuelType,
|
||||
double zeroToHundred) {
|
||||
public SportsCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType, double zeroToHundred) {
|
||||
|
||||
super(brand, model, year, mileage, seats,
|
||||
fuelLevel, fuelType);
|
||||
super(brand, model, year, mileage, seats, fuelLevel, fuelType);
|
||||
|
||||
this.zeroToHundred = zeroToHundred;
|
||||
}
|
||||
@@ -27,4 +23,20 @@ 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) {
|
||||
return getMileage() > 5000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
super.performService();
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,34 @@ public abstract class Vehicle {
|
||||
|
||||
// TODO: Create getters for all fields
|
||||
|
||||
public String getBrand(){
|
||||
return this.brand;
|
||||
}
|
||||
|
||||
public String getModel(){
|
||||
return this.model;
|
||||
}
|
||||
|
||||
public int getYear(){
|
||||
return this.year;
|
||||
}
|
||||
|
||||
public double getMileage(){
|
||||
return this.mileage;
|
||||
}
|
||||
// TODO: Implement addMileage(double km)
|
||||
// If km is negative → print error
|
||||
// Otherwise increase mileage
|
||||
|
||||
public void addMileage(int addMileage){
|
||||
if (addMileage > 0)
|
||||
this.mileage += addMileage;
|
||||
else
|
||||
System.out.println("Error");
|
||||
}
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
year + " " + brand + " " + model +
|
||||
@@ -37,4 +59,6 @@ public abstract class Vehicle {
|
||||
}
|
||||
|
||||
public abstract void showDetails();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.ElectricCar;
|
||||
import org.model.GasCar;
|
||||
import org.model.SportsCar;
|
||||
import org.model.Vehicle;
|
||||
|
||||
/*
|
||||
@@ -18,5 +21,17 @@ 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");
|
||||
}
|
||||
else if (v instanceof SportsCar) {
|
||||
|
||||
System.out.println("check brakes and performance");
|
||||
}
|
||||
else if (v instanceof GasCar) {
|
||||
|
||||
System.out.println("Check engine and oil");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user