[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 { public enum EngineType {
FUEL, ELETRIC; FUEL, ELECTRIC;
public static EngineType fromString(String string) { public static EngineType fromString(String string) {
return switch (string) { return switch (string) {
case "FUEL", "Fuel", "fuel" -> EngineType.FUEL; case "FUEL", "Fuel", "fuel" -> EngineType.FUEL;
case "ELETRIC", "Eletric", "eletric" -> EngineType.ELETRIC; case "ELECTRIC", "Electric", "electric" -> EngineType.ELECTRIC;
default -> null; 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 { public interface IFuelVehicle {
int fuelLevel(); int fuelLevel();

View File

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

View File

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

View File

@ -1,5 +1,8 @@
package aula08.ex1; package aula08.ex1;
import aula08.ex1.Enums.EngineType;
import aula08.ex1.Vehicles.*;
import java.util.Scanner; import java.util.Scanner;
public class VehicleComp { public class VehicleComp {
@ -85,7 +88,7 @@ public class VehicleComp {
case 2, 3 -> { case 2, 3 -> {
System.out.print("Car's trunk size: "); System.out.print("Car's trunk size: ");
int trunkSize = Integer.parseInt(sin.nextLine()); 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()); EngineType engineType = EngineType.fromString(sin.nextLine());
Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize, engineType); Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize, engineType);
if (vehicleType == 2) if (vehicleType == 2)
@ -102,7 +105,7 @@ public class VehicleComp {
if (vehicleType == 4) { if (vehicleType == 4) {
System.out.print("Bus passenger limit: "); System.out.print("Bus passenger limit: ");
int maxPassengers = Integer.parseInt(sin.nextLine()); 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()); EngineType engineType = EngineType.fromString(sin.nextLine());
addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers, engineType)); addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers, engineType));
} else { } 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; 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 boardNumber;
private final int weight; private final int weight;
private final int maxPassengers; private final int maxPassengers;
@ -84,7 +88,7 @@ public class Bus extends Vehicle implements IFuelVehicle, IEletricVehicle {
@Override @Override
public void fillTank(int level) { public void fillTank(int level) {
if (this.engineType == EngineType.ELETRIC) if (this.engineType == EngineType.ELECTRIC)
return; return;
this.fuelLevel = level; 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; 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 boardNumber;
private final int trunkSize; private final int trunkSize;
private final EngineType engineType; private final EngineType engineType;
@ -84,7 +88,7 @@ public class Car extends Vehicle implements IEletricVehicle, IFuelVehicle {
@Override @Override
public void fillTank(int level) { public void fillTank(int level) {
if (this.engineType == EngineType.ELETRIC) if (this.engineType == EngineType.ELECTRIC)
return; return;
this.fuelLevel = level; 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; 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; 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; 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; import java.util.Objects;
@ -11,7 +13,7 @@ public abstract class Vehicle implements IKmTravelled {
private int lastTripKm; private int lastTripKm;
private int km; 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.plate = plate;
this.brand = brand; this.brand = brand;
this.model = model; this.model = model;
@ -20,19 +22,19 @@ public abstract class Vehicle implements IKmTravelled {
this.km = 0; this.km = 0;
} }
protected String getPlate() { public String getPlate() {
return this.plate; return this.plate;
} }
protected String getBrand() { public String getBrand() {
return this.brand; return this.brand;
} }
protected String getModel() { public String getModel() {
return this.model; return this.model;
} }
protected int getPotency() { public int getPotency() {
return this.potency; return this.potency;
} }