Created Model for competitors.

This commit is contained in:
Steru 2024-08-12 22:09:50 +02:00
parent c539242efa
commit 94fb05a764
8 changed files with 150 additions and 0 deletions

15
src/model/Competitor.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "Competitor.h"
bool Competitor::setCompetitor(const QJsonObject &competitor) {
if (!competitor.contains("code")
|| !competitor.contains("name")
|| !competitor.contains("noc")) {
throw invalid_argument("Not a competitor object.");
}
this->code = competitor["code"].toString();
this->name = competitor["name"].toString();
this->noc = competitor["noc"].toString();
return true;
}

39
src/model/Competitor.h Normal file
View file

@ -0,0 +1,39 @@
#ifndef ITAT_CHALLANGE_OLYMPICS_COMPETITOR_H
#define ITAT_CHALLANGE_OLYMPICS_COMPETITOR_H
#include <QString>
#include <QMap>
#include <QJsonObject>
#include <stdexcept>
using namespace std;
class Competitor {
public:
Competitor(const Competitor &competitor) {
this->code = competitor.code;
this->name = competitor.name;
this->noc = competitor.noc;
}
Competitor(const QJsonObject &competitor) {
setCompetitor(competitor);
}
QString getCode() { return this->code; }
QString getName() { return this->name; }
QString getNOC() { return this->noc; }
bool setCompetitor(const QJsonObject &competitor);
private:
QString code;
QString name;
QString noc;
};
#endif //ITAT_CHALLANGE_OLYMPICS_COMPETITOR_H

View file

@ -0,0 +1,15 @@
#include "CompetitorWithResults.h"
bool CompetitorWithResults::setResults(const QJsonObject &results) {
if (!results.contains("mark")
|| !results.contains("medalType")) {
throw invalid_argument("Results object of competitor is incomplete.");
}
this->results = {
{QString("mark"), results["mark"].toString()},
{QString("medalType"), results["medalType"].toString()}
};
return true;
}

View file

@ -0,0 +1,28 @@
#ifndef ITAT_CHALLANGE_OLYMPICS_COMPETITORWITHRESULTS_H
#define ITAT_CHALLANGE_OLYMPICS_COMPETITORWITHRESULTS_H
#include "Competitor.h"
#include <QString>
#include <QMap>
#include <QJsonObject>
#include <stdexcept>
class CompetitorWithResults : public Competitor {
public:
CompetitorWithResults(const QJsonObject &competitor) : Competitor(competitor) {
if (!competitor.contains("results")) throw invalid_argument("Competitor does not contain results.");
QJsonObject results = competitor["results"].toObject();
setResults(results);
}
bool setResults(const QJsonObject &results);
private:
QMap<QString, QString> results;
};
#endif //ITAT_CHALLANGE_OLYMPICS_COMPETITORWITHRESULTS_H

18
src/model/MedalWinner.cpp Normal file
View file

@ -0,0 +1,18 @@
#include "MedalWinner.h"
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->wonMedals = {
{QString("ME_GOLD"), medals["ME_GOLD"].toString()},
{QString("ME_SILVER"), medals["ME_SILVER"].toString()},
{QString("ME_BRONZE"), medals["ME_BRONZE"].toString()}
};
return true;
}

35
src/model/MedalWinner.h Normal file
View file

@ -0,0 +1,35 @@
#ifndef ITAT_CHALLANGE_OLYMPICS_MEDALWINNER_H
#define ITAT_CHALLANGE_OLYMPICS_MEDALWINNER_H
#include "Competitor.h"
#include <QMap>
#include <QJsonObject>
#include <stdexcept>
class MedalWinner : public Competitor {
public:
MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner) {
this->wonMedals = {
{QString("ME_GOLD"), medalWinner.wonMedals.value("ME_GOLD")},
{QString("ME_SILVER"), medalWinner.wonMedals.value("ME_SILVER")},
{QString("ME_BRONZE"), medalWinner.wonMedals.value("ME_BRONZE")}
};
}
MedalWinner(const QJsonObject &competitor) : Competitor(competitor) {
if (competitor.contains("medals")) throw invalid_argument("Competitor has no medals.");
QJsonObject medals = competitor["medals"].toObject();
setMedals(medals);
}
bool setMedals(const QJsonObject &medals);
private:
QMap<QString, QString> wonMedals;
};
#endif //ITAT_CHALLANGE_OLYMPICS_MEDALWINNER_H