Complete inspector and an example scenario

This commit is contained in:
2026-04-29 00:32:23 +03:30
parent 305f719836
commit 9182bba222
2 changed files with 51 additions and 6 deletions
+37 -6
View File
@@ -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,8 +24,17 @@ public class Main {
// ElectricCar tesla = new ElectricCar(...);
// ============================================================
ElectricCar tesla = new ElectricCar(
"Telsa", "Model S", 2022, 1000,
4, 500, 80);
GasCar RR = new GasCar(
"Rolls Royce", "Phantom", 2018, 85000,
5, 50, "Petrol");
SportsCar porsche = new SportsCar(
"Porsche", "GT3-RS", 2024, 500,
2, 70, "Petrol", 3.9);
// ============================================================
@@ -32,7 +48,7 @@ public class Main {
// as their parent type (Vehicle).
// ============================================================
Vehicle[] vehicles = {tesla, RR, porsche};
@@ -47,7 +63,10 @@ public class Main {
// behavior and can call inherited methods.
// ============================================================
for(Vehicle v : vehicles){
v.basicInfo();
System.out.println();
}
@@ -62,7 +81,9 @@ public class Main {
// • Show subclassspecific inspection messages
// ============================================================
for(Vehicle v : vehicles){
VehicleInspector.inspect(v);
}
@@ -80,7 +101,7 @@ public class Main {
//
// ============================================================
Serviceable[] serviceables = {tesla, RR, porsche};
@@ -106,7 +127,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.printf("Brand: %s, Model: %s\n", v.getBrand(), v.getModel());
s.performService();
System.out.println();
}
else{
System.out.println("The car does not need service\n");
}
}
@@ -118,7 +149,7 @@ public class Main {
//
// ============================================================
System.out.println("=== Workshop Completed ===");
@@ -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;
/*
@@ -16,7 +19,18 @@ public class VehicleInspector {
// TODO:
// If ElectricCar → print "Check battery system"
if(v instanceof ElectricCar){
System.out.println( "Check battery system");
}
// If SportsCar → print "Check brakes and performance"
if(v instanceof SportsCar){
System.out.println("Check brakes and performance");
}
// If GasCar → print "Check engine and oil"
if(v instanceof GasCar){
System.out.println("Check engine and oil");
}
}
}