develop #1
@@ -1,37 +0,0 @@
|
||||
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;
|
||||
|
||||
public class GasCar extends Car implements Refuelable {
|
||||
|
||||
private double fuelLevel;
|
||||
private String fuelType;
|
||||
|
||||
public GasCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
double fuelLevel, String fuelType) {
|
||||
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.fuelLevel = fuelLevel;
|
||||
this.fuelType = fuelType;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
|
||||
// TODO: Implement refuel(double amount)
|
||||
// Validate amount
|
||||
// Cap at 100
|
||||
|
||||
// TODO: Override showDetails()
|
||||
}
|
||||
@@ -1,4 +1,10 @@
|
||||
package org;
|
||||
import org.model.*;
|
||||
import org.util.VehicleInspector;
|
||||
import org.model.Vehicle;
|
||||
import org.util.VehicleInspector;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
@@ -84,6 +90,10 @@ public class Main {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 6:
|
||||
// Loop through the Serviceable[] array:
|
||||
@@ -118,9 +128,43 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
Serviceable tesla = new ElectricCar("Tesla", "Model 3", 2023, 5000, 5, 75, 80);
|
||||
Serviceable honda = new GasCar("Honda", "Civic", 2022, 15000, 5, 50, "Gasoline");
|
||||
Serviceable porsche = new SportsCar("Porsche", "911", 2023, 2000, 2, 60, "Gasoline", 3.5);
|
||||
|
||||
|
||||
Vehicle[] vehicles = {(Vehicle) tesla, (Vehicle) honda, (Vehicle) porsche};
|
||||
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
|
||||
VehicleInspector.inspect(v);
|
||||
}
|
||||
|
||||
|
||||
Serviceable[] serviceables = { tesla, honda, porsche };
|
||||
|
||||
|
||||
for (Serviceable s : serviceables) {
|
||||
Vehicle v = (Vehicle) s;
|
||||
if (s.needsService(v)) {
|
||||
System.out.println(v.GetBrand() + " " + v.GetBrand() + " needs service.");
|
||||
s.performService();
|
||||
} else {
|
||||
System.out.println(v.GetBrand() + " " + v.GetModel() + " does not need service.");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ package org.model;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
|
||||
public abstract class Car extends Vehicle implements Serviceable {
|
||||
public abstract class Car extends org.model.Vehicle implements Serviceable {
|
||||
|
||||
private int seats;
|
||||
|
||||
@@ -19,12 +19,28 @@ 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()
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
System.out.println("brand : " + GetBrand()) ;
|
||||
System.out.println("model : " + GetModel() ) ;
|
||||
System.out.println("year : " + GetYear() ) ;
|
||||
System.out.println("mileage : " + GetMileage()) ;
|
||||
System.out.println("seats : " + seats) ;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
@@ -37,4 +53,16 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
@Override
|
||||
public boolean needsService(org.model.Vehicle v)
|
||||
{
|
||||
return this.GetMileage() > 10000 ;
|
||||
}
|
||||
|
||||
public abstract boolean needService(org.model.Vehicle v);
|
||||
|
||||
@Override public void performService(){
|
||||
System.out.println("General car service completed") ;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,11 +26,49 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
this.batteryCapacity = batteryCapacity;
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
@Override
|
||||
|
||||
// TODO: Override getEnergyType() → return "Electric"
|
||||
public String getEnergyType()
|
||||
{
|
||||
return "Electric" ;
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails() ;
|
||||
System.out.println("battry capacity : " + batteryCapacity) ;
|
||||
System.out.println("charge level : " + chargeLevel) ;
|
||||
}
|
||||
@Override
|
||||
public void charge(double amount)
|
||||
{
|
||||
if (amount < 0 )
|
||||
{
|
||||
System.out.println("No") ;
|
||||
}
|
||||
else
|
||||
{
|
||||
chargeLevel = chargeLevel + amount ;
|
||||
if (chargeLevel > 100)
|
||||
{
|
||||
chargeLevel = 100 ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean needService(org.model.Vehicle v)
|
||||
{
|
||||
return (this.GetMileage()> 8000);
|
||||
}
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Battery system checked") ;
|
||||
chargeLevel = 100 ;
|
||||
}
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
@@ -0,0 +1,81 @@
|
||||
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;
|
||||
|
||||
public class GasCar extends Car implements Refuelable {
|
||||
|
||||
private double fuelLevel;
|
||||
private String fuelType;
|
||||
|
||||
public GasCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
double fuelLevel, String fuelType) {
|
||||
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.fuelLevel = fuelLevel;
|
||||
this.fuelType = fuelType;
|
||||
}
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return this.GetMileage()> 12000 ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needService(Vehicle v) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Engine oil changed") ;
|
||||
fuelLevel = 100 ;
|
||||
}
|
||||
@Override
|
||||
public String getEnergyType()
|
||||
{
|
||||
return fuelType + " (Gas)" ;
|
||||
}
|
||||
@Override
|
||||
public void refuel(double amount)
|
||||
{
|
||||
if (amount < 0)
|
||||
{
|
||||
System.out.println("no ! ") ;
|
||||
}
|
||||
else
|
||||
{
|
||||
fuelLevel = fuelLevel + amount ;
|
||||
if (fuelLevel > 100)
|
||||
fuelLevel = 100 ;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails() ;
|
||||
System.out.println("fuel level : " + fuelLevel) ;
|
||||
System.out.println("fuel type : " + fuelType) ;
|
||||
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
|
||||
// TODO: Implement refuel(double amount)
|
||||
// Validate amount
|
||||
// Cap at 100
|
||||
|
||||
// TODO: Override showDetails()
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package org.model;
|
||||
import org.behavior.Chargeable;
|
||||
import org.behavior.Refuelable;
|
||||
import org.model.Vehicle;
|
||||
|
||||
public class HybridCar extends Car implements Chargeable , Refuelable {
|
||||
|
||||
private int batteryCapacity;
|
||||
private double chargeLevel;
|
||||
private double fuelLevel;
|
||||
private String fuelType;
|
||||
|
||||
public HybridCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
double fuelLevel, String fuelType, int batteryCapacity, double chargeLevel) {
|
||||
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.fuelLevel = fuelLevel;
|
||||
this.fuelType = fuelType;
|
||||
this.batteryCapacity = batteryCapacity;
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
@Override
|
||||
|
||||
// TODO: Override getEnergyType() → return "Electric"
|
||||
public String getEnergyType()
|
||||
{
|
||||
return "Electric + fuel : " + fuelType ;
|
||||
}
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails() ;
|
||||
System.out.println("battry capacity : " + batteryCapacity) ;
|
||||
System.out.println("charge level : " + chargeLevel) ;
|
||||
System.out.println("fuel level : " + fuelLevel) ;
|
||||
System.out.println("fuel type : " + fuelType) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needService(Vehicle v) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void charge(double amount)
|
||||
{
|
||||
if (amount < 0 )
|
||||
{
|
||||
System.out.println("No") ;
|
||||
}
|
||||
else
|
||||
{
|
||||
chargeLevel = chargeLevel + amount ;
|
||||
if (chargeLevel > 100)
|
||||
{
|
||||
chargeLevel = 100 ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void refuel(double amount)
|
||||
{
|
||||
if (amount < 0)
|
||||
{
|
||||
System.out.println("no ! ") ;
|
||||
}
|
||||
else
|
||||
{
|
||||
fuelLevel = fuelLevel + amount ;
|
||||
if (fuelLevel > 100)
|
||||
fuelLevel = 100 ;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return this.GetMileage() > 10000 ;
|
||||
}
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Battery system checked") ;
|
||||
chargeLevel = 100 ;
|
||||
System.out.println("Engine oil changed") ;
|
||||
fuelLevel = 100 ;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ package org.model;
|
||||
* Print "High-performance brake system checked"
|
||||
*/
|
||||
|
||||
public class SportsCar extends GasCar {
|
||||
public class SportsCar extends org.model.GasCar {
|
||||
|
||||
private double zeroToHundred;
|
||||
|
||||
@@ -23,6 +23,27 @@ public class SportsCar extends GasCar {
|
||||
|
||||
this.zeroToHundred = zeroToHundred;
|
||||
}
|
||||
@Override
|
||||
public boolean needsService(org.model.Vehicle v)
|
||||
{
|
||||
return this.GetMileage() > 5000 ;
|
||||
}
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("High-performance brake system checked") ;
|
||||
|
||||
}
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
System.out.println("*details*");
|
||||
super.showDetails() ;
|
||||
System.out.println("0 --> 100 " + zeroToHundred) ;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Print header
|
||||
@@ -21,12 +21,39 @@ public abstract class Vehicle {
|
||||
}
|
||||
|
||||
// TODO: Create getters for all fields
|
||||
|
||||
public String GetBrand()
|
||||
{
|
||||
return this.brand ;
|
||||
}
|
||||
public String GetModel()
|
||||
{
|
||||
return this.model ;
|
||||
}
|
||||
public int GetYear()
|
||||
{
|
||||
return this.year ;
|
||||
}
|
||||
public double GetMileage()
|
||||
{
|
||||
return this.mileage ;
|
||||
}
|
||||
// TODO: Implement addMileage(double km)
|
||||
public void AddMileage(double km)
|
||||
{
|
||||
if (km < 0 )
|
||||
{
|
||||
System.out.println("error !") ;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.mileage = this.mileage + km ;
|
||||
}
|
||||
}
|
||||
// If km is negative → print error
|
||||
// Otherwise increase mileage
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.*;
|
||||
import org.model.Vehicle;
|
||||
|
||||
/*
|
||||
@@ -13,6 +13,18 @@ public class VehicleInspector {
|
||||
public static void inspect(Vehicle v) {
|
||||
|
||||
v.basicInfo();
|
||||
if (v instanceof ElectricCar)
|
||||
{
|
||||
System.out.println("Check battery 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") ;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// If ElectricCar → print "Check battery system"
|
||||
Reference in New Issue
Block a user