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
public int compareTo(MyCompetitor o) {
// to be done
return -1;
int result = getName().compareTo(o.getName());
if (result == 0) {
result = getTime().compareTo(o.getTime());
if (result == 0) {
result = rank - o.rank;
}
}
return result;
}
@Override
public int hashCode() {
// to be done
return -1;
int hashCode = 1;
hashCode *= 13;
hashCode += name.hashCode();
hashCode *= 17;
hashCode += time.hashCode();
hashCode *= 31;
return hashCode + rank;
}
@Override
public boolean equals (Object o) {
// to be done
return false;
if (o instanceof MyCompetitor c) {
return hashCode() == c.hashCode() &&
getName().equals(c.getName()) &&
getTime().equals(c.getTime()) &&
rank == c.rank;
} else {
return false;
}
}
}