Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b86a1bf4f7 | ||
|
|
6318950770 |
@@ -41,9 +41,9 @@ The project consists of the following classes and interfaces:
|
|||||||
| GasCar | | ElectricCar |
|
| GasCar | | ElectricCar |
|
||||||
| (Class) | | (Class) |
|
| (Class) | | (Class) |
|
||||||
+-------------------+ +-------------------+
|
+-------------------+ +-------------------+
|
||||||
^ | |
|
^ ^ ^
|
||||||
| extends | implements | implements
|
| extends | implements | implements
|
||||||
| ⌄ ⌄
|
| | |
|
||||||
+-------------------+ +-------------------+ +-------------------+
|
+-------------------+ +-------------------+ +-------------------+
|
||||||
| SportsCar | | Refuelable | | Chargeable |
|
| SportsCar | | Refuelable | | Chargeable |
|
||||||
| (Class) | | (Interface) | | (Interface) |
|
| (Class) | | (Interface) | | (Interface) |
|
||||||
@@ -118,14 +118,7 @@ Add a `HybridCar` class
|
|||||||
- Create a `HybridCar` class that extends `Car`.
|
- Create a `HybridCar` class that extends `Car`.
|
||||||
- It will need variables for both battery level and fuel level
|
- It will need variables for both battery level and fuel level
|
||||||
- Implement `Refuelable` and `Chargeable` together
|
- Implement `Refuelable` and `Chargeable` together
|
||||||
- Service logic for HybridCar:
|
- (Optional) Override methods to manage both power sources (driving drains battery first, then gas)
|
||||||
```
|
|
||||||
needsService(): return true if mileage > 10000
|
|
||||||
performService():
|
|
||||||
Print "Engine oil changed and battery system checked"
|
|
||||||
Reset fuelLevel to 100
|
|
||||||
Reset chargeLevel to 100
|
|
||||||
```
|
|
||||||
|
|
||||||
## Workflow
|
## Workflow
|
||||||
1. Fork the repository to your own Git account
|
1. Fork the repository to your own Git account
|
||||||
@@ -140,6 +133,6 @@ git checkout -b develop
|
|||||||
```
|
```
|
||||||
git push origin develop
|
git push origin develop
|
||||||
```
|
```
|
||||||
7. Create a Pull Request from `develop` to `main` or `master`
|
7. Create a Pull Request from `develop` to `main`
|
||||||
|
|
||||||
# GOOD LUCK! :)
|
# GOOD LUCK! :)
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
package org;
|
package org;
|
||||||
|
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
import org.model.*;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 1:
|
// TODO 1:
|
||||||
// Create one instance of each vehicle type:
|
// 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 ===");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,5 @@
|
|||||||
package org.model;
|
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;
|
import org.behavior.Serviceable;
|
||||||
|
|
||||||
public abstract class Car extends Vehicle implements Serviceable {
|
public abstract class Car extends Vehicle implements Serviceable {
|
||||||
@@ -18,14 +9,34 @@ public abstract class Car extends Vehicle implements Serviceable {
|
|||||||
public Car(String brand, String model, int year,
|
public Car(String brand, String model, int year,
|
||||||
double mileage, int seats) {
|
double mileage, int seats) {
|
||||||
|
|
||||||
// TODO: Call super constructor
|
super(brand , model , year , mileage);
|
||||||
this.seats = seats;
|
this.seats = seats;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Unknown"
|
@Override
|
||||||
|
public String getEnergyType(){
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
public void showDetails(){
|
||||||
// Print brand, model, year, mileage, and number of seats
|
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:
|
* Implement Serviceable methods:
|
||||||
|
|||||||
@@ -27,13 +27,58 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
this.chargeLevel = chargeLevel;
|
this.chargeLevel = chargeLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Electric"
|
@Override
|
||||||
|
public String getEnergyType(){
|
||||||
|
return "Electric";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public void charge(double amount) {
|
||||||
// Print battery capacity and charge level
|
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)
|
@Override
|
||||||
// Validate negative
|
public void performService() {
|
||||||
// Cap at 100
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,13 +27,63 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
this.fuelType = fuelType;
|
this.fuelType = fuelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
@Override
|
||||||
|
public String getEnergyType(){
|
||||||
|
return fuelType + " (Gas)";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement refuel(double amount)
|
@Override
|
||||||
// Validate amount
|
public void refuel(double amount) {
|
||||||
// Cap at 100
|
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
|
||||||
// Call super.showDetails()
|
public void performService() {
|
||||||
// Print fuel level and fuel type
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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%");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,21 @@ public class SportsCar extends GasCar {
|
|||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public void showDetails() {
|
||||||
// Print acceleration time
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,32 @@ public abstract class Vehicle {
|
|||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create getters for all fields
|
public String getBrand() {
|
||||||
|
return brand;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement addMileage(double km)
|
public String getModel() {
|
||||||
// If km is negative → print error
|
return model;
|
||||||
// Otherwise increase mileage
|
}
|
||||||
|
|
||||||
// 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() {
|
public void basicInfo() {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -14,9 +17,14 @@ public class VehicleInspector {
|
|||||||
|
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
|
||||||
// TODO:
|
if(v instanceof SportsCar){
|
||||||
// If ElectricCar → print "Check battery system"
|
System.out.println("Check brakes and performance");
|
||||||
// If SportsCar → print "Check brakes and performance"
|
}
|
||||||
// If GasCar → print "Check engine and oil"
|
else if(v instanceof ElectricCar){
|
||||||
|
System.out.println("Check battery system");
|
||||||
|
}
|
||||||
|
else if(v instanceof GasCar){
|
||||||
|
System.out.println("Check engine and oil");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user