[POO] aula08 ex1 refactoring
This commit is contained in:
parent
63c9a7732d
commit
da5808a180
|
@ -1,12 +1,12 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Enums;
|
||||
|
||||
public enum EngineType {
|
||||
FUEL, ELETRIC;
|
||||
FUEL, ELECTRIC;
|
||||
|
||||
public static EngineType fromString(String string) {
|
||||
return switch (string) {
|
||||
case "FUEL", "Fuel", "fuel" -> EngineType.FUEL;
|
||||
case "ELETRIC", "Eletric", "eletric" -> EngineType.ELETRIC;
|
||||
case "ELECTRIC", "Electric", "electric" -> EngineType.ELECTRIC;
|
||||
default -> null;
|
||||
};
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
package aula08.ex1;
|
||||
|
||||
public interface IEletricVehicle {
|
||||
int currentBatteryLvl();
|
||||
void charge(int percentage);
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package aula08.ex1.Interfaces;
|
||||
|
||||
public interface IElectricVehicle {
|
||||
int currentBatteryLvl();
|
||||
void charge(int percentage);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Interfaces;
|
||||
|
||||
public interface IFuelVehicle {
|
||||
int fuelLevel();
|
|
@ -1,4 +1,4 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Interfaces;
|
||||
|
||||
public interface IKmTravelled {
|
||||
void trip(int km);
|
|
@ -1,5 +1,8 @@
|
|||
package aula08.ex1;
|
||||
|
||||
import aula08.ex1.Enums.EngineType;
|
||||
import aula08.ex1.Vehicles.*;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package aula08.ex1;
|
||||
|
||||
import aula08.ex1.Enums.EngineType;
|
||||
import aula08.ex1.Vehicles.*;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class VehicleComp {
|
||||
|
@ -85,7 +88,7 @@ public class VehicleComp {
|
|||
case 2, 3 -> {
|
||||
System.out.print("Car's trunk size: ");
|
||||
int trunkSize = Integer.parseInt(sin.nextLine());
|
||||
System.out.print("What's the engine type? (FUEL/ELETRIC): ");
|
||||
System.out.print("What's the engine type? (FUEL/ELECTRIC): ");
|
||||
EngineType engineType = EngineType.fromString(sin.nextLine());
|
||||
Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize, engineType);
|
||||
if (vehicleType == 2)
|
||||
|
@ -102,7 +105,7 @@ public class VehicleComp {
|
|||
if (vehicleType == 4) {
|
||||
System.out.print("Bus passenger limit: ");
|
||||
int maxPassengers = Integer.parseInt(sin.nextLine());
|
||||
System.out.print("What's the engine type? (FUEL/ELETRIC): ");
|
||||
System.out.print("What's the engine type? (FUEL/ELECTRIC): ");
|
||||
EngineType engineType = EngineType.fromString(sin.nextLine());
|
||||
addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers, engineType));
|
||||
} else {
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Vehicles;
|
||||
|
||||
import aula08.ex1.Enums.EngineType;
|
||||
import aula08.ex1.Interfaces.IElectricVehicle;
|
||||
import aula08.ex1.Interfaces.IFuelVehicle;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Bus extends Vehicle implements IFuelVehicle, IEletricVehicle {
|
||||
public class Bus extends Vehicle implements IFuelVehicle, IElectricVehicle {
|
||||
private final int boardNumber;
|
||||
private final int weight;
|
||||
private final int maxPassengers;
|
||||
|
@ -84,7 +88,7 @@ public class Bus extends Vehicle implements IFuelVehicle, IEletricVehicle {
|
|||
|
||||
@Override
|
||||
public void fillTank(int level) {
|
||||
if (this.engineType == EngineType.ELETRIC)
|
||||
if (this.engineType == EngineType.ELECTRIC)
|
||||
return;
|
||||
this.fuelLevel = level;
|
||||
}
|
|
@ -1,8 +1,12 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Vehicles;
|
||||
|
||||
import aula08.ex1.Enums.EngineType;
|
||||
import aula08.ex1.Interfaces.IElectricVehicle;
|
||||
import aula08.ex1.Interfaces.IFuelVehicle;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Car extends Vehicle implements IEletricVehicle, IFuelVehicle {
|
||||
public class Car extends Vehicle implements IElectricVehicle, IFuelVehicle {
|
||||
private final int boardNumber;
|
||||
private final int trunkSize;
|
||||
private final EngineType engineType;
|
||||
|
@ -84,7 +88,7 @@ public class Car extends Vehicle implements IEletricVehicle, IFuelVehicle {
|
|||
|
||||
@Override
|
||||
public void fillTank(int level) {
|
||||
if (this.engineType == EngineType.ELETRIC)
|
||||
if (this.engineType == EngineType.ELECTRIC)
|
||||
return;
|
||||
this.fuelLevel = level;
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Vehicles;
|
||||
|
||||
import aula08.ex1.Interfaces.IFuelVehicle;
|
||||
|
||||
import java.util.Objects;
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Vehicles;
|
||||
|
||||
import aula08.ex1.Enums.EngineType;
|
||||
|
||||
import java.util.Objects;
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Vehicles;
|
||||
|
||||
import aula08.ex1.Interfaces.IFuelVehicle;
|
||||
|
||||
import java.util.Objects;
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
package aula08.ex1;
|
||||
package aula08.ex1.Vehicles;
|
||||
|
||||
import aula08.ex1.Interfaces.IKmTravelled;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -11,7 +13,7 @@ public abstract class Vehicle implements IKmTravelled {
|
|||
private int lastTripKm;
|
||||
private int km;
|
||||
|
||||
protected Vehicle(String plate, String brand, String model, int potency) {
|
||||
public Vehicle(String plate, String brand, String model, int potency) {
|
||||
this.plate = plate;
|
||||
this.brand = brand;
|
||||
this.model = model;
|
||||
|
@ -20,19 +22,19 @@ public abstract class Vehicle implements IKmTravelled {
|
|||
this.km = 0;
|
||||
}
|
||||
|
||||
protected String getPlate() {
|
||||
public String getPlate() {
|
||||
return this.plate;
|
||||
}
|
||||
|
||||
protected String getBrand() {
|
||||
public String getBrand() {
|
||||
return this.brand;
|
||||
}
|
||||
|
||||
protected String getModel() {
|
||||
public String getModel() {
|
||||
return this.model;
|
||||
}
|
||||
|
||||
protected int getPotency() {
|
||||
public int getPotency() {
|
||||
return this.potency;
|
||||
}
|
||||
|
Loading…
Reference in New Issue