From 94fb05a764c1d0d1f275f29ecd21686d835f9768 Mon Sep 17 00:00:00 2001 From: Steru Date: Mon, 12 Aug 2024 22:09:50 +0200 Subject: [PATCH] Created Model for competitors. --- src/model/Competitor.cpp | 15 +++++++++++ src/model/Competitor.h | 39 +++++++++++++++++++++++++++++ src/model/CompetitorWithResults.cpp | 15 +++++++++++ src/model/CompetitorWithResults.h | 28 +++++++++++++++++++++ src/model/MedalWinner.cpp | 18 +++++++++++++ src/model/MedalWinner.h | 35 ++++++++++++++++++++++++++ src/{discipline => model}/Sport.cpp | 0 src/{discipline => model}/Sport.h | 0 8 files changed, 150 insertions(+) create mode 100644 src/model/Competitor.cpp create mode 100644 src/model/Competitor.h create mode 100644 src/model/CompetitorWithResults.cpp create mode 100644 src/model/CompetitorWithResults.h create mode 100644 src/model/MedalWinner.cpp create mode 100644 src/model/MedalWinner.h rename src/{discipline => model}/Sport.cpp (100%) rename src/{discipline => model}/Sport.h (100%) diff --git a/src/model/Competitor.cpp b/src/model/Competitor.cpp new file mode 100644 index 0000000..a5d2e69 --- /dev/null +++ b/src/model/Competitor.cpp @@ -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; +} diff --git a/src/model/Competitor.h b/src/model/Competitor.h new file mode 100644 index 0000000..919c2fa --- /dev/null +++ b/src/model/Competitor.h @@ -0,0 +1,39 @@ + +#ifndef ITAT_CHALLANGE_OLYMPICS_COMPETITOR_H +#define ITAT_CHALLANGE_OLYMPICS_COMPETITOR_H + +#include +#include +#include +#include + +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 diff --git a/src/model/CompetitorWithResults.cpp b/src/model/CompetitorWithResults.cpp new file mode 100644 index 0000000..d29441d --- /dev/null +++ b/src/model/CompetitorWithResults.cpp @@ -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; +} diff --git a/src/model/CompetitorWithResults.h b/src/model/CompetitorWithResults.h new file mode 100644 index 0000000..cb952f2 --- /dev/null +++ b/src/model/CompetitorWithResults.h @@ -0,0 +1,28 @@ + +#ifndef ITAT_CHALLANGE_OLYMPICS_COMPETITORWITHRESULTS_H +#define ITAT_CHALLANGE_OLYMPICS_COMPETITORWITHRESULTS_H + +#include "Competitor.h" +#include +#include +#include +#include + +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 results; + +}; + + +#endif //ITAT_CHALLANGE_OLYMPICS_COMPETITORWITHRESULTS_H diff --git a/src/model/MedalWinner.cpp b/src/model/MedalWinner.cpp new file mode 100644 index 0000000..c4cb663 --- /dev/null +++ b/src/model/MedalWinner.cpp @@ -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; +} diff --git a/src/model/MedalWinner.h b/src/model/MedalWinner.h new file mode 100644 index 0000000..e771185 --- /dev/null +++ b/src/model/MedalWinner.h @@ -0,0 +1,35 @@ + +#ifndef ITAT_CHALLANGE_OLYMPICS_MEDALWINNER_H +#define ITAT_CHALLANGE_OLYMPICS_MEDALWINNER_H + +#include "Competitor.h" +#include +#include +#include + +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 wonMedals; + +}; + + +#endif //ITAT_CHALLANGE_OLYMPICS_MEDALWINNER_H diff --git a/src/discipline/Sport.cpp b/src/model/Sport.cpp similarity index 100% rename from src/discipline/Sport.cpp rename to src/model/Sport.cpp diff --git a/src/discipline/Sport.h b/src/model/Sport.h similarity index 100% rename from src/discipline/Sport.h rename to src/model/Sport.h