[POO] created validations

This commit is contained in:
TiagoRG 2023-03-21 13:13:25 +00:00
parent 98ff46a5db
commit 2e59a60b42
3 changed files with 13 additions and 9 deletions

View File

@ -17,9 +17,10 @@ public class Person {
return this.name; return this.name;
} }
public void setName(String 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"); 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; this.name = name;
} }
@ -27,6 +28,8 @@ public class Person {
return this.cc; return this.cc;
} }
public void setCc(int cc) { public void setCc(int cc) {
if (String.valueOf(cc).length() != 8)
throw new IllegalArgumentException("CC must have 8 digits");
this.cc = cc; this.cc = cc;
} }
@ -34,9 +37,8 @@ public class Person {
return this.birthDate; return this.birthDate;
} }
public void setBirthDate(DateYMD birthDate) { public void setBirthDate(DateYMD birthDate) {
if (birthDate == null) { if (birthDate == null)
throw new IllegalArgumentException("Birth date cannot be null"); throw new IllegalArgumentException("Birth date cannot be null");
}
this.birthDate = birthDate; this.birthDate = birthDate;
} }

View File

@ -16,9 +16,10 @@ public class Professor extends Person {
return this.category; return this.category;
} }
public void setCategory(String 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"); 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; this.category = category;
} }
@ -26,9 +27,10 @@ public class Professor extends Person {
return this.department; return this.department;
} }
public void setDepartment(String 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"); throw new IllegalArgumentException("Department cannot be null or empty");
} if (!department.matches("^[a-zA-Z ]+$"))
throw new IllegalArgumentException("Invalid department");
this.department = department; this.department = department;
} }

View File

@ -88,7 +88,7 @@ public class DateYMD {
return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0; 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); return day >= 1 && day <= monthDays(month, year);
} }
} }