From b825a98d95dd9fa432a161d95eb6454536e01af4 Mon Sep 17 00:00:00 2001 From: Steru Date: Fri, 16 Aug 2024 15:39:05 +0200 Subject: Fixed constructors. --- src/model/Competitor.cpp | 13 ++++++++++--- src/model/Competitor.h | 6 +++--- src/model/CompetitorWithResults.cpp | 17 ++++++++++++++++- src/model/CompetitorWithResults.h | 16 ++++++---------- src/model/MedalWinner.cpp | 25 +++++++++++++++++++++---- src/model/MedalWinner.h | 18 +++++++++--------- 6 files changed, 65 insertions(+), 30 deletions(-) diff --git a/src/model/Competitor.cpp b/src/model/Competitor.cpp index 080dd4d..a584689 100644 --- a/src/model/Competitor.cpp +++ b/src/model/Competitor.cpp @@ -8,8 +8,15 @@ bool Competitor::setCompetitor(const QJsonObject &competitor) { throw invalid_argument("Not a competitor object."); } - this->m_code = competitor["code"].toInt(); - this->m_name = competitor["name"].toString(); - this->m_noc = competitor["m_noc"].toString(); + setCode(competitor["code"].toInt()); + setName(competitor["name"].toString()); + setNOC(competitor["m_noc"].toString()); + return true; +} + +bool Competitor::setCompetitor(const Competitor &competitor) { + setCode(competitor.m_code); + setName(competitor.m_name); + setNOC(competitor.m_noc); return true; } diff --git a/src/model/Competitor.h b/src/model/Competitor.h index 79552a5..1913325 100644 --- a/src/model/Competitor.h +++ b/src/model/Competitor.h @@ -5,6 +5,7 @@ #include #include #include +#include using namespace std; @@ -18,9 +19,7 @@ class Competitor : public QObject { Q_PROPERTY(QString noc READ noc NOTIFY nNoc) public: - Competitor() : m_code(0), m_name(""), m_noc("") {} - Competitor(const Competitor &competitor) : m_code(competitor.m_code), m_name(competitor.m_name), m_noc(competitor.m_noc) {} - Competitor(const QJsonObject &competitor) { setCompetitor(competitor); } + explicit Competitor(QObject *parent) : QObject(parent) {} int getCode() { return this->m_code; } QString getName() { return this->m_name; } @@ -31,6 +30,7 @@ public: void setNOC(QString noc) { this->m_noc = noc; } bool setCompetitor(const QJsonObject &competitor); + bool setCompetitor(const Competitor &competitor); static bool compareName(const Competitor &left, const Competitor &right) { return left.m_name.compare(right.m_name) < 0; diff --git a/src/model/CompetitorWithResults.cpp b/src/model/CompetitorWithResults.cpp index 9dbd615..70d9473 100644 --- a/src/model/CompetitorWithResults.cpp +++ b/src/model/CompetitorWithResults.cpp @@ -10,7 +10,7 @@ bool CompetitorWithResults::setResults(const QJsonObject &results) { if (!results.contains("m_mark") || !results.contains("m_medalType")) { - throw invalid_argument("Results object of competitor is incomplete."); + return false; } this->m_mark = results["m_mark"].toString(); @@ -44,3 +44,18 @@ bool CompetitorWithResults::compare(CompetitorWithResults lComp, CompetitorWithR return lTime.compare(rTime) < 0; } + +bool CompetitorWithResults::setCompetitorWithResults(const QJsonObject &competitor) { + setCompetitor(competitor); + + if (!competitor.contains("results")) return false; + QJsonObject results = competitor["results"].toObject(); + return setResults(results); +} + +void CompetitorWithResults::setCompetitorWithResults(const CompetitorWithResults &competitor) { + setCompetitor(competitor); + setMark(competitor.m_mark); + setMedalType(competitor.m_medalType); + setStatistic(competitor.m_statistic); +} diff --git a/src/model/CompetitorWithResults.h b/src/model/CompetitorWithResults.h index 2203064..a98a1e8 100644 --- a/src/model/CompetitorWithResults.h +++ b/src/model/CompetitorWithResults.h @@ -6,7 +6,6 @@ #include #include #include -#include class CompetitorWithResults : public Competitor { @@ -17,19 +16,16 @@ class CompetitorWithResults : public Competitor { Q_PROPERTY(QString statistic READ statistic NOTIFY nStatistic) public: - CompetitorWithResults() : Competitor(), m_mark(""), m_medalType("") {} - CompetitorWithResults(const CompetitorWithResults &competitor) : Competitor(competitor), - m_mark(competitor.m_mark), m_medalType(competitor.m_medalType) {} - - 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); - } + explicit CompetitorWithResults(Competitor *parent) : Competitor(parent) {} bool setResults(const QJsonObject &results); + void setMark(QString mark) { this->m_mark = mark; } + void setMedalType(QString medalType) { this->m_medalType = medalType; } void setStatistic(QString stat) { this->m_statistic = stat; } + bool setCompetitorWithResults(const QJsonObject &competitor); + void setCompetitorWithResults(const CompetitorWithResults &competitor); + QString getMark() { return this->m_mark; } QString getMedalType() { return this->m_medalType; } QString getStatistic() { return this->m_statistic; } diff --git a/src/model/MedalWinner.cpp b/src/model/MedalWinner.cpp index bdf6ee3..56cfb22 100644 --- a/src/model/MedalWinner.cpp +++ b/src/model/MedalWinner.cpp @@ -11,12 +11,12 @@ 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."); + return false; } - this->m_gold = medals["ME_GOLD"].toInt(); - this->m_silver = medals["ME_SILVER"].toInt(); - this->m_bronze = medals["ME_BRONZE"].toInt(); + setGold(medals["ME_GOLD"].toInt()); + setSilver(medals["ME_SILVER"].toInt()); + setBronze(medals["ME_BRONZE"].toInt()); return true; } @@ -38,3 +38,20 @@ bool MedalWinner::compare(MedalWinner lComp, MedalWinner rComp) { // compare medal differences return gold < 0 || (gold == 0 && (silver < 0 || (silver == 0 && bronze < 0))); } + +bool MedalWinner::setMedalWinner(const QJsonObject &competitor) { + setCompetitor(competitor); + + if (!competitor.contains("medals")) return false; + QJsonObject medals = competitor["medals"].toObject(); + setMedals(medals); + return true; +} + +void MedalWinner::setMedalWinner(const MedalWinner &competitor) { + setCompetitor(competitor); + + setGold(competitor.m_gold); + setSilver(competitor.m_silver); + setBronze(competitor.m_bronze); +} diff --git a/src/model/MedalWinner.h b/src/model/MedalWinner.h index 45d1ee8..10d08bd 100644 --- a/src/model/MedalWinner.h +++ b/src/model/MedalWinner.h @@ -4,7 +4,6 @@ #include "Competitor.h" #include #include -#include #include @@ -17,16 +16,17 @@ class MedalWinner : public Competitor { Q_PROPERTY(int bronze READ m_bronze NOTIFY nBronze) public: - MedalWinner() : Competitor(), m_gold(0), m_silver(0), m_bronze(0) {} - MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner), - m_gold(medalWinner.m_gold), m_silver(medalWinner.m_silver), m_bronze(medalWinner.m_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); - } + explicit MedalWinner(Competitor *parent) : Competitor(parent) {} + + bool setMedalWinner(const QJsonObject &competitor); + void setMedalWinner(const MedalWinner &competitor); bool setMedals(const QJsonObject &medals); + + void setGold(int gold) { this->m_gold = gold; } + void setSilver(int silver) { this->m_silver = silver; } + void setBronze(int bronze) { this->m_bronze = bronze; } + int getGold() { return m_gold; } int getSilver() { return m_silver; } int getBronze() { return m_bronze; } -- cgit v1.2.3