Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cb8f9a8d8 | ||
|
|
4e9b7eb7ea | ||
|
|
7eefa807a3 | ||
|
|
22fc28130e |
@@ -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,7 +118,14 @@ 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
|
||||||
- (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
|
## Workflow
|
||||||
1. Fork the repository to your own Git account
|
1. Fork the repository to your own Git account
|
||||||
@@ -133,6 +140,6 @@ git checkout -b develop
|
|||||||
```
|
```
|
||||||
git push origin 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! :)
|
# GOOD LUCK! :)
|
||||||
|
|||||||
+14
-38
@@ -1,12 +1,5 @@
|
|||||||
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) {
|
||||||
|
|
||||||
@@ -24,15 +17,8 @@ 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) ;
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -46,7 +32,8 @@ public class Main {
|
|||||||
// as their parent type (Vehicle).
|
// as their parent type (Vehicle).
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
Vehicle[] vehicle = {chevrolet, toyota, lamborghini};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
@@ -60,11 +47,9 @@ 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:
|
||||||
@@ -77,11 +62,9 @@ public class Main {
|
|||||||
// • Show subclass‑specific inspection messages
|
// • Show subclass‑specific inspection messages
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
for(Vehicle v : vehicle)
|
|
||||||
{
|
|
||||||
VehicleInspector.inspect(v) ;
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 5:
|
// TODO 5:
|
||||||
@@ -97,7 +80,9 @@ public class Main {
|
|||||||
//
|
//
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
Serviceable[] serviceables = {chevrolet, toyota, lamborghini};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 6:
|
// TODO 6:
|
||||||
@@ -121,17 +106,6 @@ 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 :)");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -144,7 +118,9 @@ public class Main {
|
|||||||
//
|
//
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
System.out.println("=== Workshop Completed ===");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,20 +19,13 @@ 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
|
// Print brand, model, year, mileage, and number of seats
|
||||||
public abstract void showDetails();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Implement Serviceable methods:
|
* Implement Serviceable methods:
|
||||||
@@ -45,18 +38,4 @@ 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 ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,57 +28,12 @@ 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
|
// Call super.showDetails()
|
||||||
public void showDetails() {}
|
// Print battery capacity and charge level
|
||||||
|
|
||||||
// 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%");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,57 +28,12 @@ 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
|
// Call super.showDetails()
|
||||||
public void showDetails() {}
|
// Print fuel level and fuel type
|
||||||
|
|
||||||
@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%");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,27 +25,6 @@ public class SportsCar extends GasCar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// 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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,41 +21,13 @@ 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,9 +1,6 @@
|
|||||||
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;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -21,11 +18,5 @@ 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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user