Vehicle Management System

This commit is contained in:
2026-04-28 17:21:05 +03:30
parent 6cb8f9a8d8
commit 1c7604b3c7
7 changed files with 244 additions and 239 deletions
+41 -114
View File
@@ -1,126 +1,53 @@
package org;
public class Main {
public static void main(String[] args) {
import org.model.Porsche;
import org.model.Benz;
import org.model.Audi;
import org.model.Vehicle;
import org.service.Serviceable;
import org.util.VehicleInspector;
// ============================================================
// 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(...);
// ============================================================
public class Main
{
public static void main(String[] args)
{
Porsche porsche = new Porsche("Porsche", "911 Carrera", 2023, 5000, 450);
Benz benz = new Benz("Mercedes-Benz", "E-Class", 2022, 10000, 220);
Audi audi = new Audi("Audi", "RS5 Sportback", 2021, 8000, 333);
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).
// ============================================================
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();
}
// ============================================================
// 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 subclassspecific inspection messages
// ============================================================
// ============================================================
// TODO 5:
// Create an array of Serviceable:
// Serviceable[] serviceables = { ... };
//
// This demonstrates **interfacebased 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 ===");
//
// ============================================================
System.out.println("=== Workshop Completed ===");
}
}
}