Skip to content
Snippets Groups Projects
Commit 76903570 authored by s22thong's avatar s22thong
Browse files

fix(DX): setters with empty checks for attributes in User class

parent b468cb9d
No related branches found
No related tags found
No related merge requests found
...@@ -4,10 +4,14 @@ import android.os.Build; ...@@ -4,10 +4,14 @@ import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import androidx.annotation.NonNull;
import java.text.DateFormat; import java.text.DateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
public class User implements Parcelable { public class User implements Parcelable {
private String firstName; private String firstName;
...@@ -17,15 +21,21 @@ public class User implements Parcelable { ...@@ -17,15 +21,21 @@ public class User implements Parcelable {
private LocalDate birthDate; private LocalDate birthDate;
private String[] phoneNumbers; private String[] phoneNumbers;
public User(String firstName, String lastName, String birthCity, String birthDept, LocalDate birthDate, String[] phoneNumbers) { public User(String firstName, String lastName, String birthCity, String birthDept, LocalDate birthDate, String[] phoneNumbers) throws Exception {
this.firstName = firstName; try {
this.lastName = lastName; setFirstName(firstName);
this.birthCity = birthCity; setLastName(lastName);
this.birthDept = birthDept; setBirthCity(birthCity);
this.birthDate = birthDate; setBirthDept(birthDept);
this.phoneNumbers = phoneNumbers; setBirthDate(birthDate);
setPhoneNumbers(phoneNumbers);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage());
}
} }
protected User(Parcel in) { protected User(Parcel in) {
firstName = in.readString(); firstName = in.readString();
lastName = in.readString(); lastName = in.readString();
...@@ -86,4 +96,75 @@ public class User implements Parcelable { ...@@ -86,4 +96,75 @@ public class User implements Parcelable {
public String[] getPhoneNumbers() { public String[] getPhoneNumbers() {
return phoneNumbers; 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);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment