From cfd5c922fad9669d2c82c811dc8438311542c19b Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Fri, 7 Apr 2023 23:42:59 +0100 Subject: [PATCH] [POO] created validations --- 1ano/2semestre/poo/src/aula08/ex1/Vehicles/Bus.java | 4 ++++ 1ano/2semestre/poo/src/aula08/ex1/Vehicles/Car.java | 2 ++ 1ano/2semestre/poo/src/aula08/ex1/Vehicles/Truck.java | 4 ++++ 1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java | 5 +++++ 1ano/2semestre/poo/src/utils/Validations.java | 9 +++++++++ 5 files changed, 24 insertions(+) create mode 100644 1ano/2semestre/poo/src/utils/Validations.java diff --git a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Bus.java b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Bus.java index 5abbe76..4909564 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Bus.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Bus.java @@ -17,6 +17,10 @@ public class Bus extends Vehicle implements IFuelVehicle, IElectricVehicle { public Bus(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxPassengers, EngineType engineType) { super(plate, brand, model, potency); + if (weight <= 0) + throw new IllegalArgumentException("Weight must be positive."); + if (maxPassengers <= 0) + throw new IllegalArgumentException("Max passengers must be positive."); this.boardNumber = boardNumber; this.weight = weight; this.maxPassengers = maxPassengers; diff --git a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Car.java b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Car.java index 86504ef..b45b745 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Car.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Car.java @@ -16,6 +16,8 @@ public class Car extends Vehicle implements IElectricVehicle, IFuelVehicle { public Car(String plate, String brand, String model, int potency, int boardNumber, int trunkSize, EngineType engineType) { super(plate, brand, model, potency); + if (trunkSize <= 0) + throw new IllegalArgumentException("Trunk size must be positive."); this.boardNumber = boardNumber; this.trunkSize = trunkSize; this.engineType = engineType; diff --git a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Truck.java b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Truck.java index 17738d5..572abe9 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Truck.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Truck.java @@ -12,6 +12,10 @@ public class Truck extends Vehicle implements IFuelVehicle { public Truck(String plate, String brand, String model, int potency, int boardNumber, int weight, int maxWeight) { super(plate, brand, model, potency); + if (weight <= 0) + throw new IllegalArgumentException("Weight must be positive."); + if (maxWeight <= 0) + throw new IllegalArgumentException("Max weight must be positive."); this.boardNumber = boardNumber; this.weight = weight; this.maxWeight = maxWeight; diff --git a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java index a3edf88..87c3e4c 100644 --- a/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java +++ b/1ano/2semestre/poo/src/aula08/ex1/Vehicles/Vehicle.java @@ -1,6 +1,7 @@ package aula08.ex1.Vehicles; import aula08.ex1.Interfaces.IKmTravelled; +import utils.Validations; import java.util.Objects; @@ -14,6 +15,10 @@ public abstract class Vehicle implements IKmTravelled { private int km; public Vehicle(String plate, String brand, String model, int potency) { + if (!Validations.validateVehiclePlate(plate)) + throw new IllegalArgumentException("Invalid plate!"); + if (potency <= 0) + throw new IllegalArgumentException("Potency must be positive"); this.plate = plate; this.brand = brand; this.model = model; diff --git a/1ano/2semestre/poo/src/utils/Validations.java b/1ano/2semestre/poo/src/utils/Validations.java new file mode 100644 index 0000000..902d67c --- /dev/null +++ b/1ano/2semestre/poo/src/utils/Validations.java @@ -0,0 +1,9 @@ +package utils; + +import java.util.regex.Pattern; + +public class Validations { + public static boolean validateVehiclePlate(String plate) { + return Pattern.matches("^[A-Z]{2}-\\d{2}-\\d{2}|\\d{2}-[A-Z]{2}-\\d{2}|\\d{2}-\\d{2}-[A-Z]{2}|[A-Z]{2}-\\d{2}-[A-Z]{2}|[A-Z]{2}-[A-Z]{2}-\\d{2}|\\d{2}-[A-Z]{2}-[A-Z]{2}$", plate); + } +}