Merge pull request 'Complete VMS with polymorphism demonstration' (#1) from develop into master
Reviewed-on: #1 95/100
This commit was merged in pull request #1.
This commit is contained in:
Generated
+1
-1
@@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
+41
-103
@@ -1,124 +1,62 @@
|
|||||||
package org;
|
package org;
|
||||||
|
|
||||||
|
import org.model.Vehicle;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// 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(...);
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
Vehicle[] vehicles = {tesla , toyota , ferrari};
|
||||||
|
|
||||||
// ============================================================
|
for (Vehicle v : vehicles) {
|
||||||
// TODO 2:
|
v.basicInfo();
|
||||||
// Create an array of Vehicle:
|
System.out.println();
|
||||||
// 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).
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
for (Vehicle v : vehicles) {
|
||||||
|
VehicleInspector.inspect(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
Serviceable[] serviceables = {tesla, toyota, ferrari};
|
||||||
|
|
||||||
|
for (Serviceable s : serviceables) {
|
||||||
|
|
||||||
|
Vehicle v = (Vehicle) s;
|
||||||
|
|
||||||
// ============================================================
|
v.basicInfo();
|
||||||
// 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.
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
if (s.needsService(v)) {
|
||||||
|
System.out.println(v.getBrand() + " " + v.getModel());
|
||||||
|
System.out.print("Performing service: ");
|
||||||
|
s.performService();
|
||||||
|
} else {
|
||||||
|
System.out.println(v.getBrand() + " " + v.getModel() + " does NOT need service");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("=== Workshop Completed ===");
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// 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 ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,31 +11,44 @@ package org.model;
|
|||||||
|
|
||||||
import org.behavior.Serviceable;
|
import org.behavior.Serviceable;
|
||||||
|
|
||||||
|
import java.lang.classfile.Superclass;
|
||||||
|
|
||||||
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) {
|
super(brand, model, year, mileage);
|
||||||
|
|
||||||
// TODO: Call super constructor
|
|
||||||
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.print(
|
||||||
|
getYear() + " " + getBrand() + " " + getModel() +
|
||||||
|
" | Mileage: " + getMileage() +
|
||||||
|
" | Energy: " + getEnergyType() +
|
||||||
|
" | Seats: " + seats
|
||||||
|
);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if(getMileage() > 10000) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,14 +26,49 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
this.batteryCapacity = batteryCapacity;
|
this.batteryCapacity = batteryCapacity;
|
||||||
this.chargeLevel = chargeLevel;
|
this.chargeLevel = chargeLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return "Electric";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Electric"
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.print(
|
||||||
|
" | battery Capacity: " + batteryCapacity +
|
||||||
|
" | charge Level: " + chargeLevel
|
||||||
|
);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void charge(double amount) {
|
||||||
|
if (amount <= 0) {
|
||||||
|
System.out.println("Error");
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
if (this.chargeLevel + amount > 100) {
|
||||||
// Call super.showDetails()
|
this.chargeLevel = 100;
|
||||||
// Print battery capacity and charge level
|
}
|
||||||
|
|
||||||
// TODO: Implement charge(double amount)
|
else {
|
||||||
// Validate negative
|
this.chargeLevel += amount;
|
||||||
// Cap at 100
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if(getMileage() > 8000) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
this.chargeLevel = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,13 +27,47 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
this.fuelType = fuelType;
|
this.fuelType = fuelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
@Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return fuelType + " (Gas)";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement refuel(double amount)
|
public void refuel(double amount) {
|
||||||
// Validate amount
|
if (amount <= 0) {
|
||||||
// Cap at 100
|
System.out.println("Error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
if (this.fuelLevel + amount > 100) {
|
||||||
// Call super.showDetails()
|
this.fuelLevel = 100;
|
||||||
// Print fuel level and fuel type
|
} else {
|
||||||
|
this.fuelLevel += amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.print(
|
||||||
|
" | fuel type: " + fuelType +
|
||||||
|
" | fuel level: " + fuelLevel
|
||||||
|
);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if(getMileage() > 12000) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
this.fuelLevel = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,27 @@ public class SportsCar extends GasCar {
|
|||||||
|
|
||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.print(
|
||||||
|
" | acceleration time: " + this.zeroToHundred
|
||||||
|
);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public boolean needsService(Vehicle v) {
|
||||||
// Print acceleration time
|
if(getMileage() > 5000) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,36 @@ public abstract class Vehicle {
|
|||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create getters for all fields
|
public String getBrand() {
|
||||||
|
|
||||||
// TODO: Implement addMileage(double km)
|
return brand;
|
||||||
// If km is negative → print error
|
}
|
||||||
// Otherwise increase mileage
|
|
||||||
|
|
||||||
// TODO: Declare abstract method getEnergyType()
|
public String getModel() {
|
||||||
|
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getYear() {
|
||||||
|
|
||||||
|
return year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMileage() {
|
||||||
|
|
||||||
|
return mileage;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void addMileage(double km) {
|
||||||
|
if ( km < 0 ) {
|
||||||
|
System.out.println("Error: Mileage cannot be negative!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.mileage += km;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract String getEnergyType();
|
||||||
|
|
||||||
public void basicInfo() {
|
public void basicInfo() {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package org.util;
|
|||||||
|
|
||||||
import org.*;
|
import org.*;
|
||||||
import org.model.Vehicle;
|
import org.model.Vehicle;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
|
import org.model.GasCar;
|
||||||
/*
|
/*
|
||||||
* Utility class to inspect vehicles.
|
* Utility class to inspect vehicles.
|
||||||
* Demonstrates instanceof and polymorphism.
|
* Demonstrates instanceof and polymorphism.
|
||||||
@@ -14,9 +16,14 @@ public class VehicleInspector {
|
|||||||
|
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
|
||||||
// TODO:
|
if (v instanceof SportsCar) {
|
||||||
// If ElectricCar → print "Check battery system"
|
System.out.println("→ Check brakes and performance");
|
||||||
// If SportsCar → print "Check brakes and performance"
|
}
|
||||||
// If GasCar → print "Check engine and oil"
|
else if (v instanceof ElectricCar) {
|
||||||
|
System.out.println("→ Check battery system");
|
||||||
|
}
|
||||||
|
else if (v instanceof GasCar) {
|
||||||
|
System.out.println("→ Check engine and oil");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user