[POO] aula08 ex1 refactoring

This commit is contained in:
TiagoRG 2023-04-07 23:36:02 +01:00
parent 11b885db36
commit a191dc120d
13 changed files with 50 additions and 28 deletions

View File

@ -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;
};
}

View File

@ -1,6 +0,0 @@
package aula08.ex1;
public interface IEletricVehicle {
int currentBatteryLvl();
void charge(int percentage);
}

View File

@ -0,0 +1,6 @@
package aula08.ex1.Interfaces;
public interface IElectricVehicle {
int currentBatteryLvl();
void charge(int percentage);
}

View File

@ -1,4 +1,4 @@
package aula08.ex1;
package aula08.ex1.Interfaces;
public interface IFuelVehicle {
int fuelLevel();

View File

@ -1,4 +1,4 @@
package aula08.ex1;
package aula08.ex1.Interfaces;
public interface IKmTravelled {
void trip(int km);

View File

@ -1,5 +1,8 @@
package aula08.ex1;
import aula08.ex1.Enums.EngineType;
import aula08.ex1.Vehicles.*;
import java.util.Scanner;
public class Main {

View File

@ -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 {

View File

@ -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;
}

View File

@ -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;
}

View File

@ -1,4 +1,6 @@
package aula08.ex1;
package aula08.ex1.Vehicles;
import aula08.ex1.Interfaces.IFuelVehicle;
import java.util.Objects;

View File

@ -1,4 +1,6 @@
package aula08.ex1;
package aula08.ex1.Vehicles;
import aula08.ex1.Enums.EngineType;
import java.util.Objects;

View File

@ -1,4 +1,6 @@
package aula08.ex1;
package aula08.ex1.Vehicles;
import aula08.ex1.Interfaces.IFuelVehicle;
import java.util.Objects;

View File

@ -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;
}