bonus concluded
This commit is contained in:
Generated
+1
-1
@@ -8,7 +8,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_24_PREVIEW" project-jdk-name="corretto-24" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -13,5 +13,18 @@
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>25</source>
|
||||
<target>25</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,4 +1,9 @@
|
||||
package org;
|
||||
import org.model.*;
|
||||
import org.behavior.Serviceable;
|
||||
import org.model.ElectricCar;
|
||||
import org.model.Vehicle;
|
||||
import org.util.VehicleInspector;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
@@ -16,8 +21,20 @@ public class Main {
|
||||
// • Store them in variables such as:
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
ElectricCar tesla = new ElectricCar(
|
||||
"Tesla", "Model S", 2022, 1000,
|
||||
100, 550, 50 // batteryCapacity, range
|
||||
);
|
||||
|
||||
GasCar toyota = new GasCar(
|
||||
"Toyota", "Corolla", 2018, 85000,
|
||||
5, 50, "Petrol" // seats, fuelCapacity, fuelType
|
||||
);
|
||||
|
||||
SportsCar ferrari = new SportsCar(
|
||||
"Ferrari", "F8 Tributo", 2021, 12000,
|
||||
2, 3.9, "oil", 710 // seats, engineSize, horsepower
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +48,7 @@ public class Main {
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
Vehicle[] vehicles ={tesla,toyota,ferrari};
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +63,10 @@ public class Main {
|
||||
// Demonstrate that all subclasses share the same Vehicle
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
for(Vehicle v : vehicles){
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,7 +82,9 @@ public class Main {
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
|
||||
VehicleInspector.inspect(tesla);
|
||||
VehicleInspector.inspect(toyota);
|
||||
VehicleInspector.inspect(ferrari);
|
||||
|
||||
|
||||
|
||||
@@ -79,7 +101,18 @@ public class Main {
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
Serviceable[] serviceables = {tesla,toyota,ferrari };
|
||||
for(Serviceable s : serviceables){
|
||||
Vehicle v = (Vehicle) s;
|
||||
if (s.needsService(v)){
|
||||
System.out.println("Brand: "+v.getBrand()+"Model: "+v.getModel());
|
||||
s.performService();
|
||||
}
|
||||
else{
|
||||
System.out.println("No support");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -118,7 +151,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -20,13 +20,22 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
|
||||
// TODO: Call super constructor
|
||||
this.seats = seats;
|
||||
super(brand,model,year,mileage);
|
||||
}
|
||||
|
||||
// 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 void showDetails(){
|
||||
System.out.println(
|
||||
getYear()+ " " + getBrand() + " " + getModel() +
|
||||
" | Mileage: " + getMileage() +
|
||||
" | Seats: " + seats
|
||||
);
|
||||
}
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
*
|
||||
@@ -38,4 +47,12 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
|
||||
public boolean needsService(Vehicle v){
|
||||
return v.getMileage()>10000;
|
||||
}
|
||||
|
||||
public void performService(Vehicle v){
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,35 @@ 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(){
|
||||
super.showDetails();
|
||||
System.out.println("Battery Capacity: " + batteryCapacity+"Charge Level: "+chargeLevel);
|
||||
}
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
public void charge(double amount) {
|
||||
if(0<amount){
|
||||
this.chargeLevel += amount;
|
||||
if(this.chargeLevel>100)
|
||||
this.chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage()>8000;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Battery system checked");
|
||||
chargeLevel =100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,37 @@ 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){
|
||||
this.fuelLevel += amount;
|
||||
if(this.fuelLevel > 100)
|
||||
this.fuelLevel = 100;
|
||||
}
|
||||
|
||||
}
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println("Fuel Level: " + this.fuelLevel +" "+"Fuel Type: " + this.fuelType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage()>12000;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed");
|
||||
fuelLevel =100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.model;
|
||||
|
||||
import org.behavior.Chargeable;
|
||||
import org.behavior.Refuelable;
|
||||
import org.behavior.Serviceable;
|
||||
|
||||
public class HybridCar extends Car implements Refuelable, Chargeable {
|
||||
private double battery_level;
|
||||
private double fuel_level;
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage()>10000;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed and battery system checked");
|
||||
fuel_level =100;
|
||||
battery_level=100;
|
||||
}
|
||||
public HybridCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
double battery_level, double fuel_level) {
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.battery_level = battery_level;
|
||||
this.fuel_level = fuel_level;
|
||||
}
|
||||
public void charge(double amount) {
|
||||
if(0<amount){
|
||||
this.battery_level += amount;
|
||||
if(this.battery_level>100)
|
||||
this.battery_level = 100;
|
||||
}
|
||||
}
|
||||
public void refuel(double amount) {
|
||||
if(0<amount){
|
||||
this.fuel_level += amount;
|
||||
if(this.fuel_level>100)
|
||||
this.fuel_level = 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,4 +27,18 @@ public class SportsCar extends GasCar {
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
@Override
|
||||
public void showDetails(){
|
||||
super.showDetails();
|
||||
System.out.println("acceleration time: "+zeroToHundred);
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 5000;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,12 +21,32 @@ 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)
|
||||
public void addMileage(double km){
|
||||
if(km<0){
|
||||
System.out.println("error");
|
||||
return;
|
||||
}
|
||||
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,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,19 @@ public class VehicleInspector {
|
||||
// If ElectricCar → print "Check battery system"
|
||||
// If SportsCar → print "Check brakes and performance"
|
||||
// If GasCar → print "Check engine and oil"
|
||||
/*
|
||||
switch (v) {
|
||||
case ElectricCar electricCar -> System.out.println("Check battery system");
|
||||
case SportsCar sportsCar -> System.out.println("Check brakes and performance");
|
||||
case GasCar gasCar -> System.out.println("Check engine and oil");
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if(v instanceof GasCar){System.out.println("Check engine and oil");}
|
||||
else if (v instanceof SportsCar){System.out.println("Check brakes and performance");}
|
||||
else if (v instanceof ElectricCar){System.out.println("Check battery system");}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user