From 70d5f014e50b9255dcb5c36122a109cc049c8936 Mon Sep 17 00:00:00 2001 From: Steru Date: Thu, 15 Aug 2024 19:21:02 +0200 Subject: Added Q Object macros. --- src/model/MedalWinner.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/model/MedalWinner.cpp') 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))); +} -- cgit v1.2.3