complete to do codes #1

Open
avasanatkar wants to merge 1 commits from develop into master
7 changed files with 172 additions and 1 deletions
+37 -1
View File
@@ -1,4 +1,12 @@
package org;
import org.model.Vehicle;
import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.behavior.Serviceable;
import org.util.VehicleInspector;
public class Main {
public static void main(String[] args) {
@@ -16,6 +24,9 @@ public class Main {
// • Store them in variables such as:
// ElectricCar tesla = new ElectricCar(...);
// ============================================================
ElectricCar tesla = new ElectricCar("Tesla", "Model S", 2023, 8500, 5, 100, 90);
GasCar toyota = new GasCar("Toyota", "Corolla", 2020, 13000, 5, 60, "Petrol");
SportsCar ferrari = new SportsCar("Ferrari", "F8", 2022, 5200, 2, 70, "Premium Gasoline", 2.9);
@@ -31,6 +42,7 @@ public class Main {
// subclasses (ElectricCar, GasCar, SportsCar) are stored
// as their parent type (Vehicle).
// ============================================================
Vehicle[] vehicles = { tesla, toyota, ferrari };
@@ -46,6 +58,11 @@ public class Main {
// Demonstrate that all subclasses share the same Vehicle
// behavior and can call inherited methods.
// ============================================================
System.out.println("=== Vehicle Info ===");
for (Vehicle v : vehicles) {
v.basicInfo();
System.out.println();
}
@@ -61,6 +78,11 @@ public class Main {
// • Demonstrate the use of `instanceof`
// • Show subclassspecific inspection messages
// ============================================================
System.out.println("=== Vehicle Inspection ===");
for (Vehicle v : vehicles) {
VehicleInspector.inspect(v);
System.out.println();
}
@@ -79,6 +101,7 @@ public class Main {
// Vehicle v = (Vehicle) s;
//
// ============================================================
Serviceable[] serviceables = { tesla, toyota, ferrari };
@@ -105,6 +128,19 @@ public class Main {
//
// Add blank lines between each item for readability.
// ============================================================
System.out.println("=== Vehicle Service ===");
for (Serviceable s : serviceables) {
Vehicle v = (Vehicle) s;
if (s.needsService(v)) {
System.out.println("Servicing " + v.getBrand() + " " + v.getModel() + "...");
s.performService();
} else {
System.out.println(v.getBrand() + " " + v.getModel() + " does not need service.");
}
System.out.println();
}
@@ -117,7 +153,7 @@ public class Main {
// System.out.println("=== Workshop Completed ===");
//
// ============================================================
System.out.println("=== Workshop Completed ===");
+22
View File
@@ -19,10 +19,15 @@ 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
@@ -38,4 +43,21 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService():
* - Print: "General car service completed"
*/
@Override
public void showDetails() {
System.out.println("Brand: " + getBrand());
System.out.println("Model: " + getModel());
System.out.println("Year: " + getYear());
System.out.println("Mileage: " + getMileage());
System.out.println("Seats: " + seats);
}
@Override
public boolean needsService(Vehicle v) {
return getMileage() > 10000;
}
@Override
public void performService() {
System.out.println("General car service completed");
}
}
+25
View File
@@ -28,12 +28,37 @@ 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 + " kWh");
System.out.println("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("Charge amount cannot be negative.");
return;
}
chargeLevel += amount;
if (chargeLevel > 100) {
chargeLevel = 100;
}
System.out.println("Car charged. Current charge level: " + chargeLevel + "%");
}
}
+36
View File
@@ -28,12 +28,48 @@ 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("Fuel amount cannot be negative.");
return;
}
fuelLevel += amount;
if (fuelLevel > 100) {
fuelLevel = 100;
}
System.out.println("Car 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 Level: " + fuelLevel + "%");
System.out.println("Fuel Type: " + fuelType);
}
@Override
public boolean needsService(Vehicle v) {
return getMileage() > 12000;
}
@Override
public void performService() {
System.out.println("Engine oil changed");
fuelLevel = 100;
}
}
+15
View File
@@ -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 getMileage() > 5000;
}
@Override
public void performService() {
System.out.println("High-performance brake system checked");
}
}
+23
View File
@@ -21,12 +21,35 @@ 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("Mileage cannot be negative.");
return;
}
mileage += km;
}
// TODO: Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() {
System.out.println(
@@ -1,4 +1,8 @@
package org.util;
import org.model.Vehicle;
import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.*;
import org.model.Vehicle;
@@ -18,5 +22,15 @@ 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");
}
}
}