From 9a77fa0d1a313a46cfaaa58f770f6f48d0f61be1 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] [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;