develop
This commit is contained in:
+63
-12
@@ -1,5 +1,9 @@
|
||||
package org;
|
||||
|
||||
import org.model.*;
|
||||
import org.util.VehicleInspector;
|
||||
import org.behavior.Serviceable;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -17,8 +21,37 @@ public class Main {
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
|
||||
ElectricCar tesla = new ElectricCar(
|
||||
"Tesla",
|
||||
"Model 3",
|
||||
2022,
|
||||
12000,
|
||||
5,
|
||||
75,
|
||||
80
|
||||
);
|
||||
|
||||
GasCar bmw = new GasCar(
|
||||
"BMW",
|
||||
"320i",
|
||||
2018,
|
||||
8500,
|
||||
5,
|
||||
60,
|
||||
"Petrol"
|
||||
);
|
||||
|
||||
|
||||
SportsCar porsche = new SportsCar(
|
||||
"Porsche",
|
||||
"911 Turbo",
|
||||
2021,
|
||||
3000,
|
||||
2,
|
||||
70,
|
||||
"Petrol",
|
||||
2.8
|
||||
);
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -31,9 +64,7 @@ public class Main {
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
Vehicle[] vehicles = { tesla, bmw, porsche };
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -46,9 +77,11 @@ public class Main {
|
||||
// Demonstrate that all subclasses share the same Vehicle
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
System.out.println("=== Vehicle Basic Info ===");
|
||||
for (Vehicle v : vehicles) {
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -62,8 +95,10 @@ public class Main {
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
System.out.println("=== Vehicle Inspection ===");
|
||||
for (Vehicle v : vehicles) {
|
||||
VehicleInspector.inspect(v);
|
||||
}
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -79,8 +114,7 @@ public class Main {
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
Serviceable[] serviceables = { tesla, bmw, porsche };
|
||||
|
||||
|
||||
|
||||
@@ -106,6 +140,24 @@ public class Main {
|
||||
// Add blank lines between each item for readability.
|
||||
// ============================================================
|
||||
|
||||
System.out.println("=== Service Check ===");
|
||||
|
||||
for (Serviceable s : serviceables) {
|
||||
|
||||
// 1. Cast Serviceable → Vehicle
|
||||
Vehicle v = (Vehicle) s;
|
||||
|
||||
// 2. Check if it needs service
|
||||
if (s.needsService(v)) {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " needs service.");
|
||||
s.performService();
|
||||
} else {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " does NOT need service.");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -118,8 +170,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
double mileage, int seats) {
|
||||
|
||||
// TODO: Call super constructor
|
||||
super(brand, model, year, mileage);
|
||||
this.seats = seats;
|
||||
}
|
||||
|
||||
@@ -26,16 +27,36 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Print brand, model, year, mileage, and number of seats
|
||||
@Override
|
||||
public String getEnergyType()
|
||||
{
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
@Override
|
||||
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("Energy Type: " + getEnergyType());
|
||||
System.out.println("Seats: " + seats);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return getMileage() > 10000;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
*
|
||||
* needsService(Vehicle v):
|
||||
* - Check the mileage of the current car
|
||||
* - If mileage > 10000 return true
|
||||
* - Otherwise return false
|
||||
*
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -36,4 +36,49 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
@Override
|
||||
public String getEnergyType() {
|
||||
return "Electric";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails();
|
||||
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
|
||||
System.out.println("Charge Level: " + chargeLevel + "%");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void charge(double amount)
|
||||
{
|
||||
|
||||
if (amount <= 0)
|
||||
{
|
||||
System.out.println("Invalid charge amount!");
|
||||
return;
|
||||
}
|
||||
|
||||
chargeLevel += amount;
|
||||
|
||||
if (chargeLevel > 100)
|
||||
{
|
||||
chargeLevel = 100;
|
||||
}
|
||||
|
||||
System.out.println("Car charged. Current charge level: " + chargeLevel + "%");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return getMileage() > 8000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Battery system checked");
|
||||
chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,52 @@ 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)
|
||||
@Override
|
||||
public void refuel(double amount) {
|
||||
if (amount <= 0) {
|
||||
System.out.println("Invalid refuel amount!");
|
||||
return;
|
||||
}
|
||||
|
||||
fuelLevel += amount;
|
||||
|
||||
if (fuelLevel > 100) {
|
||||
fuelLevel = 100; // cap at 100
|
||||
}
|
||||
|
||||
System.out.println("Car refueled. Current fuel level: " + fuelLevel);
|
||||
}
|
||||
// Validate amount
|
||||
// Cap at 100
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails();
|
||||
System.out.println("Fuel Type: " + fuelType);
|
||||
System.out.println("Fuel Level: " + fuelLevel + "%");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return getMileage() > 12000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Engine oil changed");
|
||||
fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,11 @@ public class SportsCar extends GasCar {
|
||||
|
||||
this.zeroToHundred = zeroToHundred;
|
||||
}
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println("0-100 Acceleration: " + zeroToHundred + " seconds");
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
|
||||
@@ -21,12 +21,41 @@ 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.err.println("Error: Mileage cannot be negative.");
|
||||
} else {
|
||||
this.mileage += km;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.Vehicle;
|
||||
import org.model.ElectricCar;
|
||||
import org.model.SportsCar;
|
||||
import org.model.GasCar;
|
||||
|
||||
|
||||
/*
|
||||
* Utility class to inspect vehicles.
|
||||
@@ -14,9 +17,21 @@ public class VehicleInspector {
|
||||
|
||||
v.basicInfo();
|
||||
|
||||
// TODO:
|
||||
// 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");
|
||||
}
|
||||
|
||||
System.out.println("---------------------------");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user