Complete Car project implementation.
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
package org;
|
||||
import org.model.*;
|
||||
import org.behavior.*;
|
||||
import org.util.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
@@ -16,7 +19,9 @@ public class Main {
|
||||
// • Store them in variables such as:
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
|
||||
ElectricCar tesla = new ElectricCar("Tesla", "Model S", 2023, 9000, 5, 100, 85);
|
||||
GasCar toyota = new GasCar("Toyota", "Camry", 2020, 15000, 5, 60, "Gasoline");
|
||||
SportsCar ferrari = new SportsCar("Ferrari", "SF90", 2024, 4000, 2, 70, "Premium", 2.5);
|
||||
|
||||
|
||||
|
||||
@@ -31,6 +36,7 @@ public class Main {
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
Vehicle[] vehicles = {tesla, toyota, ferrari};
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +52,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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -61,7 +70,10 @@ public class Main {
|
||||
// • Demonstrate the use of `instanceof`
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
for (Vehicle v : vehicles){
|
||||
VehicleInspector.inspect(v);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -79,7 +91,7 @@ public class Main {
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
Serviceable[] serviceables = {tesla, toyota, ferrari};
|
||||
|
||||
|
||||
|
||||
@@ -105,6 +117,19 @@ 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.println(v.getBrand() + " " + v.getModel() + " needs service:");
|
||||
s.performService();
|
||||
}
|
||||
else{
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " does not need service.");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -117,10 +142,7 @@ public class Main {
|
||||
// System.out.println("=== Workshop Completed ===");
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,14 +15,18 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
|
||||
private int seats;
|
||||
|
||||
public Car(String brand, String model, int year,
|
||||
double mileage, int seats) {
|
||||
public Car(String brand, String model, int year, 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
|
||||
@@ -38,4 +42,18 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
@Override
|
||||
public void showDetails(){
|
||||
System.out.println("Car Details: " + getYear() + " " + getBrand() + " " + getModel() + " | Mileage: " + getMileage() + " | Seats: " + seats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v){
|
||||
return v.getMileage() > 10_000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,27 +13,58 @@ package org.model;
|
||||
|
||||
import org.behavior.Chargeable;
|
||||
|
||||
public class ElectricCar extends Car implements Chargeable {
|
||||
public class ElectricCar extends Car implements Chargeable{
|
||||
|
||||
private int batteryCapacity;
|
||||
private double chargeLevel;
|
||||
|
||||
public ElectricCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
int batteryCapacity, double chargeLevel) {
|
||||
|
||||
public ElectricCar(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;
|
||||
}
|
||||
|
||||
// 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 + " kWh" + " | Charge Level: " + chargeLevel + "%"
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
@Override
|
||||
public void charge(double amount){
|
||||
if (amount < 0){
|
||||
System.out.println("Error: Cannot charge with negative amount.");
|
||||
return;
|
||||
}
|
||||
|
||||
chargeLevel += amount;
|
||||
|
||||
if (chargeLevel > 100){
|
||||
chargeLevel = 100;
|
||||
}
|
||||
|
||||
System.out.println("Charged. Current charge level: " + chargeLevel + "%");
|
||||
}
|
||||
@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
|
||||
@Override
|
||||
public void refuel(double amount){
|
||||
if (amount < 0){
|
||||
System.out.println("Error: Cannot refuel with negative amount.");
|
||||
return;
|
||||
}
|
||||
|
||||
fuelLevel += amount;
|
||||
|
||||
if (fuelLevel > 100){
|
||||
fuelLevel = 100;
|
||||
}
|
||||
|
||||
System.out.println("Refueled. Current fuel level: " + fuelLevel + "%");
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
@Override
|
||||
public void showDetails(){
|
||||
super.showDetails();
|
||||
System.out.println("Fuel Type: " + fuelType + " | Fuel Level: " + fuelLevel + "%");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,4 +27,19 @@ public class SportsCar extends GasCar {
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
@Override
|
||||
public void showDetails(){
|
||||
super.showDetails();
|
||||
System.out.println("0-100 km/h Acceleration: " + zeroToHundred + " seconds");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 5000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,31 @@ 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: mileage cannot be negative.");
|
||||
return;
|
||||
}
|
||||
this.mileage += km;
|
||||
}
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
|
||||
@@ -18,5 +18,14 @@ 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user