[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 54085d5295
commit aceef79d56
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
1 changed files with 15 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package aula06.ex1;
import utils.DateYMD; import utils.DateYMD;
import java.util.Objects;
public class Person { public class Person {
private String name; private String name;
private int cc; private int cc;
@ -44,4 +46,17 @@ public class Person {
public String toString() { public String toString() {
return String.format("%s; CC: %d; Data de nascimento: %s", this.name, this.cc, this.birthDate); 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);
}
} }