[POO] aula08 ex1 part2 added (exercise finished)

This commit is contained in:
TiagoRG 2023-04-07 23:02:14 +01:00
parent fc7cc34e36
commit 11b885db36
10 changed files with 240 additions and 29 deletions

View File

@ -2,16 +2,21 @@ package aula08.ex1;
import java.util.Objects;
public class Bus extends Vehicle {
public class Bus extends Vehicle implements IFuelVehicle, IEletricVehicle {
private final int boardNumber;
private final int weight;
private final int maxPassengers;
private final EngineType engineType;
public Bus(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxPassengers) {
private int fuelLevel;
private int battery;
public Bus(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxPassengers, EngineType engineType) {
super(plate, brand, model, potency);
this.boardNumber = boardNumber;
this.weight = weight;
this.maxPassengers = maxPassengers;
this.engineType = engineType;
}
public int getBoardNumber() {
@ -26,6 +31,10 @@ public class Bus extends Vehicle {
return this.maxPassengers;
}
public EngineType getEngineType() {
return this.engineType;
}
@Override
public String toString() {
return "Bus {" +
@ -38,6 +47,7 @@ public class Bus extends Vehicle {
",\n\tmaxPassengers=" + this.getMaxPassengers() +
",\n\tlastTripKm=" + this.lastTrip() +
",\n\tkm=" + this.totalDistance() +
String.format(",\n\t%s=%d", this.engineType == EngineType.FUEL ? "fuelLevel" : "battery", this.engineType == EngineType.FUEL ? this.fuelLevel : this.battery) +
"\n}";
}
@ -47,11 +57,35 @@ public class Bus extends Vehicle {
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Bus bus = (Bus) o;
return this.getBoardNumber() == bus.getBoardNumber() && this.getWeight() == bus.getWeight() && this.getMaxPassengers() == bus.getMaxPassengers();
return this.getBoardNumber() == bus.getBoardNumber() && this.getWeight() == bus.getWeight() && this.getMaxPassengers() == bus.getMaxPassengers() && this.getEngineType().equals(bus.getEngineType());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxPassengers());
}
@Override
public int currentBatteryLvl() {
return this.battery;
}
@Override
public void charge(int percentage) {
if (this.engineType == EngineType.FUEL)
return;
this.battery = percentage;
}
@Override
public int fuelLevel() {
return this.fuelLevel;
}
@Override
public void fillTank(int level) {
if (this.engineType == EngineType.ELETRIC)
return;
this.fuelLevel = level;
}
}

View File

@ -2,18 +2,25 @@ package aula08.ex1;
import java.util.Objects;
public class Car extends Vehicle {
public class Car extends Vehicle implements IEletricVehicle, IFuelVehicle {
private final int boardNumber;
private final int trunkSize;
private final EngineType engineType;
public Car(String plate, String brand, String model, int potency, int boardNumber, int trunkSize) {
private int fuelLevel;
private int battery;
public Car(String plate, String brand, String model, int potency, int boardNumber, int trunkSize, EngineType engineType) {
super(plate, brand, model, potency);
this.boardNumber = boardNumber;
this.trunkSize = trunkSize;
this.engineType = engineType;
this.fuelLevel = 0;
this.battery = 0;
}
public Car(Car car) {
this(car.getPlate(), car.getBrand(), car.getModel(), car.getPotency(), car.getBoardNumber(), car.getTrunkSize());
this(car.getPlate(), car.getBrand(), car.getModel(), car.getPotency(), car.getBoardNumber(), car.getTrunkSize(), car.getEngineType());
}
public int getBoardNumber() {
@ -24,6 +31,10 @@ public class Car extends Vehicle {
return this.trunkSize;
}
public EngineType getEngineType() {
return this.engineType;
}
@Override
public String toString() {
return "Car {" +
@ -35,6 +46,7 @@ public class Car extends Vehicle {
",\n\ttrunkSize=" + this.getTrunkSize() +
",\n\tlastTripKm=" + this.lastTrip() +
",\n\tkm=" + this.totalDistance() +
String.format(",\n\t%s=%d", this.engineType == EngineType.FUEL ? "fuelLevel" : "battery", this.engineType == EngineType.FUEL ? this.fuelLevel : this.battery) +
"\n}";
}
@ -45,11 +57,35 @@ public class Car extends Vehicle {
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Car car = (Car) o;
return this.getBoardNumber() == car.getBoardNumber() && this.getTrunkSize() == car.getTrunkSize();
return this.getBoardNumber() == car.getBoardNumber() && this.getTrunkSize() == car.getTrunkSize() && this.getEngineType().equals(car.getEngineType());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getTrunkSize());
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getTrunkSize(), this.getEngineType());
}
@Override
public int currentBatteryLvl() {
return this.battery;
}
@Override
public void charge(int percentage) {
if (this.engineType == EngineType.FUEL)
return;
this.battery = percentage;
}
@Override
public int fuelLevel() {
return this.fuelLevel;
}
@Override
public void fillTank(int level) {
if (this.engineType == EngineType.ELETRIC)
return;
this.fuelLevel = level;
}
}

View File

@ -0,0 +1,13 @@
package aula08.ex1;
public enum EngineType {
FUEL, ELETRIC;
public static EngineType fromString(String string) {
return switch (string) {
case "FUEL", "Fuel", "fuel" -> EngineType.FUEL;
case "ELETRIC", "Eletric", "eletric" -> EngineType.ELETRIC;
default -> null;
};
}
}

View File

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

View File

@ -0,0 +1,6 @@
package aula08.ex1;
public interface IFuelVehicle {
int fuelLevel();
void fillTank(int level);
}

View File

@ -58,7 +58,7 @@ public class Main {
private static void manageComp(VehicleComp vehicleComp) {
while (true) {
System.out.println("Choose an option\n1 - List vehicle\n2 - Get vehicle info\n3 - Vehicle with most Km\n4 - Send vehicle on a trip\n5 - Add Vehicle\n6 - Remove vehicle\n0 - Exit");
System.out.println("Choose an option\n1 - List vehicle\n2 - Get vehicle info\n3 - Vehicle with most Km\n4 - Manage vehicle\n5 - Add Vehicle\n6 - Remove vehicle\n0 - Exit");
int mainOption = Integer.parseInt(sin.nextLine());
switch (mainOption) {
case 0 -> {
@ -70,11 +70,94 @@ public class Main {
System.out.println(vehicleComp.getVehicleByPlate(sin.nextLine()));
}
case 3 -> vehicleComp.printVehicleWithMostKm();
case 4 -> vehicleComp.tripMenu(sin);
case 4 -> {
System.out.print("Plate of the vehicle to manage: ");
String plate = sin.nextLine();
Vehicle vehicle = vehicleComp.getVehicleByPlate(plate);
manageVehicle(vehicle);
}
case 5 -> vehicleComp.addVehicleMenu(sin);
case 6 -> vehicleComp.removeVehicleMenu(sin);
default -> System.out.println("Invalid option");
}
}
}
private static void manageVehicle(Vehicle vehicle) {
while (true) {
System.out.println("Choose an option\n1 - Send vehicle on a trip\n2 - Refuel/Recharge\n0 - Exit");
int mainOption = Integer.parseInt(sin.nextLine());
switch (mainOption) {
case 0 -> {
return;
}
case 1 -> {
System.out.print("Km the vehicle is going to travel: ");
int km = Integer.parseInt(sin.nextLine());
vehicle.trip(km);
if (vehicle instanceof Car car) {
if (car.getEngineType() == EngineType.FUEL) {
System.out.print("What fuel level is the car going to be left at: ");
int level = Integer.parseInt(sin.nextLine());
((Car) vehicle).fillTank(level);
} else {
System.out.print("What battery percentage is the car going to be left at: ");
int battery = Integer.parseInt(sin.nextLine());
((Car) vehicle).charge(battery);
}
} else if (vehicle instanceof Bus bus) {
if (bus.getEngineType() == EngineType.FUEL) {
System.out.print("What fuel level is the bus going to be left at: ");
int level = Integer.parseInt(sin.nextLine());
((Bus) vehicle).fillTank(level);
} else {
System.out.print("What battery percentage is the bus going to be left at: ");
int battery = Integer.parseInt(sin.nextLine());
((Bus) vehicle).charge(battery);
}
} else if (vehicle instanceof Motorcycle) {
System.out.print("What fuel level is the motorcycle going to be left at: ");
int level = Integer.parseInt(sin.nextLine());
((Motorcycle) vehicle).fillTank(level);
} else if (vehicle instanceof Truck) {
System.out.print("What fuel level is the truck going to be left at: ");
int level = Integer.parseInt(sin.nextLine());
((Truck) vehicle).fillTank(level);
}
}
case 2 -> {
if (vehicle instanceof Car car) {
if (car.getEngineType() == EngineType.FUEL) {
System.out.print("What fuel level are you refueling the car to: ");
int level = Integer.parseInt(sin.nextLine());
((Car) vehicle).fillTank(level);
} else {
System.out.print("What battery percentage are you charging the car to: ");
int battery = Integer.parseInt(sin.nextLine());
((Car) vehicle).charge(battery);
}
} else if (vehicle instanceof Bus bus) {
if (bus.getEngineType() == EngineType.FUEL) {
System.out.print("What fuel level are you refueling the bus to: ");
int level = Integer.parseInt(sin.nextLine());
((Bus) vehicle).fillTank(level);
} else {
System.out.print("What battery percentage are you charging the bus to: ");
int battery = Integer.parseInt(sin.nextLine());
((Bus) vehicle).charge(battery);
}
} else if (vehicle instanceof Motorcycle) {
System.out.print("What fuel level are you refueling the motorcycle to: ");
int level = Integer.parseInt(sin.nextLine());
((Motorcycle) vehicle).fillTank(level);
} else if (vehicle instanceof Truck) {
System.out.print("What fuel level are you refueling the truck to: ");
int level = Integer.parseInt(sin.nextLine());
((Truck) vehicle).fillTank(level);
}
}
default -> System.out.println("Invalid option");
}
}
}
}

View File

@ -2,25 +2,28 @@ package aula08.ex1;
import java.util.Objects;
public class Motorcicle extends Vehicle {
private final MotorcicleType type;
public class Motorcycle extends Vehicle implements IFuelVehicle {
private final MotorcycleType type;
public Motorcicle(String plate, String brand, String model, int potency, MotorcicleType type) {
private int fuelLevel;
public Motorcycle(String plate, String brand, String model, int potency, MotorcycleType type) {
super(plate, brand, model, potency);
this.type = type;
this.fuelLevel = 0;
}
public Motorcicle(Motorcicle motorcicle) {
this(motorcicle.getPlate(), motorcicle.getBrand(), motorcicle.getModel(), motorcicle.getPotency(), motorcicle.getType());
public Motorcycle(Motorcycle motorcycle) {
this(motorcycle.getPlate(), motorcycle.getBrand(), motorcycle.getModel(), motorcycle.getPotency(), motorcycle.getType());
}
public MotorcicleType getType() {
public MotorcycleType getType() {
return this.type;
}
@Override
public String toString() {
return "Motorcicle {" +
return "Motorcycle {" +
"\n\tplate='" + this.getPlate() + '\'' +
",\n\tbrand='" + this.getBrand() + '\'' +
",\n\tmodel='" + this.getModel() + '\'' +
@ -28,6 +31,7 @@ public class Motorcicle extends Vehicle {
",\n\ttype=" + this.getType().toString() +
",\n\tlastTripKm=" + this.lastTrip() +
",\n\tkm=" + this.totalDistance() +
",\n\tfuelLevel=" + this.fuelLevel() +
"\n}";
}
@ -36,7 +40,7 @@ public class Motorcicle extends Vehicle {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Motorcicle that = (Motorcicle) o;
Motorcycle that = (Motorcycle) o;
return this.getType() == that.getType();
}
@ -45,7 +49,17 @@ public class Motorcicle extends Vehicle {
return Objects.hash(super.hashCode(), this.getType());
}
enum MotorcicleType {
@Override
public int fuelLevel() {
return this.fuelLevel;
}
@Override
public void fillTank(int level) {
this.fuelLevel = level;
}
enum MotorcycleType {
SPORT, TOURING;
public String toString() {
@ -55,11 +69,11 @@ public class Motorcicle extends Vehicle {
};
}
public static MotorcicleType fromString(String s) {
public static MotorcycleType fromString(String s) {
return switch (s) {
case "SPORT", "Sport", "sport" -> SPORT;
case "TOURING", "Touring", "touring" -> TOURING;
default -> throw new IllegalArgumentException("Invalid MotorcicleType: " + s);
default -> throw new IllegalArgumentException("Invalid MotorcycleType: " + s);
};
}
}

View File

@ -15,7 +15,7 @@ public class Taxi extends Car {
}
public Car getCar() {
return new Car(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.getBoardNumber(), this.getTrunkSize());
return new Car(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.getBoardNumber(), this.getTrunkSize(), this.getEngineType());
}
public int getLicenseNumber() {
@ -34,6 +34,7 @@ public class Taxi extends Car {
",\n\ttrunkSize=" + this.getTrunkSize() +
",\n\tlastTripKm=" + this.lastTrip() +
",\n\tkm=" + this.totalDistance() +
String.format(",\n\t%s=%d", this.getEngineType() == EngineType.FUEL ? "fuelLevel" : "battery", this.getEngineType() == EngineType.FUEL ? this.fuelLevel() : this.currentBatteryLvl()) +
"\n}";
}

View File

@ -2,16 +2,18 @@ package aula08.ex1;
import java.util.Objects;
public class Truck extends Vehicle {
public class Truck extends Vehicle implements IFuelVehicle {
private final int boardNumber;
private final int weight;
private final int maxWeight;
private int fuelLevel;
public Truck(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxWeight) {
super(plate, brand, model, potency);
this.boardNumber = boardNumber;
this.weight = weight;
this.maxWeight = maxWeight;
this.fuelLevel = 0;
}
public int getBoardNumber() {
@ -38,6 +40,7 @@ public class Truck extends Vehicle {
",\n\tmaxWeight=" + this.getMaxWeight() +
",\n\tlastTripKm=" + this.lastTrip() +
",\n\tkm=" + this.totalDistance() +
",\n\tfuelLevel=" + this.fuelLevel() +
"\n}";
}
@ -55,4 +58,14 @@ public class Truck extends Vehicle {
public int hashCode() {
return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxWeight());
}
@Override
public int fuelLevel() {
return this.fuelLevel;
}
@Override
public void fillTank(int level) {
this.fuelLevel = level;
}
}

View File

@ -42,6 +42,7 @@ public class VehicleComp {
System.out.print("Km to travel: ");
int km = Integer.parseInt(sin.nextLine());
Vehicle vehicle = this.getVehicleByPlate(plate);
vehicle.trip(km);
}
@ -69,13 +70,13 @@ public class VehicleComp {
String model = sin.nextLine();
System.out.print("Vehicle potency: ");
int potency = Integer.parseInt(sin.nextLine());
System.out.print("What vehicle are you adding?\n1 - Motorcicle\n2 - Car\n3 - Taxi\n4 - Bus\n5 - Truck\n>> ");
System.out.print("What vehicle are you adding?\n1 - Motorcycle\n2 - Car\n3 - Taxi\n4 - Bus\n5 - Truck\n>> ");
int vehicleType = Integer.parseInt(sin.nextLine());
switch (vehicleType) {
case 1 -> {
System.out.print("What's the motorcicle type? (SPORT/TOURING): ");
Motorcicle.MotorcicleType motorcicleType = Motorcicle.MotorcicleType.fromString(sin.nextLine());
addVehicle(new Motorcicle(plate, brand, model, potency, motorcicleType));
System.out.print("What's the motorcycle type? (SPORT/TOURING): ");
Motorcycle.MotorcycleType motorcycleType = Motorcycle.MotorcycleType.fromString(sin.nextLine());
addVehicle(new Motorcycle(plate, brand, model, potency, motorcycleType));
}
case 2, 3, 4, 5 -> {
System.out.print("Vehicle's board number: ");
@ -84,7 +85,9 @@ public class VehicleComp {
case 2, 3 -> {
System.out.print("Car's trunk size: ");
int trunkSize = Integer.parseInt(sin.nextLine());
Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize);
System.out.print("What's the engine type? (FUEL/ELETRIC): ");
EngineType engineType = EngineType.fromString(sin.nextLine());
Car car = new Car(plate, brand, model, potency, boardNumber, trunkSize, engineType);
if (vehicleType == 2)
addVehicle(car);
else {
@ -99,7 +102,9 @@ public class VehicleComp {
if (vehicleType == 4) {
System.out.print("Bus passenger limit: ");
int maxPassengers = Integer.parseInt(sin.nextLine());
addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers));
System.out.print("What's the engine type? (FUEL/ELETRIC): ");
EngineType engineType = EngineType.fromString(sin.nextLine());
addVehicle(new Bus(plate, brand, model, potency, boardNumber, weight, maxPassengers, engineType));
} else {
System.out.print("Truck weight limit: ");
int maxWeight = Integer.parseInt(sin.nextLine());