Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d0813ba9f | ||
|
|
79bb92299c | ||
|
|
9182bba222 | ||
|
|
305f719836 | ||
|
|
161187a478 | ||
|
|
cbf0eaf98c | ||
|
|
3f68c299d9 | ||
|
|
c82acadfb3 | ||
|
|
a2d81cd924 |
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_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,5 +1,12 @@
|
||||
package org;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
import org.model.ElectricCar;
|
||||
import org.model.GasCar;
|
||||
import org.model.SportsCar;
|
||||
import org.model.Vehicle;
|
||||
import org.util.VehicleInspector;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -17,8 +24,17 @@ public class Main {
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
|
||||
ElectricCar tesla = new ElectricCar(
|
||||
"Telsa", "Model S", 2022, 1000,
|
||||
4, 500, 80);
|
||||
|
||||
GasCar RR = new GasCar(
|
||||
"Rolls Royce", "Phantom", 2018, 85000,
|
||||
5, 50, "Petrol");
|
||||
|
||||
SportsCar porsche = new SportsCar(
|
||||
"Porsche", "GT3-RS", 2024, 500,
|
||||
2, 70, "Petrol", 3.9);
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -32,7 +48,7 @@ public class Main {
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
|
||||
Vehicle[] vehicles = {tesla, RR, porsche};
|
||||
|
||||
|
||||
|
||||
@@ -47,7 +63,10 @@ public class Main {
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
|
||||
for(Vehicle v : vehicles){
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,7 +81,9 @@ public class Main {
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
|
||||
for(Vehicle v : vehicles){
|
||||
VehicleInspector.inspect(v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -80,7 +101,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
Serviceable[] serviceables = {tesla, RR, porsche};
|
||||
|
||||
|
||||
|
||||
@@ -106,7 +127,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.printf("Brand: %s, Model: %s\n", v.getBrand(), v.getModel());
|
||||
s.performService();
|
||||
System.out.println();
|
||||
}
|
||||
else{
|
||||
System.out.println("The car does not need service\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -118,7 +149,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -19,13 +19,23 @@ 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 void showDetails(){
|
||||
System.out.printf("Brand: %s, Model: %s, Year: %s, Mileage: %s\n",
|
||||
getBrand(), getModel(), getYear(), getMileage());
|
||||
}
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
@@ -38,4 +48,17 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(v.getMileage() > 10000){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,40 @@ 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.printf(", Battery Capacity: %s, Charge Level: %s",
|
||||
batteryCapacity, chargeLevel);
|
||||
}
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
@Override
|
||||
public void charge(double amount) {
|
||||
if(amount < 0){
|
||||
System.out.println("Error: can\'t add negative number!");
|
||||
return;
|
||||
}
|
||||
if(chargeLevel + amount <= 100){
|
||||
chargeLevel += amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return 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
|
||||
@Override
|
||||
public void refuel(double amount) {
|
||||
if(fuelLevel + amount <= 100){
|
||||
fuelLevel += amount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.printf(", Fuel Type: %s, Fuel Level: %s\n",
|
||||
fuelType, fuelLevel);
|
||||
}
|
||||
|
||||
@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,63 @@
|
||||
package org.model;
|
||||
|
||||
import org.behavior.Chargeable;
|
||||
import org.behavior.Refuelable;
|
||||
|
||||
public class HybridCar extends Car implements Refuelable, Chargeable {
|
||||
private double fuelLevel;
|
||||
private String fuelType;
|
||||
private int batteryCapacity;
|
||||
private double chargeLevel;
|
||||
|
||||
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
|
||||
public String getEnergyType() {
|
||||
return "Gas & Electricity";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refuel(double amount) {
|
||||
if(fuelLevel + amount <= 100){
|
||||
fuelLevel += amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void charge(double amount) {
|
||||
if(amount < 0){
|
||||
System.out.println("Error: can\'t add negative number!");
|
||||
return;
|
||||
}
|
||||
if(chargeLevel + amount <= 100){
|
||||
chargeLevel += amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.printf(", Fuel Type: %s, Fuel Level: %s, Battery Capacity: %s, Charge Level: %s\n",
|
||||
fuelType, fuelLevel, batteryCapacity, chargeLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 10000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed and battery system checked");
|
||||
fuelLevel = 100;
|
||||
chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,19 @@ public class SportsCar extends GasCar {
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.printf(", Acceleration Time (0-100): %s", zeroToHundred);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return getMileage() > 5000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,11 +22,30 @@ 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: can\'t add negetive number!");
|
||||
return;
|
||||
}
|
||||
mileage += km;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
/*
|
||||
@@ -16,7 +19,18 @@ public class VehicleInspector {
|
||||
|
||||
// TODO:
|
||||
// If ElectricCar → print "Check battery system"
|
||||
if(v instanceof ElectricCar){
|
||||
System.out.println( "Check battery system");
|
||||
}
|
||||
|
||||
// If SportsCar → print "Check brakes and performance"
|
||||
if(v instanceof SportsCar){
|
||||
System.out.println("Check brakes and performance");
|
||||
}
|
||||
|
||||
// If GasCar → print "Check engine and oil"
|
||||
if(v instanceof GasCar){
|
||||
System.out.println("Check engine and oil");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user