From aceef79d560d22f57cfae0829d01e0270975e2a1 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sun, 30 Apr 2023 20:19:03 +0100 Subject: [PATCH] [POO] equals and hashcode added to aula06.ex1.Person.java --- 1ano/2semestre/poo/src/aula06/ex1/Person.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/1ano/2semestre/poo/src/aula06/ex1/Person.java b/1ano/2semestre/poo/src/aula06/ex1/Person.java index c1b5386..4512c05 100644 --- a/1ano/2semestre/poo/src/aula06/ex1/Person.java +++ b/1ano/2semestre/poo/src/aula06/ex1/Person.java @@ -2,6 +2,8 @@ package aula06.ex1; import utils.DateYMD; +import java.util.Objects; + public class Person { private String name; private int cc; @@ -44,4 +46,17 @@ public class Person { public String toString() { return String.format("%s; CC: %d; Data de nascimento: %s", this.name, this.cc, this.birthDate); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return cc == person.cc && Objects.equals(name, person.name) && Objects.equals(birthDate, person.birthDate); + } + + @Override + public int hashCode() { + return Objects.hash(name, cc, birthDate); + } }