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