diff --git a/app/src/main/java/fr/imt_atlantique/myfirstapplication/User.java b/app/src/main/java/fr/imt_atlantique/myfirstapplication/User.java index 56620dc5dca672b09f29ee4b167440aef91aea08..863d36009365bef96354206949c1298e46b06692 100644 --- a/app/src/main/java/fr/imt_atlantique/myfirstapplication/User.java +++ b/app/src/main/java/fr/imt_atlantique/myfirstapplication/User.java @@ -4,10 +4,14 @@ import android.os.Build; import android.os.Parcel; import android.os.Parcelable; +import androidx.annotation.NonNull; + import java.text.DateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; public class User implements Parcelable { private String firstName; @@ -17,15 +21,21 @@ public class User implements Parcelable { private LocalDate birthDate; private String[] phoneNumbers; - public User(String firstName, String lastName, String birthCity, String birthDept, LocalDate birthDate, String[] phoneNumbers) { - this.firstName = firstName; - this.lastName = lastName; - this.birthCity = birthCity; - this.birthDept = birthDept; - this.birthDate = birthDate; - this.phoneNumbers = phoneNumbers; + public User(String firstName, String lastName, String birthCity, String birthDept, LocalDate birthDate, String[] phoneNumbers) throws Exception { + try { + setFirstName(firstName); + setLastName(lastName); + setBirthCity(birthCity); + setBirthDept(birthDept); + setBirthDate(birthDate); + setPhoneNumbers(phoneNumbers); + } catch (Exception e) { + throw new IllegalArgumentException(e.getMessage()); + } } + + protected User(Parcel in) { firstName = in.readString(); lastName = in.readString(); @@ -86,4 +96,75 @@ public class User implements Parcelable { public String[] getPhoneNumbers() { return phoneNumbers; } + + public void setFirstName(String firstName) { + if (firstName.isEmpty()) { + throw new IllegalArgumentException("The first name can not be empty."); + } + this.firstName = capitalizeFirstLetter(firstName); + } + + public void setLastName(String lastName) { + if (lastName.isEmpty()) { + throw new IllegalArgumentException("The last name can not be empty."); + } + this.lastName = capitalizeFirstLetter(lastName); + } + + public void setBirthCity(String birthCity) { + if (birthCity.isEmpty()) { + throw new IllegalArgumentException("The birth city can not be unspecified."); + } + this.birthCity = capitalizeFirstLetter(birthCity); + } + + public void setBirthDept(String birthDept) { + if (birthDept.isEmpty()) { + throw new IllegalArgumentException("The birth department can not be unspecified."); + } + this.birthDept = capitalizeFirstLetter(birthDept); + } + + public void setBirthDate(LocalDate birthDate) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (birthDate != null && birthDate.isAfter(LocalDate.now())) { + throw new IllegalArgumentException("The birth date cannot be in the future."); + } + } + this.birthDate = birthDate; + } + + public void setPhoneNumbers(String[] phoneNumbers) { + this.phoneNumbers = phoneNumbers; + } + + @NonNull + @Override + public String toString() { + return "User{" + + "firstName='" + + firstName + + '\'' + + ", lastName='" + + lastName + + '\'' + + ", birthCity='" + + birthCity + + '\'' + + ", birthDept='" + + birthDept + + '\'' + + ", birthDate=" + + birthDate + + ", phoneNumbers=" + + Arrays.toString(phoneNumbers) + + '}'; + } + + private String capitalizeFirstLetter(String input) { + if (input == null || input.isEmpty()) { + return input; + } + return input.substring(0, 1).toUpperCase() + input.substring(1); + } }