itat_challenge/src/model/MedalWinner.cpp

41 lines
1.3 KiB
C++
Raw Normal View History

2024-08-12 22:09:50 +02:00
#include "MedalWinner.h"
2024-08-15 19:21:02 +02:00
/**
* Replaces/sets the won medals of a competitor.
*
* @param medals The won medals with their amount.
* @return True, if successful.
*/
2024-08-12 22:09:50 +02:00
bool MedalWinner::setMedals(const QJsonObject &medals) {
if (!medals.contains("ME_GOLD")
|| !medals.contains("ME_SILVER")
|| !medals.contains("ME_BRONZE")) {
throw invalid_argument("Medal object of competitor is incomplete.");
}
this->m_gold = medals["ME_GOLD"].toInt();
this->m_silver = medals["ME_SILVER"].toInt();
this->m_bronze = medals["ME_BRONZE"].toInt();
2024-08-12 22:09:50 +02:00
return true;
}
2024-08-15 19:21:02 +02:00
/**
* Static compare method, which can compare the amount of medals of two MedalWinners.
* Gold has the highest priority, then m_silver and finally m_bronze.
2024-08-15 19:21:02 +02:00
*
* @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)));
}