[POO] added verifications to Vehicle.java

This commit is contained in:
TiagoRG 2023-05-04 15:19:49 +01:00
parent 7e9b485fc8
commit f17880dd81
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 10 additions and 1 deletions

View File

@ -7,7 +7,7 @@ import java.util.Scanner;
public class Main { public class Main {
private static final Scanner sin = new Scanner(System.in); private static final Scanner sin = new Scanner(System.in);
private static VehicleComp[] vehicleComps; public static VehicleComp[] vehicleComps;
public static void main(String[] args) { public static void main(String[] args) {
while (true) { while (true) {

View File

@ -1,6 +1,8 @@
package aula08.ex1.Vehicles; package aula08.ex1.Vehicles;
import aula08.ex1.Interfaces.IKmTravelled; import aula08.ex1.Interfaces.IKmTravelled;
import aula08.ex1.VehicleComp;
import aula08.ex1.Main;
import utils.Validations; import utils.Validations;
import java.util.Objects; import java.util.Objects;
@ -17,6 +19,13 @@ public abstract class Vehicle implements IKmTravelled {
public Vehicle(String plate, String brand, String model, int potency) { public Vehicle(String plate, String brand, String model, int potency) {
if (!Validations.validateVehiclePlate(plate)) if (!Validations.validateVehiclePlate(plate))
throw new IllegalArgumentException("Invalid 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) if (potency <= 0)
throw new IllegalArgumentException("Potency must be positive"); throw new IllegalArgumentException("Potency must be positive");
this.plate = plate; this.plate = plate;