Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ae0cdb95d |
@@ -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,14 +118,7 @@ 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
|
||||
- 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
|
||||
```
|
||||
- (Optional) Override methods to manage both power sources (driving drains battery first, then gas)
|
||||
|
||||
## Workflow
|
||||
1. Fork the repository to your own Git account
|
||||
@@ -140,6 +133,6 @@ git checkout -b 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! :)
|
||||
|
||||
+38
-14
@@ -1,5 +1,12 @@
|
||||
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 static void main(String[] args) {
|
||||
|
||||
@@ -17,8 +24,15 @@ public class Main {
|
||||
// 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) ;
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -32,8 +46,7 @@ public class Main {
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
Vehicle[] vehicle = {chevrolet, toyota, lamborghini};
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -47,9 +60,11 @@ public class Main {
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
for (Vehicle v : vehicle)
|
||||
{
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// TODO 4:
|
||||
@@ -62,9 +77,11 @@ public class Main {
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
for(Vehicle v : vehicle)
|
||||
{
|
||||
VehicleInspector.inspect(v) ;
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// TODO 5:
|
||||
@@ -80,9 +97,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
Serviceable[] serviceables = {chevrolet, toyota, lamborghini};
|
||||
|
||||
// ============================================================
|
||||
// TODO 6:
|
||||
@@ -106,6 +121,17 @@ public class Main {
|
||||
// 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 :)");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -118,9 +144,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,20 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
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()
|
||||
// Print brand, model, year, mileage, and number of seats
|
||||
@Override
|
||||
public abstract void showDetails();
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
@@ -38,4 +45,18 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - 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,12 +28,57 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
}
|
||||
|
||||
// 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() {}
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// 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,12 +28,57 @@ public class GasCar extends Car implements Refuelable {
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
@Override
|
||||
public String getEnergyType()
|
||||
{
|
||||
return fuelType+" (Gas)" ;
|
||||
}
|
||||
|
||||
// TODO: Implement refuel(double amount)
|
||||
// Validate amount
|
||||
// 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()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
@Override
|
||||
public void showDetails() {}
|
||||
|
||||
@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,6 +25,27 @@ public class SportsCar extends GasCar {
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Print header
|
||||
// Call super.showDetails()
|
||||
// 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,13 +21,41 @@ public abstract class Vehicle {
|
||||
}
|
||||
|
||||
// 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)
|
||||
// If km is negative → print error
|
||||
// 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()
|
||||
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
year + " " + brand + " " + model +
|
||||
|
||||
@@ -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;
|
||||
|
||||
/*
|
||||
@@ -18,5 +21,11 @@ public class VehicleInspector {
|
||||
// If ElectricCar → print "Check battery system"
|
||||
// If SportsCar → print "Check brakes and performance"
|
||||
// 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