Merge PR 'priject vehicle' (#1) from develop into master - Full Mark 100/100

Full Mark 100/100
This commit was merged in pull request #1.
This commit is contained in:
2026-05-15 13:31:17 +00:00
8 changed files with 269 additions and 40 deletions
+32 -5
View File
@@ -1,8 +1,13 @@
package org;
import org.behavior.Serviceable;
import org.model.*;
import org.util.VehicleInspector;
public class Main {
public static void main(String[] args) {
// ============================================================
// TODO 1:
// Create one instance of each vehicle type:
@@ -18,7 +23,9 @@ public class Main {
// ============================================================
ElectricCar tesla = new ElectricCar("Tesla", "Model S", 2024, 9000, 5, 100, 80);
GasCar toyota = new GasCar("Toyota", "Camry", 2022, 15000, 5, 40, "Petrol");
SportsCar porsche = new SportsCar("Porsche", "911", 2025, 3000, 2, 50, "Premium", 3.2);
// ============================================================
@@ -33,7 +40,7 @@ public class Main {
// ============================================================
Vehicle[] vehicles = {tesla, toyota, porsche};
// ============================================================
@@ -48,7 +55,10 @@ public class Main {
// ============================================================
for (Vehicle v : vehicles) {
v.basicInfo();
System.out.println();
}
// ============================================================
@@ -63,7 +73,10 @@ public class Main {
// ============================================================
for (Vehicle v : vehicles) {
VehicleInspector.inspect(v);
}
System.out.println();
// ============================================================
@@ -81,7 +94,7 @@ public class Main {
// ============================================================
Serviceable[] serviceables = {tesla, toyota, porsche};
// ============================================================
@@ -107,6 +120,19 @@ public class Main {
// ============================================================
for (Serviceable s : serviceables) {
Vehicle v = (Vehicle) s;
if (s.needsService(v)) {
System.out.println("Service Required for: " + v.getBrand() + " " + v.getModel());
s.performService();
} else {
System.out.println("The car does not need service! ");
}
System.out.println();
}
@@ -120,6 +146,7 @@ public class Main {
System.out.println("=== Workshop Completed ===");
}
+24 -12
View File
@@ -1,14 +1,5 @@
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;
public abstract class Car extends Vehicle implements Serviceable {
@@ -18,13 +9,34 @@ public abstract class Car extends Vehicle implements Serviceable {
public Car(String brand, String model, int year,
double mileage, int seats) {
// TODO: Call super constructor
super(brand , model , year , mileage);
this.seats = seats;
}
// TODO: Override getEnergyType() → return "Unknown"
@Override
public String getEnergyType(){
return "Unknown";
}
// TODO: Override showDetails()
public void showDetails(){
basicInfo();
System.out.println("Seat :" + seats);
}
public boolean needsService(Vehicle v){
return this.getMileage() > 10000;
}
public void performService(){
System.out.println("General car service completed");
}
public abstract int getChargeLevel();
public abstract int getFuelLevel();
public abstract boolean needService();
/*
* Implement Serviceable methods:
+52 -5
View File
@@ -27,11 +27,58 @@ public class ElectricCar extends Car implements Chargeable {
this.chargeLevel = chargeLevel;
}
// TODO: Override getEnergyType() → return "Electric"
@Override
public String getEnergyType(){
return "Electric";
}
// TODO: Override showDetails()
@Override
public void charge(double amount) {
if(amount<0){
System.out.println("Error: Cannot charge with a negative amount.");
return;
}
this.chargeLevel += amount;
if (this.chargeLevel > 100) {
this.chargeLevel = 100;
System.out.println("Battery is fully charged.");
}
else {
System.out.println("Charged " + amount + "Current charge level: " + this.chargeLevel);
}
}
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 100
@Override
public void performService() {
System.out.println("Battery system checked");
chargeLevel = 100;
System.out.println("Reset chargeLevel to 100");
}
@Override
public int getChargeLevel() {
return 0;
}
@Override
public int getFuelLevel() {
return 0;
}
@Override
public boolean needService() {
return false;
}
@Override
public boolean needsService(Vehicle v) {
return this.getMileage() > 8000;
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("Battery Capacity: " + this.batteryCapacity);
System.out.println("Charge Level: " + this.chargeLevel);
}
}
+57 -5
View File
@@ -27,11 +27,63 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
@Override
public String getEnergyType(){
return fuelType + " (Gas)";
}
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
@Override
public void refuel(double amount) {
if(amount <0){
System.out.println("Error: Cannot refuel with a negative amount.");
return;
}
this.fuelLevel += amount;
if(this.fuelLevel>100){
this.fuelLevel = 100;
System.out.println("Fuel tank is full.");
}
else{
System.out.println("Refueled" + amount + "Current fuel level:" + this.fuelLevel);
}
}
// TODO: Override showDetails()
@Override
public void performService() {
System.out.println("Engine oil changed");
this.fuelLevel = 100;
System.out.println("Fuel level reset to 100.");
}
@Override
public int getChargeLevel() {
return 0;
}
@Override
public int getFuelLevel() {
return 0;
}
@Override
public boolean needService() {
return false;
}
@Override
public boolean needsService(Vehicle v) {
return this.getMileage() > 12000;
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("FUel Type :" + this.fuelType );
System.out.println("Fuel Level :" + this.fuelLevel );
if (needsService(this)) {
System.out.println("Status: Needs Service");
}
}
}
+51
View File
@@ -0,0 +1,51 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Chargeable , Refuelable {
private int chargeLevel;
private int fuelLevel;
public HybridCar(String brand, String model, int year, double mileage, int seats) {
super(brand, model, year, mileage, seats);
this.chargeLevel=chargeLevel;
this.fuelLevel = fuelLevel;
}
@Override
public void charge(double amount) {
this.chargeLevel = 100;
System.out.println("HybridCar charged to 100%");
}
@Override
public int getChargeLevel(){
return this.chargeLevel;
}
@Override
public void refuel(double amount) {
this.fuelLevel = 100;
System.out.println("HybridCar refueled to 100%");
}
@Override
public int getFuelLevel(){
return this.fuelLevel;
}
@Override
public boolean needService(){
return this.getMileage() > 10000;
}
@Override
public void performService(){
System.out.println("Engine oil changed and battery system checked");
this.fuelLevel = 100;
this.chargeLevel = 100;
System.out.println("Fuel and charge levels reset to 100%");
}
}
+17 -4
View File
@@ -24,8 +24,21 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred;
}
// TODO: Override showDetails()
// Print header
// Call super.showDetails()
// Print acceleration time
@Override
public void showDetails() {
System.out.println("--- Sports Car Details ---");
super.showDetails();
System.out.println("Acceleration: " + this.zeroToHundred );
}
@Override
public boolean needsService(Vehicle v) {
return this.getMileage() > 5000;
}
@Override
public void performService() {
System.out.println("High-performance brake system checked");
}
}
+24 -5
View File
@@ -20,13 +20,32 @@ public abstract class Vehicle {
this.mileage = mileage;
}
// TODO: Create getters for all fields
public String getBrand() {
return brand;
}
// TODO: Implement addMileage(double km)
// If km is negative → print error
// Otherwise increase mileage
public String getModel() {
return model;
}
// TODO: Declare abstract method getEnergyType()
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.");
}
else {
this.mileage += km;
}
}
public abstract String getEnergyType();
public void basicInfo() {
System.out.println(
+12 -4
View File
@@ -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;
/*
@@ -14,9 +17,14 @@ public class VehicleInspector {
v.basicInfo();
// TODO:
// If ElectricCar → print "Check battery system"
// If SportsCar → print "Check brakes and performance"
// If GasCar → print "Check engine and oil"
if(v instanceof SportsCar){
System.out.println("Check brakes and performance");
}
else if(v instanceof ElectricCar){
System.out.println("Check battery system");
}
else if(v instanceof GasCar){
System.out.println("Check engine and oil");
}
}
}