Merge pull request 'WS-03-advanced-oop' (#1) from develope into master
Reviewed-on: #1 100 / 100 + Bonus
This commit was merged in pull request #1.
This commit is contained in:
Generated
+1
@@ -0,0 +1 @@
|
|||||||
|
Main.java
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
package org;
|
package org;
|
||||||
|
import org.model.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -17,9 +20,14 @@ public class Main {
|
|||||||
// ElectricCar tesla = new ElectricCar(...);
|
// ElectricCar tesla = new ElectricCar(...);
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
ElectricCar tesla = new ElectricCar("Tesla", "Roodster",
|
||||||
|
2023, 0, 7, 53, 80);
|
||||||
|
GasCar ferrari= new GasCar("Scuderia Ferrari", "SF90",
|
||||||
|
2024, 100, 4,78,"petrol");
|
||||||
|
SportsCar lamborghini= new SportsCar("lamborghini", "huracan",
|
||||||
|
2026, 20000, 2,72 ,"Petrol",2.6);
|
||||||
|
HybridCar porsche= new HybridCar("porsche", "panamara",
|
||||||
|
2024, 200, 7, 26, 50);
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 2:
|
// TODO 2:
|
||||||
@@ -32,7 +40,7 @@ public class Main {
|
|||||||
// as their parent type (Vehicle).
|
// as their parent type (Vehicle).
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
Vehicle[] vehicle={tesla,ferrari,lamborghini,porsche};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -47,7 +55,11 @@ public class Main {
|
|||||||
// behavior and can call inherited methods.
|
// behavior and can call inherited methods.
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
for (int i=0; i< vehicle.length; i++)
|
||||||
|
{
|
||||||
|
vehicle[i].basicInfo();
|
||||||
|
System.out.println("=======================================");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -62,7 +74,11 @@ public class Main {
|
|||||||
// • Show subclass‑specific inspection messages
|
// • Show subclass‑specific inspection messages
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
for (int i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
vehicle[i].performService();
|
||||||
|
System.out.println("=======================================");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -80,6 +96,7 @@ public class Main {
|
|||||||
//
|
//
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
Serviceable[] serviceables = {tesla, ferrari, lamborghini, porsche};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -107,6 +124,18 @@ public class Main {
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
|
for (Serviceables s: serviceables)
|
||||||
|
{
|
||||||
|
Vehicle v= (Vehicle) s;
|
||||||
|
if (v.needsService())
|
||||||
|
{
|
||||||
|
System.out.println("brand: "+ v.getBrand()+ " model"+ v.getModel());
|
||||||
|
v.performService();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
System.out.println("brand: "+ v.getBrand()+ " model"+ v.getModel()+" does not need service");
|
||||||
|
System.out.println("=======================================");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -118,7 +147,7 @@ public class Main {
|
|||||||
//
|
//
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
System.out.println("=== Workshop Completed ===");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,17 +15,21 @@ public abstract class Car extends Vehicle implements Serviceable {
|
|||||||
|
|
||||||
private int seats;
|
private int seats;
|
||||||
|
|
||||||
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
|
// 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()
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Implement Serviceable methods:
|
* Implement Serviceable methods:
|
||||||
*
|
*
|
||||||
@@ -37,4 +41,21 @@ public abstract class Car extends Vehicle implements Serviceable {
|
|||||||
* performService():
|
* performService():
|
||||||
* - Print: "General car service completed"
|
* - Print: "General car service completed"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService()
|
||||||
|
{
|
||||||
|
if (getMileage()>1000)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void performService();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails()
|
||||||
|
{
|
||||||
|
System.out.println("Car's seats: "+ seats);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,10 +28,32 @@ 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
|
||||||
|
public void showDetails()
|
||||||
|
{
|
||||||
|
System.out.println("batteryCapacity: "+ batteryCapacity+ " chargeLevel: "+ chargeLevel);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement charge(double amount)
|
// TODO: Implement charge(double amount)
|
||||||
// Validate negative
|
public void performService()
|
||||||
// Cap at 100
|
{
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
chargeLevel=100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService()
|
||||||
|
{
|
||||||
|
if (getMileage()>8000)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,10 +28,32 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||||
|
@Override
|
||||||
|
public String getEnergyType()
|
||||||
|
{
|
||||||
|
return "Gas";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement refuel(double amount)
|
// TODO: Implement refuel(double amount)
|
||||||
// Validate amount
|
public void performService()
|
||||||
// Cap at 100
|
{
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
fuelLevel=100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService()
|
||||||
|
{
|
||||||
|
if (getMileage()>12000)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
|
@Override
|
||||||
|
public void showDetails()
|
||||||
|
{
|
||||||
|
System.out.println("fuelType: "+fuelType+" fuelLevel: "+fuelLevel);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package org.model;
|
||||||
|
|
||||||
|
import org.behavior.Chargeable;
|
||||||
|
|
||||||
|
public class HybridCar extends Car implements Chargeable {
|
||||||
|
private int batteryCapacity;
|
||||||
|
private double chargeLevel;
|
||||||
|
|
||||||
|
public HybridCar(String brand, String model, int year,
|
||||||
|
double mileage, int seats,
|
||||||
|
int batteryCapacity, double chargeLevel) {
|
||||||
|
|
||||||
|
super(brand, model, year, mileage, seats);
|
||||||
|
this.batteryCapacity = batteryCapacity;
|
||||||
|
this.chargeLevel = chargeLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEnergyType()
|
||||||
|
{
|
||||||
|
return "Hybrid";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService()
|
||||||
|
{
|
||||||
|
if (getMileage()>24000)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
chargeLevel=100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails()
|
||||||
|
{
|
||||||
|
System.out.println("batteryCapacity: "+ batteryCapacity+ " chargeLevel: "+ chargeLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,8 +24,29 @@ public class SportsCar extends GasCar {
|
|||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void performService()
|
||||||
|
{
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService()
|
||||||
|
{
|
||||||
|
if (getMileage()>5000)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Print header
|
// Print header
|
||||||
// Call super.showDetails()
|
// Call super.showDetails()
|
||||||
// Print acceleration time
|
// Print acceleration time
|
||||||
|
@Override
|
||||||
|
public void showDetails()
|
||||||
|
{
|
||||||
|
System.out.println("sport car");
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(zeroToHundred);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,12 +21,35 @@ 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 double addMileage(double km)
|
||||||
|
{
|
||||||
|
if (km<0)
|
||||||
|
{
|
||||||
|
System.out.println("error");
|
||||||
|
return mileage;
|
||||||
|
}
|
||||||
|
mileage+=km*0.621371;
|
||||||
|
return mileage;
|
||||||
|
}
|
||||||
|
|
||||||
// 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(
|
||||||
@@ -37,4 +60,6 @@ public abstract class Vehicle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public abstract void showDetails();
|
public abstract void showDetails();
|
||||||
|
public abstract void performService();
|
||||||
|
public abstract boolean needsService();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user