Skip to content

Instantly share code, notes, and snippets.

@sidd-kulk
Last active June 11, 2021 08:19
Show Gist options
  • Save sidd-kulk/0a5d39b17b747f6609491f58a0bc1bdf to your computer and use it in GitHub Desktop.
Save sidd-kulk/0a5d39b17b747f6609491f58a0bc1bdf to your computer and use it in GitHub Desktop.
class Employee {
private int id;
private int yearOfBirth;
private int monthOfBirth;
private int dateOfBirth;
private String addressFlat;
private String addressStreet;
private String city;
private String state;
private String pincode;
private String country;
public Employee(int id, int yearOfBirth, int monthOfBirth,
int dateOfBirth, String addressFlat,
String addressStreet, String city,
String state, String pincode, String country) {
this.id = id;
if(yearOfBirth > 2003) {
throw new IllegalArgumentException("Employee must be at least 18 years old")
}
this.yearOfBirth = yearOfBirth;
if(monthOfBirth < 1 || monthOfBirth > 12) {
throw new IllegalArgumentException("There are only 12 months in a year!!");
}
this.monthOfBirth = monthOfBirth;
if(dateOfBirth < 1 || dateOfBirth > 31) { // Ignore leap year/month logic
throw new IllegalArgumentException("There are at max 31 days in a month!!");
}
this.dateOfBirth = dateOfBirth;
if(addressFlat == null || addressFlat.length() < 1) {
throw new IllegalArgumentException("That seems like a short flat number!!");
}
this.addressFlat = addressFlat;
if(addressStreet == null || addressStreet.length() < 5) {
throw new IllegalArgumentException("That seems like a short flat number!!");
}
this.addressStreet = addressStreet;
if(city == null || city.length() < 3) {
throw new IllegalArgumentException("Seems like an invalid city name!!");
}
this.city = city;
if(state == null || state.length() < 3) {
throw new IllegalArgumentException("That seems like a short flat number!!");
}
this.state = state;
if(pincode == null || pincode.length() < 7) {
throw new IllegalArgumentException("Pin code is invalid!!");
}
this.pincode = pincode;
if(country == null || country.length() < 3 || BlacklistedCountries.contains(country)) {
throw new IllegalArgumentException("Incorrect or blacklisted country!!");
}
this.country = country;
}
// Other methods as necessary
}
class BlacklistedCountries {
public static String[] get() {
return new String[]{"Mordor", "Isengard", "Angmar"};
}
public static boolean contains(String country){
return Arrays.stream(BlacklistedCountries.get()).anyMatch(country::equals);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment