Complete project with HybridCar bonus #1

Merged
peyman merged 1 commits from develop into master 2026-05-07 19:17:14 +00:00
8 changed files with 263 additions and 204 deletions
+42 -114
View File
@@ -1,126 +1,54 @@
package org;
import org.behavior.Serviceable;
import org.model.*;
import org.util.VehicleInspector;
public class Main {
public static void main(String[] args) {
ElectricCar tesla = new ElectricCar(
"Tesla", "Model S", 2022, 1000,
100, 550, 50
);
// ============================================================
// 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(...);
// ============================================================
// ============================================================
// 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).
// ============================================================
// ============================================================
// 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 ===");
//
// ============================================================
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
);
HybridCar Honda = new HybridCar(
"Honda", "Accord Hybrid" , 2022 , 35000,
5 , 82 ,65
);
Vehicle[] vehicles = {tesla , toyota , ferrari , Honda};
for (int i = 0 ; i< vehicles.length; i++){
vehicles[i].basicInfo();
System.out.println();
}
VehicleInspector.inspect(tesla);
VehicleInspector.inspect(toyota);
VehicleInspector.inspect(ferrari);
VehicleInspector.inspect(Honda);
Serviceable[] serviceables = {tesla , toyota , ferrari , Honda};
for (int i = 0 ; i< vehicles.length; i++){
Vehicle v = (Vehicle) serviceables[i];
if (serviceables[i].needsService(v)) {
System.out.println("Brand : "+v.getBrand() +" Model : "+v.getModel());
serviceables[i].performService();
System.out.println();
}else{
System.out.println(v.getBrand()+" does not need service");
System.out.println();
}
}
System.out.println("=== Workshop Completed ===");
}
}
+26 -27
View File
@@ -1,41 +1,40 @@
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 {
private int seats;
public Car(String brand, String model, int year,
double mileage, int seats) {
// TODO: Call super constructor
public Car(String brand, String model, int year, double mileage, int seats) {
super(brand , model , year , mileage);
this.seats = seats;
}
// TODO: Override getEnergyType() → return "Unknown"
@Override
public String getEnergyType() {
return "Unknown";
}
// TODO: Override showDetails()
// Print brand, model, year, mileage, and number of seats
@Override
public void showDetails() {
super.basicInfo();
System.out.println(" | Seats: " + seats);
}
@Override
public boolean needsService(Vehicle v) {
if (v.getMileage()> 10000){
return true;
}else{
return false;
}
}
@Override
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"
*/
}
+36 -18
View File
@@ -1,16 +1,5 @@
package org.model;
/*
* ElectricCar extends Car.
* It already inherits Serviceable from Car.
*
* Service logic for ElectricCar:
* - needsService(): return true if mileage > 8000
* - performService():
* Print "Battery system checked"
* Reset chargeLevel to 100
*/
import org.behavior.Chargeable;
public class ElectricCar extends Car implements Chargeable {
@@ -27,13 +16,42 @@ public class ElectricCar extends Car implements Chargeable {
this.chargeLevel = chargeLevel;
}
// TODO: Override getEnergyType() → return "Electric"
@Override
public String getEnergyType() {
return "Electric";
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print battery capacity and charge level
@Override
public void showDetails() {
super.showDetails();
System.out.println(" | battery Capacity: "+batteryCapacity+" | charge Level: "+chargeLevel);
}
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 100
@Override
public void charge(double amount) {
if (amount <= 0) {
System.out.println("Invalid amount! Must be positive.");
return;
}
if (chargeLevel + amount > 100) {
chargeLevel = 100;
System.out.println("Battery is fully charged! Charge level is now 100.0%");
}else {
chargeLevel += amount;
System.out.println("Added " + amount + "% charge. Charge level is now " + chargeLevel + "%");
}
}
@Override
public boolean needsService(Vehicle v) {
if (v.getMileage()> 8000){
return true;
}else{
return false;
}
}
@Override
public void performService() {
System.out.println("Battery system checked");
chargeLevel = 100;
}
}
+39 -17
View File
@@ -1,15 +1,5 @@
package org.model;
/*
* GasCar extends Car.
* Inherits Serviceable from Car.
*
* Service logic for GasCar:
* - needsService(): return true if mileage > 12000
* - performService():
* Print "Engine oil changed"
* Reset fuelLevel to 100
*/
import org.behavior.Refuelable;
@@ -27,13 +17,45 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
@Override
public boolean needsService(Vehicle v) {
if (v.getMileage()> 12000){
return true;
}else{
return false;
}
}
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
@Override
public void performService() {
System.out.println("Engine oil changed");
fuelLevel = 100;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print fuel level and fuel type
@Override
public String getEnergyType() {
return fuelType + " (Gas)";
}
@Override
public void refuel(double amount) {
if (amount <= 0){
System.out.println("Invalid amount! Must be positive.");
return;
}
if (fuelLevel + amount > 100) {
fuelLevel = 100;
System.out.println("Tank is full! Fuel level is now 100.0");
} else {
fuelLevel += amount;
System.out.println("Added " + amount + " units. Fuel level is now " + fuelLevel);
}
}
@Override
public void showDetails() {
super.showDetails();
System.out.println(" | fuel Level: "+fuelLevel+" | fuel Type: "+fuelType);
}
}
+65
View File
@@ -0,0 +1,65 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Refuelable, Chargeable {
private double batteryLevel;
private double fuelLevel;
public HybridCar(String brand, String model, int year,
double mileage, int seats,
double batteryLevel, double fuelLevel){
super(brand , model , year , mileage, seats);
this.batteryLevel = batteryLevel;
this.fuelLevel= fuelLevel;
}
@Override
public boolean needsService(Vehicle v) {
if (v.getMileage()> 10000){
return true;
}else{
return false;
}
}
@Override
public void performService() {
System.out.println("Engine oil changed and battery system checked");
fuelLevel = 100;
batteryLevel = 100;
}
@Override
public void refuel(double amount) {
if (amount <= 0){
System.out.println("Invalid amount! Must be positive.");
return;
}
if (fuelLevel + amount > 100) {
fuelLevel = 100;
System.out.println("Tank is full! Fuel level is now 100.0");
} else {
fuelLevel += amount;
System.out.println("Added " + amount + " units. Fuel level is now " + fuelLevel);
}
}
@Override
public void charge(double amount) {
if (amount <= 0){
System.out.println("Invalid amount! Must be positive.");
return;
}
if (batteryLevel + amount > 100) {
batteryLevel = 100;
System.out.println("Battery is fully charged! battery level is now 100.0");
} else {
batteryLevel += amount;
System.out.println("Added " + amount + " units. Battery level is now " + batteryLevel);
}
}
}
+22 -12
View File
@@ -1,14 +1,5 @@
package org.model;
/*
* SportsCar extends GasCar.
*
* Special service logic for SportsCar:
* - needsService(): return true if mileage > 5000
* - performService():
* Print "High-performance brake system checked"
*/
public class SportsCar extends GasCar {
private double zeroToHundred;
@@ -24,7 +15,26 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print acceleration time
@Override
public boolean needsService(Vehicle v) {
if (v.getMileage()> 5000){
return true;
}else{
return false;
}
}
@Override
public void performService() {
super.performService();
System.out.println("High-performance brake system checked");
}
@Override
public void showDetails() {
super.showDetails();
System.out.println(" | Acceleration time (0-100): " + zeroToHundred + " seconds");
}
}
+20 -5
View File
@@ -19,14 +19,29 @@ public abstract class Vehicle {
this.year = year;
this.mileage = mileage;
}
public String getBrand(){
return brand;
}
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");
} else{
mileage+= km;
}
}
public abstract String getEnergyType();
// TODO: Create getters for all fields
// TODO: Implement addMileage(double km)
// If km is negative → print error
// Otherwise increase mileage
// TODO: Declare abstract method getEnergyType()
public void basicInfo() {
System.out.println(
+13 -11
View File
@@ -1,22 +1,24 @@
package org.util;
import org.*;
import org.model.Vehicle;
/*
* Utility class to inspect vehicles.
* Demonstrates instanceof and polymorphism.
*/
import org.model.*;
public class VehicleInspector {
public static void inspect(Vehicle v) {
v.basicInfo();
// TODO:
// If ElectricCar → print "Check battery system"
// If SportsCar → print "Check brakes and performance"
// If GasCar → print "Check engine and oil"
v.basicInfo();
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 HybridCar) {
System.out.println("Check engine and battery systems");
} else if (v instanceof GasCar) {
System.out.println("Check engine and oil");
}
System.out.println();
}
}