Add a method for hashing and comparison

This commit is contained in:
Manuel Thalmann 2022-11-15 15:48:18 +01:00
parent 29bd296ceb
commit e9d1148cd0

View file

@ -47,19 +47,39 @@ public class MyCompetitor implements Comparable<MyCompetitor> {
@Override @Override
public int compareTo(MyCompetitor o) { public int compareTo(MyCompetitor o) {
// to be done int result = getName().compareTo(o.getName());
return -1;
if (result == 0) {
result = getTime().compareTo(o.getTime());
if (result == 0) {
result = rank - o.rank;
}
}
return result;
} }
@Override @Override
public int hashCode() { public int hashCode() {
// to be done int hashCode = 1;
return -1; hashCode *= 13;
hashCode += name.hashCode();
hashCode *= 17;
hashCode += time.hashCode();
hashCode *= 31;
return hashCode + rank;
} }
@Override @Override
public boolean equals (Object o) { public boolean equals (Object o) {
// to be done if (o instanceof MyCompetitor c) {
return hashCode() == c.hashCode() &&
getName().equals(c.getName()) &&
getTime().equals(c.getTime()) &&
rank == c.rank;
} else {
return false; return false;
} }
}
} }