Fixed constructors.
This commit is contained in:
parent
b869b89836
commit
b825a98d95
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QObject>
|
||||
#include <qqml.h>
|
||||
|
||||
|
||||
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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QObject>
|
||||
#include <stdexcept>
|
||||
|
||||
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; }
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
#include "Competitor.h"
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <QAbstractListModel>
|
||||
|
||||
|
@ -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; }
|
||||
|
|
Loading…
Reference in a new issue