From f17880dd81445cc2c144e91240304493154acf29 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Thu, 4 May 2023 15:19:49 +0100 Subject: [PATCH 1/3] [POO] added verifications to Vehicle.java --- 1ano/2semestre/poo/src/aula08/ex1/Main.java | 2 +- 1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/1ano/2semestre/poo/src/aula08/ex1/Main.java b/1ano/2semestre/poo/src/aula08/ex1/Main.java index 48684cf..8e62f7b 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Main.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Main.java @@ -7,7 +7,7 @@ import java.util.Scanner; public class Main { private static final Scanner sin = new Scanner(System.in); - private static VehicleComp[] vehicleComps; + public static VehicleComp[] vehicleComps; public static void main(String[] args) { while (true) { diff --git a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java index 87c3e4c..6271af8 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java @@ -1,6 +1,8 @@ package aula08.ex1.Vehicles; import aula08.ex1.Interfaces.IKmTravelled; +import aula08.ex1.VehicleComp; +import aula08.ex1.Main; import utils.Validations; import java.util.Objects; @@ -17,6 +19,13 @@ public abstract class Vehicle implements IKmTravelled { public Vehicle(String plate, String brand, String model, int potency) { if (!Validations.validateVehiclePlate(plate)) throw new IllegalArgumentException("Invalid plate!"); + for (VehicleComp vehicleComp : Main.vehicleComps) + if (vehicleComp.getVehicleByPlate(plate) != null) + throw new IllegalArgumentException("Plate already exists!"); + if (brand == null || brand.isEmpty()) + throw new IllegalArgumentException("Brand must not be empty"); + if (model == null || model.isEmpty()) + throw new IllegalArgumentException("Model must not be empty"); if (potency <= 0) throw new IllegalArgumentException("Potency must be positive"); this.plate = plate; From 94c43567d04fe19d1cb0b1990e51ba22025f4334 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Thu, 4 May 2023 15:41:45 +0100 Subject: [PATCH 2/3] [POO] simplified enums fromString --- .../2semestre/poo/src/aula08/ex1/Enums/EngineType.java | 6 +++--- .../poo/src/aula08/ex2/Enums/AlimentType.java | 10 +++++----- 1ano/2semestre/poo/src/aula08/ex2/Enums/DishType.java | 8 ++++---- 1ano/2semestre/poo/src/aula08/ex2/Enums/FishState.java | 6 +++--- 1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java | 10 +++++----- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/1ano/2semestre/poo/src/aula08/ex1/Enums/EngineType.java b/1ano/2semestre/poo/src/aula08/ex1/Enums/EngineType.java index 743da2c..7bd2da0 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Enums/EngineType.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Enums/EngineType.java @@ -4,9 +4,9 @@ public enum EngineType { FUEL, ELECTRIC; public static EngineType fromString(String string) { - return switch (string) { - case "FUEL", "Fuel", "fuel" -> EngineType.FUEL; - case "ELECTRIC", "Electric", "electric" -> EngineType.ELECTRIC; + return switch (string.toUpperCase()) { + case "FUEL" -> FUEL; + case "ELECTRIC" -> ELECTRIC; default -> null; }; } diff --git a/1ano/2semestre/poo/src/aula08/ex2/Enums/AlimentType.java b/1ano/2semestre/poo/src/aula08/ex2/Enums/AlimentType.java index 5bfc701..4cb8336 100644 --- a/1ano/2semestre/poo/src/aula08/ex2/Enums/AlimentType.java +++ b/1ano/2semestre/poo/src/aula08/ex2/Enums/AlimentType.java @@ -4,11 +4,11 @@ public enum AlimentType { MEAT, FISH, CEREAL, VEGETABLE; public static AlimentType fromString(String string) { - return switch (string) { - case "MEAT", "Meat", "meat" -> MEAT; - case "FISH", "Fish", "fish" -> FISH; - case "CEREAL", "Cereal", "cereal" -> CEREAL; - case "VEGETABLE", "Vegetable", "vegetable" -> VEGETABLE; + return switch (string.toUpperCase()) { + case "MEAT"-> MEAT; + case "FISH" -> FISH; + case "CEREAL" -> CEREAL; + case "VEGETABLE" -> VEGETABLE; default -> null; }; } diff --git a/1ano/2semestre/poo/src/aula08/ex2/Enums/DishType.java b/1ano/2semestre/poo/src/aula08/ex2/Enums/DishType.java index 90098e5..d5d33e4 100644 --- a/1ano/2semestre/poo/src/aula08/ex2/Enums/DishType.java +++ b/1ano/2semestre/poo/src/aula08/ex2/Enums/DishType.java @@ -4,10 +4,10 @@ public enum DishType { NORMAL, VEGAN, DIET; public static DishType fromString(String string) { - return switch (string) { - case "NORMAL", "Normal", "normal" -> NORMAL; - case "VEGAN", "Vegan", "vegan" -> VEGAN; - case "DIET", "Diet", "diet" -> DIET; + return switch (string.toUpperCase()) { + case "NORMAL" -> NORMAL; + case "VEGAN" -> VEGAN; + case "DIET" -> DIET; default -> null; }; } diff --git a/1ano/2semestre/poo/src/aula08/ex2/Enums/FishState.java b/1ano/2semestre/poo/src/aula08/ex2/Enums/FishState.java index ee33518..db2824b 100644 --- a/1ano/2semestre/poo/src/aula08/ex2/Enums/FishState.java +++ b/1ano/2semestre/poo/src/aula08/ex2/Enums/FishState.java @@ -4,9 +4,9 @@ public enum FishState { FRESH, FROZEN; public static FishState fromString(String string) { - return switch (string) { - case "FRESH", "Fresh", "fresh" -> FRESH; - case "FROZEN", "Frozen", "frozen" -> FROZEN; + return switch (string.toUpperCase()) { + case "FRESH"-> FRESH; + case "FROZEN" -> FROZEN; default -> null; }; } diff --git a/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java b/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java index 73709d6..ff9f6dc 100644 --- a/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java +++ b/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java @@ -4,11 +4,11 @@ public enum MeatType { COW, PORK, TURKEY, CHICKEN; public static MeatType fromString(String string) { - return switch (string) { - case "COW", "Cow", "cow" -> COW; - case "PORK", "Pork", "pork" -> PORK; - case "TURKEY", "Turkey", "turkey" -> TURKEY; - case "CHICKEN", "Chicken", "chicken" -> CHICKEN; + return switch (string.toUpperCase()) { + case "COW" -> COW; + case "PORK" -> PORK; + case "TURKEY" -> TURKEY; + case "CHICKEN" -> CHICKEN; default -> null; }; } From 2a3d5eaa11c9e7f6347350977acce7bf0f326645 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Thu, 4 May 2023 15:44:17 +0100 Subject: [PATCH 3/3] [POO] added OTHER to MeatType --- 1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java b/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java index ff9f6dc..ffdf55f 100644 --- a/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java +++ b/1ano/2semestre/poo/src/aula08/ex2/Enums/MeatType.java @@ -1,7 +1,7 @@ package aula08.ex2.Enums; public enum MeatType { - COW, PORK, TURKEY, CHICKEN; + COW, PORK, TURKEY, CHICKEN, OTHER; public static MeatType fromString(String string) { return switch (string.toUpperCase()) { @@ -9,7 +9,7 @@ public enum MeatType { case "PORK" -> PORK; case "TURKEY" -> TURKEY; case "CHICKEN" -> CHICKEN; - default -> null; + default -> OTHER; }; } @@ -20,6 +20,7 @@ public enum MeatType { case PORK -> "PORK"; case TURKEY -> "TURKEY"; case CHICKEN -> "CHICKEN"; + default -> "OTHER"; }; } }