[POO] equals and hashcode added to aula06.ex1.Person.java

This commit is contained in:
TiagoRG 2023-04-30 20:19:03 +01:00
parent 0be641aeab
commit fd5f717206
1 changed files with 15 additions and 0 deletions

View File

@ -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);
}
}