diff --git a/1ano/2semestre/poo/src/aula08/ex1/Bus.java b/1ano/2semestre/poo/src/aula08/ex1/Bus.java new file mode 100644 index 0000000..4128400 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/Bus.java @@ -0,0 +1,57 @@ +package aula08.ex1; + +import java.util.Objects; + +public class Bus extends Vehicle { + private final int boardNumber; + private final int weight; + private final int maxPassengers; + + public Bus(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxPassengers) { + super(plate, brand, model, potency); + this.boardNumber = boardNumber; + this.weight = weight; + this.maxPassengers = maxPassengers; + } + + public int getBoardNumber() { + return this.boardNumber; + } + + public int getWeight() { + return this.weight; + } + + public int getMaxPassengers() { + return this.maxPassengers; + } + + @Override + public String toString() { + return "Bus {" + + "\n\tplate='" + this.getPlate() + '\'' + + ",\n\tbrand='" + this.getBrand() + '\'' + + ",\n\tmodel='" + this.getModel() + '\'' + + ",\n\tpotency=" + this.getPotency() + + ",\n\tboardNumber=" + this.getBoardNumber() + + ",\n\tweight=" + this.getWeight() + + ",\n\tmaxPassengers=" + this.getMaxPassengers() + + ",\n\tlastTripKm=" + this.lastTrip() + + ",\n\tkm=" + this.totalDistance() + + "\n}"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + 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(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxPassengers()); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/Car.java b/1ano/2semestre/poo/src/aula08/ex1/Car.java new file mode 100644 index 0000000..5af2f32 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/Car.java @@ -0,0 +1,55 @@ +package aula08.ex1; + +import java.util.Objects; + +public class Car extends Vehicle { + private final int boardNumber; + private final int trunkSize; + + public Car(String plate, String brand, String model, int potency, int boardNumber, int trunkSize) { + super(plate, brand, model, potency); + this.boardNumber = boardNumber; + this.trunkSize = trunkSize; + } + + public Car(Car car) { + this(car.getPlate(), car.getBrand(), car.getModel(), car.getPotency(), car.getBoardNumber(), car.getTrunkSize()); + } + + public int getBoardNumber() { + return this.boardNumber; + } + + public int getTrunkSize() { + return this.trunkSize; + } + + @Override + public String toString() { + return "Car {" + + "\n\tplate='" + this.getPlate() + '\'' + + ",\n\tbrand='" + this.getBrand() + '\'' + + ",\n\tmodel='" + this.getModel() + '\'' + + ",\n\tpotency=" + this.getPotency() + + ",\n\tboardNumber=" + this.getBoardNumber() + + ",\n\ttrunkSize=" + this.getTrunkSize() + + ",\n\tlastTripKm=" + this.lastTrip() + + ",\n\tkm=" + this.totalDistance() + + "\n}"; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + 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(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getTrunkSize()); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/IKmTravelled.java b/1ano/2semestre/poo/src/aula08/ex1/IKmTravelled.java new file mode 100644 index 0000000..905795b --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/IKmTravelled.java @@ -0,0 +1,7 @@ +package aula08.ex1; + +public interface IKmTravelled { + void trip(int km); + int lastTrip(); + int totalDistance(); +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/Main.java b/1ano/2semestre/poo/src/aula08/ex1/Main.java new file mode 100644 index 0000000..dbb53ec --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/Main.java @@ -0,0 +1,80 @@ +package aula08.ex1; + +import java.util.Scanner; + +public class Main { + private static final Scanner sin = new Scanner(System.in); + private static VehicleComp[] vehicleComps; + + public static void main(String[] args) { + while (true) { + System.out.println("Choose an option\n1 - Create new company\n2 - Delete company\n3 - List companies\n4 - Manage company\n0 - Exit"); + int mainOption = Integer.parseInt(sin.nextLine()); + switch (mainOption) { + case 0 -> { + sin.close(); + System.exit(0); + } + case 1 -> { + System.out.print("New company name: "); + String name = sin.nextLine(); + VehicleComp vehicleComp = new VehicleComp(name); + + if (vehicleComps == null) { + vehicleComps = new VehicleComp[1]; + vehicleComps[0] = vehicleComp; + } else { + VehicleComp[] newVehicleComps = new VehicleComp[vehicleComps.length + 1]; + System.arraycopy(vehicleComps, 0, newVehicleComps, 0, vehicleComps.length); + newVehicleComps[newVehicleComps.length - 1] = vehicleComp; + vehicleComps = newVehicleComps; + } + } + case 2, 3, 4 -> { + for (VehicleComp vehicleComp : vehicleComps) + System.out.println(vehicleComp); + if (mainOption == 2) { + System.out.print("Name of the company to remove: "); + String name = sin.nextLine(); + VehicleComp[] newVehicleComps = new VehicleComp[vehicleComps.length - 1]; + int index = 0; + for (VehicleComp vC : vehicleComps) + if (!vC.getName().equals(name)) + newVehicleComps[index++] = vC; + vehicleComps = newVehicleComps; + } + if (mainOption == 4) { + System.out.print("Name of the company to manage: "); + String name = sin.nextLine(); + for (VehicleComp vehicleComp : vehicleComps) + if (vehicleComp.getName().equals(name)) + manageComp(vehicleComp); + } + } + default -> System.out.println("Invalid option"); + } + } + } + + 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"); + int mainOption = Integer.parseInt(sin.nextLine()); + switch (mainOption) { + case 0 -> { + return; + } + case 1 -> vehicleComp.listVehicles(); + case 2 -> { + System.out.print("Vehicle plate: "); + System.out.println(vehicleComp.getVehicleByPlate(sin.nextLine())); + } + case 3 -> vehicleComp.printVehicleWithMostKm(); + case 4 -> vehicleComp.tripMenu(sin); + case 5 -> vehicleComp.addVehicleMenu(sin); + case 6 -> vehicleComp.removeVehicleMenu(sin); + default -> System.out.println("Invalid option"); + } + } + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/Motorcicle.java b/1ano/2semestre/poo/src/aula08/ex1/Motorcicle.java new file mode 100644 index 0000000..51cd99d --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/Motorcicle.java @@ -0,0 +1,66 @@ +package aula08.ex1; + +import java.util.Objects; + +public class Motorcicle extends Vehicle { + private final MotorcicleType type; + + public Motorcicle(String plate, String brand, String model, int potency, MotorcicleType type) { + super(plate, brand, model, potency); + this.type = type; + } + + public Motorcicle(Motorcicle motorcicle) { + this(motorcicle.getPlate(), motorcicle.getBrand(), motorcicle.getModel(), motorcicle.getPotency(), motorcicle.getType()); + } + + public MotorcicleType getType() { + return this.type; + } + + @Override + public String toString() { + return "Motorcicle {" + + "\n\tplate='" + this.getPlate() + '\'' + + ",\n\tbrand='" + this.getBrand() + '\'' + + ",\n\tmodel='" + this.getModel() + '\'' + + ",\n\tpotency=" + this.getPotency() + + ",\n\ttype=" + this.getType().toString() + + ",\n\tlastTripKm=" + this.lastTrip() + + ",\n\tkm=" + this.totalDistance() + + "\n}"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Motorcicle that = (Motorcicle) o; + return this.getType() == that.getType(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), this.getType()); + } + + enum MotorcicleType { + SPORT, TOURING; + + public String toString() { + return switch (this) { + case SPORT -> "Sport"; + case TOURING -> "Touring"; + }; + } + + public static MotorcicleType fromString(String s) { + return switch (s) { + case "SPORT", "Sport", "sport" -> SPORT; + case "TOURING", "Touring", "touring" -> TOURING; + default -> throw new IllegalArgumentException("Invalid MotorcicleType: " + s); + }; + } + } +} \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula08/ex1/Taxi.java b/1ano/2semestre/poo/src/aula08/ex1/Taxi.java new file mode 100644 index 0000000..323c735 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/Taxi.java @@ -0,0 +1,54 @@ +package aula08.ex1; + +import java.util.Objects; + +public class Taxi extends Car { + private final int licenseNumber; + + public Taxi(Car car, int licenseNumber) { + super(car); + this.licenseNumber = licenseNumber; + } + + public Taxi(Taxi taxi) { + this(taxi.getCar(), taxi.getLicenseNumber()); + } + + public Car getCar() { + return new Car(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.getBoardNumber(), this.getTrunkSize()); + } + + public int getLicenseNumber() { + return this.licenseNumber; + } + + @Override + public String toString() { + return "Taxi {" + + "\n\tlicenseNumber=" + this.licenseNumber + + ",\n\tplate='" + this.getPlate() + '\'' + + ",\n\tbrand='" + this.getBrand() + '\'' + + ",\n\tmodel='" + this.getModel() + '\'' + + ",\n\tpotency=" + this.getPotency() + + ",\n\tboardNumber=" + this.getBoardNumber() + + ",\n\ttrunkSize=" + this.getTrunkSize() + + ",\n\tlastTripKm=" + this.lastTrip() + + ",\n\tkm=" + this.totalDistance() + + "\n}"; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Taxi taxi = (Taxi) o; + return this.getLicenseNumber() == taxi.getLicenseNumber(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), getLicenseNumber()); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/Truck.java b/1ano/2semestre/poo/src/aula08/ex1/Truck.java new file mode 100644 index 0000000..3152041 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/Truck.java @@ -0,0 +1,58 @@ +package aula08.ex1; + +import java.util.Objects; + +public class Truck extends Vehicle { + private final int boardNumber; + private final int weight; + private final int maxWeight; + + 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; + } + + public int getBoardNumber() { + return this.boardNumber; + } + + public int getWeight() { + return this.weight; + } + + public int getMaxWeight() { + return this.maxWeight; + } + + @Override + public String toString() { + return "Truck {" + + "\n\tplate='" + this.getPlate() + '\'' + + ",\n\tbrand='" + this.getBrand() + '\'' + + ",\n\tmodel='" + this.getModel() + '\'' + + ",\n\tpotency=" + this.getPotency() + + ",\n\tboardNumber=" + this.getBoardNumber() + + ",\n\tweight=" + this.getWeight() + + ",\n\tmaxWeight=" + this.getMaxWeight() + + ",\n\tlastTripKm=" + this.lastTrip() + + ",\n\tkm=" + this.totalDistance() + + "\n}"; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Truck truck = (Truck) o; + return this.getBoardNumber() == truck.getBoardNumber() && this.getWeight() == truck.getWeight() && this.getMaxWeight() == truck.getMaxWeight(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), this.getBoardNumber(), this.getWeight(), this.getMaxWeight()); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/Vehicle.java b/1ano/2semestre/poo/src/aula08/ex1/Vehicle.java new file mode 100644 index 0000000..4f6e813 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/Vehicle.java @@ -0,0 +1,67 @@ +package aula08.ex1; + +import java.util.Objects; + +public abstract class Vehicle implements IKmTravelled { + private final String plate; + private final String brand; + private final String model; + private final int potency; + + private int lastTripKm; + private int km; + + protected Vehicle(String plate, String brand, String model, int potency) { + this.plate = plate; + this.brand = brand; + this.model = model; + this.potency = potency; + this.lastTripKm = 0; + this.km = 0; + } + + protected String getPlate() { + return this.plate; + } + + protected String getBrand() { + return this.brand; + } + + protected String getModel() { + return this.model; + } + + protected int getPotency() { + return this.potency; + } + + @Override + public void trip(int km) { + this.lastTripKm = km; + this.km += km; + } + + @Override + public int lastTrip() { + return lastTripKm; + } + + @Override + public int totalDistance() { + return km; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Vehicle vehicle = (Vehicle) o; + return this.getPotency() == vehicle.getPotency() && this.lastTripKm == vehicle.lastTripKm && this.km == vehicle.km && this.getPlate().equals(vehicle.getPlate()) && this.getBrand().equals(vehicle.getBrand()) && this.getModel().equals(vehicle.getModel()); + } + + @Override + public int hashCode() { + return Objects.hash(this.getPlate(), this.getBrand(), this.getModel(), this.getPotency(), this.lastTripKm, this.km); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java b/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java new file mode 100644 index 0000000..3dff6ae --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex1/VehicleComp.java @@ -0,0 +1,160 @@ +package aula08.ex1; + +import java.util.Scanner; + +public class VehicleComp { + private final String name; + private Vehicle[] vehicles; + + public VehicleComp(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public void listVehicles() { + System.out.println("List of vehicles in company " + this.name); + if (this.vehicles == null) { + System.out.println("You must add a vehicle first"); + return; + } + for (Vehicle v : this.vehicles) + System.out.println(v); + } + + public Vehicle getVehicleByPlate(String plate) { + for (Vehicle v : this.vehicles) + if (v.getPlate().equals(plate)) + return v; + return null; + } + + public void tripMenu(Scanner sin) { + if (this.vehicles == null) { + System.out.println("You must add a vehicle first"); + return; + } + this.listVehicles(); + System.out.print("Plate of the vehicle: "); + String plate = sin.nextLine(); + System.out.print("Km to travel: "); + int km = Integer.parseInt(sin.nextLine()); + Vehicle vehicle = this.getVehicleByPlate(plate); + vehicle.trip(km); + } + + public void printVehicleWithMostKm() { + if (this.vehicles == null) { + System.out.println("You must add a vehicle first"); + return; + } + Vehicle max = this.vehicles[0]; + for (Vehicle vehicle : this.vehicles) + if (vehicle.totalDistance() > max.totalDistance()) + max = vehicle; + System.out.println(max); + } + + + // Add Vehicle methods + + public void addVehicleMenu(Scanner sin) { + System.out.print("Vehicle plate: "); + String plate = sin.nextLine(); + System.out.print("Vehicle brand: "); + String brand = sin.nextLine(); + System.out.print("Vehicle model: "); + 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>> "); + 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)); + } + case 2, 3, 4, 5 -> { + System.out.print("Vehicle's board number: "); + int boardNumber = Integer.parseInt(sin.nextLine()); + switch (vehicleType) { + 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); + if (vehicleType == 2) + addVehicle(car); + else { + System.out.print("Taxi's license number: "); + int licenseNumber = Integer.parseInt(sin.nextLine()); + addVehicle(new Taxi(car, licenseNumber)); + } + } + case 4, 5 -> { + System.out.print("Vehicle's weight: "); + int weight = Integer.parseInt(sin.nextLine()); + 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)); + } else { + System.out.print("Truck weight limit: "); + int maxWeight = Integer.parseInt(sin.nextLine()); + addVehicle(new Truck(plate, brand, model, potency, boardNumber, weight, maxWeight)); + } + } + } + } + default -> throw new IllegalStateException("Unexpected value: " + vehicleType); + } + } + + private void addVehicle(Vehicle vehicle) { + if (this.vehicles == null) { + this.vehicles = new Vehicle[1]; + this.vehicles[0] = vehicle; + } else { + Vehicle[] newVehicles = new Vehicle[this.vehicles.length + 1]; + System.arraycopy(this.vehicles, 0, newVehicles, 0, this.vehicles.length); + newVehicles[newVehicles.length - 1] = vehicle; + this.vehicles = newVehicles; + } + } + + + // Remove Vehicle methods + + public void removeVehicleMenu(Scanner sin) { + this.listVehicles(); + System.out.print("Plate of the vehicle to remove: "); + String plate = sin.nextLine(); + Vehicle vehicle = this.getVehicleByPlate(plate); + removeVehicle(vehicle); + } + + private void removeVehicle(Vehicle vehicle) { + Vehicle[] newVehicles = new Vehicle[this.vehicles.length - 1]; + int index = 0; + for (Vehicle v : this.vehicles) + if (!v.getPlate().equals(vehicle.getPlate())) + newVehicles[index++] = v; + this.vehicles = newVehicles; + } + + + private int totalDistance() { + int total = 0; + for (Vehicle vehicle : this.vehicles) + total += vehicle.totalDistance(); + return total; + } + + + @Override + public String toString() { + return String.format("Company %s%nNumber of vehicles: %d%nTotal distance: %d", this.name, this.vehicles.length, this.totalDistance()); + } +} diff --git a/1ano/2semestre/poo/src/aula08/ex3/ShoppingCartTester.java b/1ano/2semestre/poo/src/aula08/ex3/ShoppingCartTester.java new file mode 100644 index 0000000..522e606 --- /dev/null +++ b/1ano/2semestre/poo/src/aula08/ex3/ShoppingCartTester.java @@ -0,0 +1,19 @@ +package aula08.ex3; + +public class ShoppingCartTester { + public static void main(String[] args) { + /*Produto p1 = new ProdutoGenerico("Camisolas", 10, 3); + Produto p2 = new ProdutoGenerico("Calças", 30, 1); + Produto p3 = new ProdutoComDesconto("Sapatilhas", 50, 2, 50); + Produto p4 = new ProdutoComDesconto("Casacos", 100, 1, 10); + + CarrinhoDeCompras carrinho = new CarrinhoDeCompras(); + carrinho.adicionarProduto(p1, 1); + carrinho.adicionarProduto(p2, 5); + carrinho.adicionarProduto(p3, 2); + carrinho.adicionarProduto(p4, 1); + + carrinho.listarProdutos(); + System.out.printf("Preço total da compra %.2f\n", carrinho.calcularTotal());*/ + } +}