[POO] created validations

This commit is contained in:
TiagoRG 2023-04-07 23:42:59 +01:00
parent da5808a180
commit d950efe641
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
5 changed files with 24 additions and 0 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}