Fixed constructors.

This commit is contained in:
Steru 2024-08-16 15:39:05 +02:00
parent 66d5d8685f
commit 3918de89ce
6 changed files with 65 additions and 30 deletions

View file

@ -8,8 +8,15 @@ bool Competitor::setCompetitor(const QJsonObject &competitor) {
throw invalid_argument("Not a competitor object."); throw invalid_argument("Not a competitor object.");
} }
this->m_code = competitor["code"].toInt(); setCode(competitor["code"].toInt());
this->m_name = competitor["name"].toString(); setName(competitor["name"].toString());
this->m_noc = competitor["m_noc"].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; return true;
} }

View file

@ -5,6 +5,7 @@
#include <QMap> #include <QMap>
#include <QJsonObject> #include <QJsonObject>
#include <QObject> #include <QObject>
#include <qqml.h>
using namespace std; using namespace std;
@ -18,9 +19,7 @@ class Competitor : public QObject {
Q_PROPERTY(QString noc READ noc NOTIFY nNoc) Q_PROPERTY(QString noc READ noc NOTIFY nNoc)
public: public:
Competitor() : m_code(0), m_name(""), m_noc("") {} explicit Competitor(QObject *parent) : QObject(parent) {}
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); }
int getCode() { return this->m_code; } int getCode() { return this->m_code; }
QString getName() { return this->m_name; } QString getName() { return this->m_name; }
@ -31,6 +30,7 @@ public:
void setNOC(QString noc) { this->m_noc = noc; } void setNOC(QString noc) { this->m_noc = noc; }
bool setCompetitor(const QJsonObject &competitor); bool setCompetitor(const QJsonObject &competitor);
bool setCompetitor(const Competitor &competitor);
static bool compareName(const Competitor &left, const Competitor &right) { static bool compareName(const Competitor &left, const Competitor &right) {
return left.m_name.compare(right.m_name) < 0; return left.m_name.compare(right.m_name) < 0;

View file

@ -10,7 +10,7 @@
bool CompetitorWithResults::setResults(const QJsonObject &results) { bool CompetitorWithResults::setResults(const QJsonObject &results) {
if (!results.contains("m_mark") if (!results.contains("m_mark")
|| !results.contains("m_medalType")) { || !results.contains("m_medalType")) {
throw invalid_argument("Results object of competitor is incomplete."); return false;
} }
this->m_mark = results["m_mark"].toString(); this->m_mark = results["m_mark"].toString();
@ -44,3 +44,18 @@ bool CompetitorWithResults::compare(CompetitorWithResults lComp, CompetitorWithR
return lTime.compare(rTime) < 0; 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);
}

View file

@ -6,7 +6,6 @@
#include <QMap> #include <QMap>
#include <QJsonObject> #include <QJsonObject>
#include <QObject> #include <QObject>
#include <stdexcept>
class CompetitorWithResults : public Competitor { class CompetitorWithResults : public Competitor {
@ -17,19 +16,16 @@ class CompetitorWithResults : public Competitor {
Q_PROPERTY(QString statistic READ statistic NOTIFY nStatistic) Q_PROPERTY(QString statistic READ statistic NOTIFY nStatistic)
public: public:
CompetitorWithResults() : Competitor(), m_mark(""), m_medalType("") {} explicit CompetitorWithResults(Competitor *parent) : Competitor(parent) {}
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);
}
bool setResults(const QJsonObject &results); 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; } 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 getMark() { return this->m_mark; }
QString getMedalType() { return this->m_medalType; } QString getMedalType() { return this->m_medalType; }
QString getStatistic() { return this->m_statistic; } QString getStatistic() { return this->m_statistic; }

View file

@ -11,12 +11,12 @@ bool MedalWinner::setMedals(const QJsonObject &medals) {
if (!medals.contains("ME_GOLD") if (!medals.contains("ME_GOLD")
|| !medals.contains("ME_SILVER") || !medals.contains("ME_SILVER")
|| !medals.contains("ME_BRONZE")) { || !medals.contains("ME_BRONZE")) {
throw invalid_argument("Medal object of competitor is incomplete."); return false;
} }
this->m_gold = medals["ME_GOLD"].toInt(); setGold(medals["ME_GOLD"].toInt());
this->m_silver = medals["ME_SILVER"].toInt(); setSilver(medals["ME_SILVER"].toInt());
this->m_bronze = medals["ME_BRONZE"].toInt(); setBronze(medals["ME_BRONZE"].toInt());
return true; return true;
} }
@ -38,3 +38,20 @@ bool MedalWinner::compare(MedalWinner lComp, MedalWinner rComp) {
// compare medal differences // compare medal differences
return gold < 0 || (gold == 0 && (silver < 0 || (silver == 0 && bronze < 0))); 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);
}

View file

@ -4,7 +4,6 @@
#include "Competitor.h" #include "Competitor.h"
#include <QMap> #include <QMap>
#include <QJsonObject> #include <QJsonObject>
#include <stdexcept>
#include <QAbstractListModel> #include <QAbstractListModel>
@ -17,16 +16,17 @@ class MedalWinner : public Competitor {
Q_PROPERTY(int bronze READ m_bronze NOTIFY nBronze) Q_PROPERTY(int bronze READ m_bronze NOTIFY nBronze)
public: public:
MedalWinner() : Competitor(), m_gold(0), m_silver(0), m_bronze(0) {} explicit MedalWinner(Competitor *parent) : Competitor(parent) {}
MedalWinner(const MedalWinner &medalWinner) : Competitor(medalWinner),
m_gold(medalWinner.m_gold), m_silver(medalWinner.m_silver), m_bronze(medalWinner.m_bronze) {} bool setMedalWinner(const QJsonObject &competitor);
MedalWinner(const QJsonObject &competitor) : Competitor(competitor) { void setMedalWinner(const MedalWinner &competitor);
if (!competitor.contains("medals")) throw invalid_argument("Competitor has no medals.");
QJsonObject medals = competitor["medals"].toObject();
setMedals(medals);
}
bool setMedals(const QJsonObject &medals); 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 getGold() { return m_gold; }
int getSilver() { return m_silver; } int getSilver() { return m_silver; }
int getBronze() { return m_bronze; } int getBronze() { return m_bronze; }