[POO] created validations
This commit is contained in:
parent
a191dc120d
commit
cfd5c922fa
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue