summaryrefslogtreecommitdiff
path: root/src/model/MedalWinner.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/MedalWinner.cpp')
-rw-r--r--src/model/MedalWinner.cpp32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/model/MedalWinner.cpp b/src/model/MedalWinner.cpp
index c4cb663..4eca811 100644
--- a/src/model/MedalWinner.cpp
+++ b/src/model/MedalWinner.cpp
@@ -1,6 +1,12 @@
#include "MedalWinner.h"
+/**
+ * Replaces/sets the won medals of a competitor.
+ *
+ * @param medals The won medals with their amount.
+ * @return True, if successful.
+ */
bool MedalWinner::setMedals(const QJsonObject &medals) {
if (!medals.contains("ME_GOLD")
|| !medals.contains("ME_SILVER")
@@ -8,11 +14,27 @@ bool MedalWinner::setMedals(const QJsonObject &medals) {
throw invalid_argument("Medal object of competitor is incomplete.");
}
- this->wonMedals = {
- {QString("ME_GOLD"), medals["ME_GOLD"].toString()},
- {QString("ME_SILVER"), medals["ME_SILVER"].toString()},
- {QString("ME_BRONZE"), medals["ME_BRONZE"].toString()}
- };
+ this->gold = medals["ME_GOLD"].toInt();
+ this->silver = medals["ME_SILVER"].toInt();
+ this->bronze = medals["ME_BRONZE"].toInt();
return true;
}
+
+/**
+ * Static compare method, which can compare the amount of medals of two MedalWinners.
+ * Gold has the highest priority, then silver and finally bronze.
+ *
+ * @param lComp First competitor to compare.
+ * @param rComp Second competitor to compare.
+ * @return True, if the second competitor got more or higher medals.
+ */
+bool MedalWinner::compare(MedalWinner lComp, MedalWinner rComp) {
+ // create difference between medal amounts
+ int gold = lComp.getGold() - rComp.getGold();
+ int silver = lComp.getSilver() - rComp.getSilver();
+ int bronze = lComp.getBronze() - rComp.getBronze();
+
+ // compare medal differences
+ return gold < 0 || (gold == 0 && (silver < 0 || (silver == 0 && bronze < 0)));
+}