From e9d1148cd0e9a5d5d2843aa301016dc332a28dd6 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 15 Nov 2022 15:48:18 +0100 Subject: [PATCH] Add a method for hashing and comparison --- .../java/ch/nuth/zhaw/exbox/MyCompetitor.java | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/MyCompetitor.java b/app/src/main/java/ch/nuth/zhaw/exbox/MyCompetitor.java index fef2cf6..021838e 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/MyCompetitor.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/MyCompetitor.java @@ -47,19 +47,39 @@ public class MyCompetitor implements Comparable { @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; + } } }