From 63c9a7732db6d54329e53706190cfd1f53489dfc Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Fri, 7 Apr 2023 23:02:14 +0100 Subject: [PATCH] [POO] aula08 ex1 part2 added (exercise finished) --- 1ano/2semestre/poo/src/aula08/ex1/Bus.java | 40 ++++++++- 1ano/2semestre/poo/src/aula08/ex1/Car.java | 46 ++++++++-- .../poo/src/aula08/ex1/EngineType.java | 13 +++ .../poo/src/aula08/ex1/IEletricVehicle.java | 6 ++ .../poo/src/aula08/ex1/IFuelVehicle.java | 6 ++ 1ano/2semestre/poo/src/aula08/ex1/Main.java | 87 ++++++++++++++++++- .../ex1/{Motorcicle.java => Motorcycle.java} | 36 +++++--- 1ano/2semestre/poo/src/aula08/ex1/Taxi.java | 3 +- 1ano/2semestre/poo/src/aula08/ex1/Truck.java | 15 +++- .../poo/src/aula08/ex1/VehicleComp.java | 17 ++-- 10 files changed, 240 insertions(+), 29 deletions(-) create mode 100644 1ano/2semestre/poo/src/aula08/ex1/EngineType.java create mode 100644 1ano/2semestre/poo/src/aula08/ex1/IEletricVehicle.java create mode 100644 1ano/2semestre/poo/src/aula08/ex1/IFuelVehicle.java rename 1ano/2semestre/poo/src/aula08/ex1/{Motorcicle.java => Motorcycle.java} (61%) diff --git a/1ano/2semestre/poo/src/aula08/ex1/Bus.java b/1ano/2semestre/poo/src/aula08/ex1/Bus.java index 4128400..8882217 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Bus.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Bus.java @@ -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; + } } diff --git a/1ano/2semestre/poo/src/aula08/ex1/Car.java b/1ano/2semestre/poo/src/aula08/ex1/Car.java index 5af2f32..7fbe39e 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Car.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Car.java @@ -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; } } diff --git a/1ano/2semestre/poo/src/aula08/ex1/EngineType.java b/1ano/2semestre/poo/src/aula08/ex1/EngineType.java new file mode 100644 index 0000000..3358d13 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/EngineType.java @@ -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; + }; + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/IEletricVehicle.java b/1ano/2semestre/poo/src/aula08/ex1/IEletricVehicle.java new file mode 100644 index 0000000..561c874 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/IEletricVehicle.java @@ -0,0 +1,6 @@ +package aula08.ex1; + +public interface IEletricVehicle { + int currentBatteryLvl(); + void charge(int percentage); +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/IFuelVehicle.java b/1ano/2semestre/poo/src/aula08/ex1/IFuelVehicle.java new file mode 100644 index 0000000..c04d95a --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/IFuelVehicle.java @@ -0,0 +1,6 @@ +package aula08.ex1; + +public interface IFuelVehicle { + int fuelLevel(); + void fillTank(int level); +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/Main.java b/1ano/2semestre/poo/src/aula08/ex1/Main.java index dbb53ec..82f3784 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Main.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Main.java @@ -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"); + } + } + } } diff --git a/1ano/2semestre/poo/src/aula08/ex1/Motorcicle.java b/1ano/2semestre/poo/src/aula08/ex1/Motorcycle.java similarity index 61% rename from 1ano/2semestre/poo/src/aula08/ex1/Motorcicle.java rename to 1ano/2semestre/poo/src/aula08/ex1/Motorcycle.java index 51cd99d..f014525 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Motorcicle.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Motorcycle.java @@ -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); }; } } diff --git a/1ano/2semestre/poo/src/aula08/ex1/Taxi.java b/1ano/2semestre/poo/src/aula08/ex1/Taxi.java index 323c735..f5f3f1f 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Taxi.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Taxi.java @@ -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}"; } diff --git a/1ano/2semestre/poo/src/aula08/ex1/Truck.java b/1ano/2semestre/poo/src/aula08/ex1/Truck.java index 3152041..62e320f 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Truck.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Truck.java @@ -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; + } } diff --git a/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java b/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java index 3dff6ae..2806086 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java +++ b/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java @@ -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());