Vehicle Management System #1
+39
-113
@@ -1,126 +1,52 @@
|
|||||||
package org;
|
package org;
|
||||||
|
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
|
import org.model.Vehicle;
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
ElectricCar porsche = new ElectricCar("Porsche", "Taycan", 2023, 12000.0, 4, 93, 65.5);
|
||||||
|
|
||||||
// ============================================================
|
GasCar benz = new GasCar("Mercedes-Benz", "C200", 2022, 28000.0, 5, 42.0, "Petrol");
|
||||||
// TODO 1:
|
|
||||||
// Create one instance of each vehicle type:
|
|
||||||
// - ElectricCar
|
|
||||||
// - GasCar
|
|
||||||
// - SportsCar
|
|
||||||
//
|
|
||||||
// Requirements:
|
|
||||||
// • Use the constructors you created in each class.
|
|
||||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
|
||||||
// • Store them in variables such as:
|
|
||||||
// ElectricCar tesla = new ElectricCar(...);
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
SportsCar audi = new SportsCar("Audi", "R8", 2021, 15000.0, 2, 55.0, "Petrol", 3.1);
|
||||||
|
|
||||||
|
Vehicle[] vehicles = { porsche, benz, audi };
|
||||||
|
|
||||||
|
for (Vehicle v : vehicles)
|
||||||
|
{
|
||||||
|
v.basicInfo();
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Vehicle v : vehicles)
|
||||||
|
{
|
||||||
|
VehicleInspector.inspect(v);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
Serviceable[] serviceables = { porsche, benz, audi };
|
||||||
// TODO 2:
|
|
||||||
// Create an array of Vehicle:
|
|
||||||
// Vehicle[] vehicles = { ... };
|
|
||||||
//
|
|
||||||
// This array MUST contain the objects created above.
|
|
||||||
// This demonstrates **polymorphism** because all different
|
|
||||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
|
||||||
// as their parent type (Vehicle).
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 3:
|
|
||||||
// Loop through the Vehicle[] array and:
|
|
||||||
// • Call v.basicInfo();
|
|
||||||
// • Print a blank line after each.
|
|
||||||
//
|
|
||||||
// Purpose:
|
|
||||||
// Demonstrate that all subclasses share the same Vehicle
|
|
||||||
// behavior and can call inherited methods.
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 4:
|
|
||||||
// Use VehicleInspector to inspect each vehicle:
|
|
||||||
//
|
|
||||||
// VehicleInspector.inspect(v);
|
|
||||||
//
|
|
||||||
// Purpose:
|
|
||||||
// • Demonstrate the use of `instanceof`
|
|
||||||
// • Show subclass‑specific inspection messages
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 5:
|
|
||||||
// Create an array of Serviceable:
|
|
||||||
// Serviceable[] serviceables = { ... };
|
|
||||||
//
|
|
||||||
// This demonstrates **interface‑based polymorphism**.
|
|
||||||
// Every Car subtype implements Serviceable.
|
|
||||||
//
|
|
||||||
// IMPORTANT:
|
|
||||||
// • You must cast to Vehicle when needed:
|
|
||||||
// Vehicle v = (Vehicle) s;
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 6:
|
|
||||||
// Loop through the Serviceable[] array:
|
|
||||||
//
|
|
||||||
// Steps inside loop:
|
|
||||||
//
|
|
||||||
// 1. Cast the current object to Vehicle:
|
|
||||||
// Vehicle v = (Vehicle) s;
|
|
||||||
//
|
|
||||||
// 2. Check:
|
|
||||||
// if (s.needsService(v)) { ... }
|
|
||||||
//
|
|
||||||
// 3. If true:
|
|
||||||
// - Print a message showing brand + model
|
|
||||||
// - Call s.performService();
|
|
||||||
//
|
|
||||||
// 4. Otherwise:
|
|
||||||
// - Print that the car does not need service
|
|
||||||
//
|
|
||||||
// Add blank lines between each item for readability.
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 7 (Optional):
|
|
||||||
// After everything is done, print a final message:
|
|
||||||
//
|
|
||||||
// System.out.println("=== Workshop Completed ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (Serviceable s : serviceables)
|
||||||
|
{
|
||||||
|
Vehicle v = (Vehicle) s;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
System.out.println("=== Workshop Completed ===");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,43 @@
|
|||||||
package org.model;
|
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;
|
import org.behavior.Serviceable;
|
||||||
|
|
||||||
public abstract class Car extends Vehicle implements Serviceable {
|
public abstract class Car extends Vehicle implements Serviceable
|
||||||
|
{
|
||||||
private int seats;
|
private int seats;
|
||||||
|
|
||||||
public Car(String brand, String model, int year,
|
public Car(String brand, String model, int year,
|
||||||
double mileage, int seats) {
|
double mileage, int seats)
|
||||||
|
{
|
||||||
// TODO: Call super constructor
|
super(brand, model, year, mileage);
|
||||||
this.seats = seats;
|
this.seats = seats;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Unknown"
|
@Override
|
||||||
|
public String getEnergyType()
|
||||||
|
{
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// 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("Seats: " + seats);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
@Override
|
||||||
* Implement Serviceable methods:
|
public boolean needsService(Vehicle v)
|
||||||
*
|
{
|
||||||
* needsService(Vehicle v):
|
return getMileage() > 10000;
|
||||||
* - Check the mileage of the current car
|
}
|
||||||
* - If mileage > 10000 return true
|
|
||||||
* - Otherwise return false
|
@Override
|
||||||
*
|
public void performService()
|
||||||
* performService():
|
{
|
||||||
* - Print: "General car service completed"
|
System.out.println("General car service completed");
|
||||||
*/
|
}
|
||||||
}
|
}
|
||||||
@@ -1,39 +1,62 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
|
||||||
* ElectricCar extends Car.
|
|
||||||
* It already inherits Serviceable from Car.
|
|
||||||
*
|
|
||||||
* Service logic for ElectricCar:
|
|
||||||
* - needsService(): return true if mileage > 8000
|
|
||||||
* - performService():
|
|
||||||
* Print "Battery system checked"
|
|
||||||
* Reset chargeLevel to 100
|
|
||||||
*/
|
|
||||||
|
|
||||||
import org.behavior.Chargeable;
|
import org.behavior.Chargeable;
|
||||||
|
|
||||||
public class ElectricCar extends Car implements Chargeable {
|
public class ElectricCar extends Car implements Chargeable
|
||||||
|
{
|
||||||
private int batteryCapacity;
|
private int batteryCapacity;
|
||||||
private double chargeLevel;
|
private double chargeLevel;
|
||||||
|
|
||||||
public ElectricCar(String brand, String model, int year,
|
public ElectricCar(String brand, String model, int year,
|
||||||
double mileage, int seats,
|
double mileage, int seats,
|
||||||
int batteryCapacity, double chargeLevel) {
|
int batteryCapacity, double chargeLevel)
|
||||||
|
{
|
||||||
super(brand, model, year, mileage, seats);
|
super(brand, model, year, mileage, seats);
|
||||||
this.batteryCapacity = batteryCapacity;
|
this.batteryCapacity = batteryCapacity;
|
||||||
this.chargeLevel = chargeLevel;
|
this.chargeLevel = chargeLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Electric"
|
@Override
|
||||||
|
public String getEnergyType()
|
||||||
|
{
|
||||||
|
return "Electric";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public void showDetails()
|
||||||
// Print battery capacity and charge level
|
{
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
|
||||||
|
System.out.println("Charge Level: " + chargeLevel + "%");
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement charge(double amount)
|
@Override
|
||||||
// Validate negative
|
public void charge(double amount)
|
||||||
// Cap at 100
|
{
|
||||||
}
|
if (amount < 0)
|
||||||
|
{
|
||||||
|
System.out.println("Charge amount cannot be negative.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
chargeLevel += amount;
|
||||||
|
if (chargeLevel > 100)
|
||||||
|
{
|
||||||
|
chargeLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v)
|
||||||
|
{
|
||||||
|
return getMileage() > 8000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService()
|
||||||
|
{
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
chargeLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +1,61 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
|
||||||
* GasCar extends Car.
|
|
||||||
* Inherits Serviceable from Car.
|
|
||||||
*
|
|
||||||
* Service logic for GasCar:
|
|
||||||
* - needsService(): return true if mileage > 12000
|
|
||||||
* - performService():
|
|
||||||
* Print "Engine oil changed"
|
|
||||||
* Reset fuelLevel to 100
|
|
||||||
*/
|
|
||||||
|
|
||||||
import org.behavior.Refuelable;
|
import org.behavior.Refuelable;
|
||||||
|
|
||||||
public class GasCar extends Car implements Refuelable {
|
public class GasCar extends Car implements Refuelable
|
||||||
|
{
|
||||||
private double fuelLevel;
|
private double fuelLevel;
|
||||||
private String fuelType;
|
private String fuelType;
|
||||||
|
|
||||||
public GasCar(String brand, String model, int year,
|
public GasCar(String brand, String model, int year,
|
||||||
double mileage, int seats,
|
double mileage, int seats,
|
||||||
double fuelLevel, String fuelType) {
|
double fuelLevel, String fuelType)
|
||||||
|
{
|
||||||
super(brand, model, year, mileage, seats);
|
super(brand, model, year, mileage, seats);
|
||||||
this.fuelLevel = fuelLevel;
|
this.fuelLevel = fuelLevel;
|
||||||
this.fuelType = fuelType;
|
this.fuelType = fuelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
@Override
|
||||||
|
public String getEnergyType()
|
||||||
|
{
|
||||||
|
return fuelType + " (Gas)";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement refuel(double amount)
|
@Override
|
||||||
// Validate amount
|
public void refuel(double amount)
|
||||||
// Cap at 100
|
{
|
||||||
|
if (amount < 0)
|
||||||
|
{
|
||||||
|
System.out.println("Refuel amount cannot be negative.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
fuelLevel += amount;
|
||||||
// Call super.showDetails()
|
if (fuelLevel > 100)
|
||||||
// Print fuel level and fuel type
|
{
|
||||||
}
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails()
|
||||||
|
{
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println("Fuel Level: " + fuelLevel + "%");
|
||||||
|
System.out.println("Fuel Type: " + fuelType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v)
|
||||||
|
{
|
||||||
|
return getMileage() > 12000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService()
|
||||||
|
{
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,30 +1,36 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
public class SportsCar extends GasCar
|
||||||
* SportsCar extends GasCar.
|
{
|
||||||
*
|
|
||||||
* Special service logic for SportsCar:
|
|
||||||
* - needsService(): return true if mileage > 5000
|
|
||||||
* - performService():
|
|
||||||
* Print "High-performance brake system checked"
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class SportsCar extends GasCar {
|
|
||||||
|
|
||||||
private double zeroToHundred;
|
private double zeroToHundred;
|
||||||
|
|
||||||
public SportsCar(String brand, String model, int year,
|
public SportsCar(String brand, String model, int year,
|
||||||
double mileage, int seats,
|
double mileage, int seats,
|
||||||
double fuelLevel, String fuelType,
|
double fuelLevel, String fuelType,
|
||||||
double zeroToHundred) {
|
double zeroToHundred)
|
||||||
|
{
|
||||||
super(brand, model, year, mileage, seats,
|
super(brand, model, year, mileage, seats,
|
||||||
fuelLevel, fuelType);
|
fuelLevel, fuelType);
|
||||||
|
|
||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public void showDetails()
|
||||||
// Print acceleration time
|
{
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println("0-100 km/h: " + zeroToHundred + " seconds");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v)
|
||||||
|
{
|
||||||
|
return getMileage() > 5000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService()
|
||||||
|
{
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,59 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
public abstract class Vehicle
|
||||||
* Abstract base class for all vehicles.
|
{
|
||||||
* This class DOES NOT implement Serviceable.
|
|
||||||
* Service behavior will be defined in concrete subclasses.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public abstract class Vehicle {
|
|
||||||
|
|
||||||
private String brand;
|
private String brand;
|
||||||
private String model;
|
private String model;
|
||||||
private int year;
|
private int year;
|
||||||
private double mileage;
|
private double mileage;
|
||||||
|
|
||||||
public Vehicle(String brand, String model, int year, double mileage) {
|
public Vehicle(String brand, String model, int year, double mileage)
|
||||||
|
{
|
||||||
this.brand = brand;
|
this.brand = brand;
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.year = year;
|
this.year = year;
|
||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create getters for all fields
|
public String getBrand()
|
||||||
|
{
|
||||||
|
return brand;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement addMileage(double km)
|
public String getModel()
|
||||||
// If km is negative → print error
|
{
|
||||||
// Otherwise increase mileage
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Declare abstract method getEnergyType()
|
public int getYear()
|
||||||
|
{
|
||||||
|
return year;
|
||||||
|
}
|
||||||
|
|
||||||
public void basicInfo() {
|
public double getMileage()
|
||||||
|
{
|
||||||
|
return mileage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMileage(double km)
|
||||||
|
{
|
||||||
|
if (km < 0)
|
||||||
|
{
|
||||||
|
System.out.println("Error: Mileage cannot be negative.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.mileage += km;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract String getEnergyType();
|
||||||
|
|
||||||
|
public void basicInfo()
|
||||||
|
{
|
||||||
System.out.println(
|
System.out.println(
|
||||||
year + " " + brand + " " + model +
|
year + " " + brand + " " + model + " | Mileage: " + mileage + " | Energy: " + getEnergyType());
|
||||||
" | Mileage: " + mileage +
|
|
||||||
" | Energy: " + getEnergyType()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void showDetails();
|
public abstract void showDetails();
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,28 @@
|
|||||||
package org.util;
|
package org.util;
|
||||||
|
|
||||||
import org.*;
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
import org.model.Vehicle;
|
import org.model.Vehicle;
|
||||||
|
|
||||||
/*
|
public class VehicleInspector
|
||||||
* Utility class to inspect vehicles.
|
{
|
||||||
* Demonstrates instanceof and polymorphism.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class VehicleInspector {
|
|
||||||
|
|
||||||
public static void inspect(Vehicle v) {
|
|
||||||
|
|
||||||
|
public static void inspect(Vehicle v)
|
||||||
|
{
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
|
||||||
// TODO:
|
if (v instanceof ElectricCar)
|
||||||
// If ElectricCar → print "Check battery system"
|
{
|
||||||
// If SportsCar → print "Check brakes and performance"
|
System.out.println("Check battery system");
|
||||||
// If GasCar → print "Check engine and oil"
|
}
|
||||||
|
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