/
party.cpp
130 lines (117 loc) · 3.28 KB
/
party.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include"Party.h"
#include "DistrictArr.h"
Party::Party() {
name = "";
id = -1;
RepsTotal = -1;
votes_got = -1;
}
Party::Party(istream& in, DistrictArr& darr)
{
int PresId;
getline(in, name, '\0');
in.read(rcastc(&id), sizeof(id));
in.read(rcastc(&votes_got), sizeof(votes_got));
in.read(rcastc(&RepsTotal), sizeof(RepsTotal));
in.read(rcastc(&PresId), sizeof(PresId));
if (!in.good())
throw Except("Loading Error");
president = darr.getCitizenFrom(PresId);
if(president == nullptr)
throw Except("Loading Error");
repsByDis.load(in, darr);
}
Party::Party(string _partyName, int _partyId, const Citizen* _president) {
name = _partyName;
id = _partyId;
president = _president;
RepsTotal = 0;
votes_got = 0;
}
void Party::addVotes(int add) {
votes_got += add;
}
bool Party::setPartyName(string _name) {
name = _name;
return true;
}
bool Party::setRepresentativesTotal(int num) {
RepsTotal = num;
return true;
}
bool Party::setPartyId(int id)
{
id = id;
return true;
}
void Party::addVotesGot(int votes)
{
votes_got += votes;
}
const Reps* Party::getRepsListByDis(int disNumber) const {
return repsByDis[disNumber];
}
void Party::addReps(const Citizen* representative,int distictId) {
repsByDis.addRep(representative, distictId);
}
void Party::addDistrict(const District* district_id)
{
repsByDis.addDistrict(district_id);
}
void Party::addElectoralVotes()
{
const District* d;
int party_votes;
votes_got = 0;
RepsTotal = 0;
for (int i = 0; i < repsByDis.size(); i++)
{
d = repsByDis[i]->getDistrict();
addToRepsTotal(d->getRepsOfParty(id));
party_votes = d->getVotesOfParty(id);
addVotesGot(party_votes);
}
}
int Party::addToRepsTotal(int won)
{
RepsTotal += won;
return OK;
}
ostream& operator<<(ostream& os,Party& party){
os << "*************************************************" << endl;
os << "Party Name:" << party.getName() << endl<<
"Party Number: " << party.getId() << endl <<
"Candidate for Head of State: " << party.getPresident().getName() << endl;
if (party.getRepsList().size()) os << "List of Representatives: " << endl;
os << "**********************************" << endl
<< party.getRepsList()
<< "******************************" << endl << endl;
return os;
}
ostream& operator<<(ostream& os, const Party& party) {
os << "*************************************************" << endl;
os << "Party Name:" << party.getName() << endl <<
"Party Number: " << party.getId() << endl <<
"Candidate for Head of State: " << party.getPresident().getName() << endl;
if (party.getRepsList().size()) os << "List of Representatives: " << endl;
os << "**********************************" << endl
<< party.getRepsList()
<< "******************************" << endl << endl;
return os;
}
bool operator<(Party const& a, Party const& b)
{
return a.RepsTotal > b.RepsTotal;
}
bool Party::save(ostream& out) const
{
int presid = president->getID();
out.write(name.c_str(), name.size());
out.write("\0", sizeof(char));
out.write(rcastcc(&id), sizeof(id));
out.write(rcastcc(&votes_got), sizeof(votes_got));
out.write(rcastcc(&RepsTotal), sizeof(RepsTotal));
out.write(rcastcc(&presid), sizeof(int));
repsByDis.save(out);
return out.good();
}