|
|
|
|
|
by vips7L
673 days ago
|
|
Yes this is just extreme bike-shedding at this point. But none of this is impossible with more OO principles, like interfaces: class User {
// Convenience function to check if the user is an adult in their current location
boolean isAdult() {
return this.location.isAdult(this);
}
boolean isOfDrinkingAge() {
return this.location.isOfDrinkingAge(this);
}
}
interface Location {
boolean isAdult(User u);
boolean isOfDrinkingAge(User u);
}
class WeirdLawsLocation implements Location {
boolean isAdult(User u) {
return switch (u.gender()) {
case MALE -> u.age() >= 16;
case FEMALE -> u.age() >= 18;
}
}
boolean isOfDrinkingAge(User u) {
return u.age() >= 21
}
}
In the hypothetical that you want to check somewhere the user is not currently: class SwedenLocation implements Location {
boolean isAdult(User u) {
return u.age() >= 18;
}
boolean isOfDrinkinAge(User u) {
return u.age() >= 18;
}
}
var sweden = new SwedenLocation();
sweden.isOfDrinkingAge(user);
|
|