implement Vehicle Management System

This commit is contained in:
2026-04-28 21:58:42 +03:30
parent e8aedb8354
commit 6ae0cdb95d
7 changed files with 211 additions and 14 deletions
+38 -14
View File
@@ -1,5 +1,12 @@
package org; package org;
import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.model.Vehicle;
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) {
@@ -17,8 +24,15 @@ public class Main {
// ElectricCar tesla = new ElectricCar(...); // ElectricCar tesla = new ElectricCar(...);
// ============================================================ // ============================================================
ElectricCar chevrolet = new ElectricCar("Chevrolet", "Bolt EV",
2023, 8500, 5, 65, 80) ;
GasCar toyota = new GasCar("Toyota", "Camery",
2022, 45000, 5, 65, "Petrol");
SportsCar lamborghini = new SportsCar("Lamborghini", "Huracan EVO",
2022, 9500, 2, 55, "Petrol",
3.2) ;
// ============================================================ // ============================================================
@@ -32,8 +46,7 @@ public class Main {
// as their parent type (Vehicle). // as their parent type (Vehicle).
// ============================================================ // ============================================================
Vehicle[] vehicle = {chevrolet, toyota, lamborghini};
// ============================================================ // ============================================================
@@ -47,9 +60,11 @@ public class Main {
// behavior and can call inherited methods. // behavior and can call inherited methods.
// ============================================================ // ============================================================
for (Vehicle v : vehicle)
{
v.basicInfo();
System.out.println();
}
// ============================================================ // ============================================================
// TODO 4: // TODO 4:
@@ -62,9 +77,11 @@ public class Main {
// • Show subclassspecific inspection messages // • Show subclassspecific inspection messages
// ============================================================ // ============================================================
for(Vehicle v : vehicle)
{
VehicleInspector.inspect(v) ;
System.out.println();
}
// ============================================================ // ============================================================
// TODO 5: // TODO 5:
@@ -80,9 +97,7 @@ public class Main {
// //
// ============================================================ // ============================================================
Serviceable[] serviceables = {chevrolet, toyota, lamborghini};
// ============================================================ // ============================================================
// TODO 6: // TODO 6:
@@ -106,6 +121,17 @@ public class Main {
// Add blank lines between each item for readability. // Add blank lines between each item for readability.
// ============================================================ // ============================================================
for (Serviceable s : serviceables)
{
Vehicle v = (Vehicle) s;
if (s.needsService(v))
{
System.out.println(v.getBrand() + " " + v.getModel() + " need service !!");
s.performService();
}
else
System.out.println(v.getBrand() + "" + v.getModel() + "" + " does not need service :)");
}
@@ -118,9 +144,7 @@ public class Main {
// //
// ============================================================ // ============================================================
System.out.println("=== Workshop Completed ===");
} }
} }
+22
View File
@@ -19,12 +19,20 @@ public abstract class Car extends Vehicle implements Serviceable {
double mileage, int seats) { double mileage, int seats) {
// TODO: Call super constructor // TODO: Call super constructor
super(brand, model, year, mileage);
this.seats = seats; this.seats = seats;
} }
// TODO: Override getEnergyType() → return "Unknown" // TODO: Override getEnergyType() → return "Unknown"
@Override
public String getEnergyType()
{
return "Unknown " ;
}
// TODO: Override showDetails() // TODO: Override showDetails()
@Override
public abstract void showDetails();
/* /*
* Implement Serviceable methods: * Implement Serviceable methods:
@@ -37,4 +45,18 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService(): * performService():
* - Print: "General car service completed" * - Print: "General car service completed"
*/ */
@Override
public boolean needsService(Vehicle v)
{
if (this.getMileage() > 10000)
{
return true;
}
return false;
}
@Override
public void performService()
{
System.out.println("General car service completed ");
}
} }
+47
View File
@@ -28,10 +28,57 @@ public class ElectricCar extends Car implements Chargeable {
} }
// TODO: Override getEnergyType() → return "Electric" // TODO: Override getEnergyType() → return "Electric"
@Override
public String getEnergyType()
{
return "Electric" ;
}
// TODO: Override showDetails() // TODO: Override showDetails()
@Override
public void showDetails() {}
// TODO: Implement charge(double amount) // TODO: Implement charge(double amount)
// Validate negative // Validate negative
// Cap at 100 // Cap at 100
public void charge(double amount)
{
if (amount<0)
{
System.out.println("Error : The charge amount can not be negative. :)");
return;
}
if (amount==0)
{
System.out.println("Nothing for charge! :)");
return;
}
if (amount>0)
{
chargeLevel+=amount;
if(chargeLevel>100)
{
chargeLevel=100;
System.out.println("Charge is full :)");
}
else
System.out.println("fuel level : " + chargeLevel + "%");
}
}
@Override
public boolean needsService(Vehicle v)
{
if (this.getMileage() > 8000)
return true;
return false;
}
@Override
public void performService()
{
System.out.println("Battery system checked");
chargeLevel = 100 ;
System.out.println("Charge level : 100%");
}
} }
+47
View File
@@ -28,10 +28,57 @@ public class GasCar extends Car implements Refuelable {
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" // TODO: Override getEnergyType() → return fuelType + " (Gas)"
@Override
public String getEnergyType()
{
return fuelType+" (Gas)" ;
}
// TODO: Implement refuel(double amount) // TODO: Implement refuel(double amount)
// Validate amount // Validate amount
// Cap at 100 // Cap at 100
public void refuel(double amount)
{
if (amount<0)
{
System.out.println("Error : the refuel amount can not be negative. :)");
return;
}
if (amount==0)
{
System.out.println("Nothing for refuel! :)");
return;
}
if (amount>0)
{
fuelLevel+=amount;
if(fuelLevel>100)
{
fuelLevel=100;
System.out.println("Tank is full :)");
}
else
System.out.println("fuel level : " + fuelLevel + "%");
}
}
// TODO: Override showDetails() // TODO: Override showDetails()
@Override
public void showDetails() {}
@Override
public boolean needsService(Vehicle v)
{
if (this.getMileage() > 12000)
return true;
return false;
}
@Override
public void performService()
{
System.out.println("Engine oil changed");
fuelLevel = 100 ;
System.out.println("fuel level : 100%");
}
} }
+20
View File
@@ -28,4 +28,24 @@ public class SportsCar extends GasCar {
// Print header // Print header
// Call super.showDetails() // Call super.showDetails()
// Print acceleration time // Print acceleration time
public void showDetails()
{
System.out.println("====Sports Car Specifications====");
super.showDetails() ;
System.out.println("Acceleration : " + zeroToHundred + "s");
}
@Override
public boolean needsService(Vehicle v)
{
if ( this.getMileage() > 5000)
return true;
return false;
}
@Override
public void performService()
{
System.out.println("High-performance brake system checked");
}
} }
+28
View File
@@ -21,13 +21,41 @@ public abstract class Vehicle {
} }
// TODO: Create getters for all fields // 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) // TODO: Implement addMileage(double km)
// If km is negative → print error // If km is negative → print error
// Otherwise increase mileage // Otherwise increase mileage
public void addMileage (double km)
{
if (km<0)
System.out.println("Error: km is negative");
else
mileage += km;
}
// TODO: Declare abstract method getEnergyType() // TODO: Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(
year + " " + brand + " " + model + year + " " + brand + " " + model +
@@ -1,6 +1,9 @@
package org.util; package org.util;
import org.*; import org.*;
import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.model.Vehicle; import org.model.Vehicle;
/* /*
@@ -18,5 +21,11 @@ public class VehicleInspector {
// If ElectricCar → print "Check battery system" // If ElectricCar → print "Check battery system"
// If SportsCar → print "Check brakes and performance" // If SportsCar → print "Check brakes and performance"
// If GasCar → print "Check engine and oil" // If GasCar → print "Check engine and oil"
if (v instanceof ElectricCar)
System.out.println("Check battrey 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");
} }
} }