We need age, and it better show debugging info.

This commit is contained in:
Paweł Dyda 2022-11-17 20:44:40 +01:00
parent 7ae12fd42f
commit 9d064a5a36

View File

@ -3,13 +3,16 @@ package pl.amu.edu.demo.data;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.ToString;
import lombok.experimental.FieldDefaults;
import java.time.LocalDate;
import java.util.Objects;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@FieldDefaults(level = AccessLevel.PUBLIC, makeFinal = true)
@Builder
@ToString
public class Person {
String displayName;
String firstName;
@ -17,4 +20,17 @@ public class Person {
LocalDate birthDate;
Housing housing;
boolean isMarried;
public int getAge() {
var birthYear = Objects.requireNonNull(birthDate).getYear();
var now = LocalDate.now();
var baseDifference = now.getYear() - birthYear;
if (birthDate.getMonthValue() > now.getMonthValue() ||
(birthDate.getMonthValue() == now.getMonthValue() &&
birthDate.getDayOfMonth() >= now.getDayOfMonth())) {
return baseDifference;
}
return baseDifference - 1;
}
}