diff options
author | Steru <jerrydream111@gmail.com> | 2024-08-15 19:21:02 +0200 |
---|---|---|
committer | Steru <jerrydream111@gmail.com> | 2024-08-16 23:50:34 +0200 |
commit | 70d5f014e50b9255dcb5c36122a109cc049c8936 (patch) | |
tree | f14ab291bec1a27457166ab1c4e0e70920b2ffa9 /src/model/MedalWinner.cpp | |
parent | 173ce00cf29822294137d3ebacc5b33ad01a3d25 (diff) |
Added Q Object macros.
Diffstat (limited to 'src/model/MedalWinner.cpp')
-rw-r--r-- | src/model/MedalWinner.cpp | 32 |
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))); +} |