diff options
author | Steru <jerrydream111@gmail.com> | 2024-08-15 19:21:02 +0200 |
---|---|---|
committer | Steru <jerrydream111@gmail.com> | 2024-08-16 15:48:53 +0200 |
commit | 5f4431e19c0b6c50c7f24ce040bcbcdd40b7a13c (patch) | |
tree | a769254e53716ca2c0a1fde64b2842bc3a6bac13 /src/model/MedalWinner.cpp | |
parent | 3d19fd72716561a0322ba0acc5c94aac9f161e9d (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))); +} |