From 9d064a5a36c2eabab26cc394e9ac725897e41f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Dyda?= Date: Thu, 17 Nov 2022 20:44:40 +0100 Subject: [PATCH] We need age, and it better show debugging info. --- .../main/java/pl/amu/edu/demo/data/Person.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/demo/02/src/main/java/pl/amu/edu/demo/data/Person.java b/demo/02/src/main/java/pl/amu/edu/demo/data/Person.java index b2d7453..7b836f4 100644 --- a/demo/02/src/main/java/pl/amu/edu/demo/data/Person.java +++ b/demo/02/src/main/java/pl/amu/edu/demo/data/Person.java @@ -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; + } + }