[Ap-workshop] - Final Submission for Review #1
Generated
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
+107
-84
@@ -1,125 +1,148 @@
|
||||
package org;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
import org.model.*;
|
||||
import org.util.VehicleInspector;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// ============================================================
|
||||
// TODO 1:
|
||||
// Create one instance of each vehicle type:
|
||||
// - ElectricCar
|
||||
// - GasCar
|
||||
// - SportsCar
|
||||
//
|
||||
// Requirements:
|
||||
// • Use the constructors you created in each class.
|
||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
||||
// • Store them in variables such as:
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
|
||||
ElectricCar tesla = new ElectricCar("tesla", "Model 3", 2023, 1000, 5, 75, 90.0);
|
||||
|
||||
GasCar Toyota = new GasCar("Toyota", "Corolla", 2021, 15000, 5, 80, "gasoline");
|
||||
|
||||
SportsCar DodgeDemon = new SportsCar("Dodge", "Challenger Demon", 2018, 5000, 5, 70, "gasoline", 2.3);
|
||||
|
||||
HybridCar Lamborghini = new HybridCar("Lamborghini", "Aventador J (Hybrid)",2024, 1200, 2, 25.0, 60.0);
|
||||
|
||||
// ============================================================
|
||||
// TODO 2:
|
||||
// Create an array of Vehicle:
|
||||
// Vehicle[] vehicles = { ... };
|
||||
//
|
||||
// This array MUST contain the objects created above.
|
||||
// This demonstrates **polymorphism** because all different
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
Vehicle[] vehicles = {tesla, Toyota, DodgeDemon, Lamborghini};
|
||||
Serviceable[] serviceable = {tesla, Toyota, DodgeDemon, Lamborghini};
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println(" ======= menu ======= ");
|
||||
System.out.println("choose one option: ");
|
||||
System.out.println("1.show all basic info");
|
||||
System.out.println("2.show details");
|
||||
System.out.println("3.inspect all cars");
|
||||
System.out.println("4.servise the cars");
|
||||
System.out.println("5.incease mileage for a car");
|
||||
System.out.println("6.refeul a car");
|
||||
System.out.println("7.exit");
|
||||
|
||||
int input = scanner.nextInt();
|
||||
|
||||
// ============================================================
|
||||
// TODO 3:
|
||||
// Loop through the Vehicle[] array and:
|
||||
// • Call v.basicInfo();
|
||||
// • Print a blank line after each.
|
||||
//
|
||||
// Purpose:
|
||||
// Demonstrate that all subclasses share the same Vehicle
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
if (input == 1) {
|
||||
for (Vehicle V : vehicles) {
|
||||
|
||||
V.basicInfo();
|
||||
System.out.println("---------------------------------------");
|
||||
|
||||
}
|
||||
|
||||
} else if (input == 2) {
|
||||
|
||||
for (Vehicle V : vehicles) {
|
||||
|
||||
// ============================================================
|
||||
// TODO 4:
|
||||
// Use VehicleInspector to inspect each vehicle:
|
||||
//
|
||||
// VehicleInspector.inspect(v);
|
||||
//
|
||||
// Purpose:
|
||||
// • Demonstrate the use of `instanceof`
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
V.showDetails();
|
||||
System.out.println("---------------------------------------");
|
||||
|
||||
}
|
||||
} else if (input == 3) {
|
||||
for (Vehicle V : vehicles) {
|
||||
|
||||
VehicleInspector.inspect(V);
|
||||
System.out.println("---------------------------------------");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else if (input == 4) {
|
||||
for (Serviceable s : serviceable) {
|
||||
Vehicle v = (Vehicle) s;
|
||||
if (s.needsService(v)) {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " needs service.");
|
||||
s.performService();
|
||||
} else {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " does not need service.");
|
||||
}
|
||||
System.out.println("---------------------------------------");
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// TODO 5:
|
||||
// Create an array of Serviceable:
|
||||
// Serviceable[] serviceables = { ... };
|
||||
//
|
||||
// This demonstrates **interface‑based polymorphism**.
|
||||
// Every Car subtype implements Serviceable.
|
||||
//
|
||||
// IMPORTANT:
|
||||
// • You must cast to Vehicle when needed:
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
}
|
||||
else if (input == 5) {
|
||||
|
||||
System.out.println(" ====== add mileage ====== ");
|
||||
System.out.println("choose a car: (its brand)");
|
||||
scanner.nextLine();
|
||||
String myCar = scanner.nextLine();
|
||||
|
||||
System.out.println("Enter the distance: ");
|
||||
double D = scanner.nextDouble();
|
||||
|
||||
if (Objects.equals(myCar, "tesla")) {
|
||||
|
||||
tesla.addMileage(D);
|
||||
|
||||
// ============================================================
|
||||
// TODO 6:
|
||||
// Loop through the Serviceable[] array:
|
||||
//
|
||||
// Steps inside loop:
|
||||
//
|
||||
// 1. Cast the current object to Vehicle:
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// 2. Check:
|
||||
// if (s.needsService(v)) { ... }
|
||||
//
|
||||
// 3. If true:
|
||||
// - Print a message showing brand + model
|
||||
// - Call s.performService();
|
||||
//
|
||||
// 4. Otherwise:
|
||||
// - Print that the car does not need service
|
||||
//
|
||||
// Add blank lines between each item for readability.
|
||||
// ============================================================
|
||||
} else if (Objects.equals(myCar, "Toyota")) {
|
||||
|
||||
Toyota.addMileage(D);
|
||||
|
||||
} else if (Objects.equals(myCar, "Dodge")) {
|
||||
|
||||
DodgeDemon.addMileage(D);
|
||||
} else if (Objects.equals(myCar, "Lamborghini")) {
|
||||
Lamborghini.addMileage(D);
|
||||
} else {
|
||||
System.out.println("invalid name for a car");
|
||||
}
|
||||
|
||||
} else if (input == 6) {
|
||||
System.out.println(" ====== refuel ====== ");
|
||||
System.out.println("choose a car: (its brand)");
|
||||
scanner.nextLine();
|
||||
String myCar = scanner.nextLine();
|
||||
|
||||
// ============================================================
|
||||
// TODO 7 (Optional):
|
||||
// After everything is done, print a final message:
|
||||
//
|
||||
// System.out.println("=== Workshop Completed ===");
|
||||
//
|
||||
// ============================================================
|
||||
System.out.println("Enter the amount of fuel: ");
|
||||
double D = scanner.nextDouble();
|
||||
|
||||
if (Objects.equals(myCar, "tesla")) {
|
||||
|
||||
tesla.charge(D);
|
||||
|
||||
} else if (Objects.equals(myCar, "Toyota")) {
|
||||
|
||||
Toyota.refuel(D);
|
||||
|
||||
} else if (Objects.equals(myCar, "Dodge")) {
|
||||
|
||||
DodgeDemon.refuel(D);
|
||||
} else if (Objects.equals(myCar, "Lamborghini")) {
|
||||
|
||||
System.out.println("choose one: 1.gasoline or 2.electric");
|
||||
int n = scanner.nextInt();
|
||||
if (n == 1)
|
||||
Lamborghini.refuel(D);
|
||||
else if (n == 2)
|
||||
Lamborghini.charge(D);
|
||||
|
||||
} else {
|
||||
System.out.println("invalid name for a car");
|
||||
}
|
||||
|
||||
} else if (input == 7) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -19,14 +19,32 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
double mileage, int seats) {
|
||||
|
||||
// TODO: Call super constructor
|
||||
super(brand, model, year, mileage);
|
||||
this.seats = seats;
|
||||
}
|
||||
|
||||
public int getSeats() {
|
||||
return 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.println(getYear() + " " + getBrand() + " " + getModel() +
|
||||
" | mileage: " + getMileage() +
|
||||
" | seats: " + getSeats());
|
||||
}
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
*
|
||||
@@ -38,4 +56,16 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
|
||||
return v.getMileage() > 10000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,53 @@ 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 (amount < 0)
|
||||
System.out.println("This amount is unacceptable.");
|
||||
else {
|
||||
chargeLevel += amount;
|
||||
if (chargeLevel >= 100) {
|
||||
chargeLevel = 100;
|
||||
System.out.println("Battery fully charged.");
|
||||
}
|
||||
else
|
||||
System.out.println("charge level increased.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return super.needsService(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
|
||||
super.performService();
|
||||
System.out.println("Battery system checked");
|
||||
this.chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,55 @@ 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 (amount < 0)
|
||||
System.out.println("This amount is unacceptable.");
|
||||
if (fuelLevel == 100)
|
||||
System.out.println("fuel level is already 100%.");
|
||||
else {
|
||||
fuelLevel += amount;
|
||||
if (fuelLevel >= 100) {
|
||||
fuelLevel = 100;
|
||||
System.out.println("fuel level reached 100%.");
|
||||
} else
|
||||
System.out.println("fuel level increased.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println(" | fuelLevel: " + fuelLevel + " | fuel type: " + fuelType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return super.needsService(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
super.performService();
|
||||
System.out.println("engine and oil checked");
|
||||
this.fuelLevel = 100;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.model;
|
||||
|
||||
import org.behavior.Chargeable;
|
||||
import org.behavior.Refuelable;
|
||||
|
||||
public class HybridCar extends Car implements Refuelable, Chargeable {
|
||||
|
||||
private double fuelLevel;
|
||||
private double chargeLevel;
|
||||
|
||||
public HybridCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, double chargeLevel) {
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.fuelLevel = fuelLevel;
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getEnergyType() {
|
||||
return "Hybrid (Gas + Gasoline)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println(" | fuel level: " + fuelLevel + " | charge level: " + chargeLevel);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refuel(double amount) {
|
||||
if (amount < 0)
|
||||
System.out.println("This amount is unacceptable.");
|
||||
if (fuelLevel == 100)
|
||||
System.out.println("fuel level is already 100%.");
|
||||
else {
|
||||
fuelLevel += amount;
|
||||
if (fuelLevel >= 100) {
|
||||
fuelLevel = 100;
|
||||
System.out.println("fuel level reached 100%.");
|
||||
} else
|
||||
System.out.println("fuel level increased.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void charge(double amount) {
|
||||
if (amount < 0)
|
||||
System.out.println("This amount is unacceptable.");
|
||||
else {
|
||||
chargeLevel += amount;
|
||||
if (chargeLevel >= 100) {
|
||||
chargeLevel = 100;
|
||||
System.out.println("Battery fully charged.");
|
||||
}
|
||||
else
|
||||
System.out.println("charge level increased.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return super.needsService(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
super.performService();
|
||||
System.out.println("Engine oil changed and battery system checked");
|
||||
this.fuelLevel = 100;
|
||||
this.chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ package org.model;
|
||||
|
||||
public class SportsCar extends GasCar {
|
||||
|
||||
|
||||
private double zeroToHundred;
|
||||
|
||||
public SportsCar(String brand, String model, int year,
|
||||
@@ -27,4 +28,23 @@ 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 boolean needsService(Vehicle v) {
|
||||
return super.needsService(v);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
super.performService();
|
||||
System.out.println("brakes and performance checked");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,10 +22,37 @@ 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("This amount is not acceptable.");
|
||||
else {
|
||||
mileage += km;
|
||||
System.out.println("mileage increased");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
|
||||
public void basicInfo() {
|
||||
@@ -37,4 +64,5 @@ public abstract class Vehicle {
|
||||
}
|
||||
|
||||
public abstract void showDetails();
|
||||
public abstract String getEnergyType();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.Vehicle;
|
||||
import org.model.*;
|
||||
|
||||
/*
|
||||
* Utility class to inspect vehicles.
|
||||
@@ -18,5 +18,21 @@ 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 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");
|
||||
} else if (v instanceof HybridCar) {
|
||||
|
||||
System.out.println("check engine oil and battery system");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user