From 17c01703dbb03870a259ac78b7852eab7b17a6c6 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 21 Mar 2023 13:13:25 +0000 Subject: [PATCH] [POO] created validations --- 1ano/2semestre/poo/src/aula06/Person.java | 10 ++++++---- 1ano/2semestre/poo/src/aula06/Professor.java | 10 ++++++---- 1ano/2semestre/poo/src/utils/DateYMD.java | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/1ano/2semestre/poo/src/aula06/Person.java b/1ano/2semestre/poo/src/aula06/Person.java index 6d46a70..09e10a0 100644 --- a/1ano/2semestre/poo/src/aula06/Person.java +++ b/1ano/2semestre/poo/src/aula06/Person.java @@ -17,9 +17,10 @@ public class Person { return this.name; } public void setName(String name) { - if (name == null || name.isEmpty()) { + if (name == null || name.isEmpty()) throw new IllegalArgumentException("Name cannot be null or empty"); - } + if (!name.matches("^[a-zA-Z ]+$")) + throw new IllegalArgumentException("Name must only contain letters and spaces"); this.name = name; } @@ -27,6 +28,8 @@ public class Person { return this.cc; } public void setCc(int cc) { + if (String.valueOf(cc).length() != 8) + throw new IllegalArgumentException("CC must have 8 digits"); this.cc = cc; } @@ -34,9 +37,8 @@ public class Person { return this.birthDate; } public void setBirthDate(DateYMD birthDate) { - if (birthDate == null) { + if (birthDate == null) throw new IllegalArgumentException("Birth date cannot be null"); - } this.birthDate = birthDate; } diff --git a/1ano/2semestre/poo/src/aula06/Professor.java b/1ano/2semestre/poo/src/aula06/Professor.java index 55dd541..1dd9a16 100644 --- a/1ano/2semestre/poo/src/aula06/Professor.java +++ b/1ano/2semestre/poo/src/aula06/Professor.java @@ -16,9 +16,10 @@ public class Professor extends Person { return this.category; } public void setCategory(String category) { - if (category == null || category.isEmpty()) { + if (category == null || category.isEmpty()) throw new IllegalArgumentException("Category cannot be null or empty"); - } + if (!(category.equals("Auxiliar") || category.equals("Associado") || category.equals("CatedrĂ¡tico"))) + throw new IllegalArgumentException("Invalid category"); this.category = category; } @@ -26,9 +27,10 @@ public class Professor extends Person { return this.department; } public void setDepartment(String department) { - if (department == null || department.isEmpty()) { + if (department == null || department.isEmpty()) throw new IllegalArgumentException("Department cannot be null or empty"); - } + if (!department.matches("^[a-zA-Z ]+$")) + throw new IllegalArgumentException("Invalid department"); this.department = department; } diff --git a/1ano/2semestre/poo/src/utils/DateYMD.java b/1ano/2semestre/poo/src/utils/DateYMD.java index 65bb60f..b4723c4 100644 --- a/1ano/2semestre/poo/src/utils/DateYMD.java +++ b/1ano/2semestre/poo/src/utils/DateYMD.java @@ -88,7 +88,7 @@ public class DateYMD { return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0; } - static boolean validDate(int day, int month, int year) { + public static boolean validDate(int day, int month, int year) { return day >= 1 && day <= monthDays(month, year); } } \ No newline at end of file