-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounty.cpp
152 lines (152 loc) · 4.27 KB
/
County.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#define _CRT_SECURE_NO_WARNINGS
#include "County.h"
#include <iostream>
using namespace std;
namespace votes
{
int County::countyCounter=0;
County::County(const string& countyName, int numdelegates)
{
string errorName;
if (numdelegates <= 0)
throw (errorName = "Number of delegate must be a positive number");
_countyName = countyName;
_numdelegates = numdelegates;
_countySerial=++countyCounter;
}
CountyDelegate* County::getDelgate(int delgatePlace)const
{
return CDArr.at(delgatePlace);
}
ostream& operator<<(ostream& os, const County& county)
{
os<< "County Number:"<<county._countySerial << " County Name:" << county._countyName << " Amount of Delegates:" << county._numdelegates;
return os;
}
County::~County()
{
for (int i = 0; i < CDArr.size(); i++)
delete CDArr.at(i);
list<Citizen*>::const_iterator itr = _citizenAllowed.begin();
list<Citizen*>::const_iterator end = _citizenAllowed.end();
for (; itr != end; ++itr)
delete(*itr);
}
County::County()
{
_numdelegates =-1;
_countySerial =-1;
}
void County::AddCD(Citizen* delegate,Party* party)
{
CountyDelegate* CurrDelegate = new CountyDelegate(delegate, party);
CDArr.push_back(CurrDelegate);
}
Citizen* County::searchCitizen(int id)const
{
return findCitizen(id);
}
void County::getCountyVotes(vector<int> &votearr)
{
getVotes(votearr);
}
void County::PrintCitizenList() const
{
PrintList(_countyName);
}
void County::saveCounty(ostream& out) const
{
out.write(rcastcc(&_countySerial), sizeof(_countySerial));
out.write(rcastcc(&_numdelegates), sizeof(_numdelegates));
out.write(rcastcc(&countyCounter), sizeof(countyCounter));
int countyNamelen = static_cast<int> (_countyName.size());
out.write(rcastcc(&countyNamelen), sizeof(countyNamelen));
out.write(rcastcc(&_countyName[0]), sizeof(char) * countyNamelen);
saveCitizensList(out);
}
void County::loadCounty(istream& in)
{
in.read(rcastc(&_countySerial), sizeof(_countySerial));
in.read(rcastc(&_numdelegates), sizeof(_numdelegates));
in.read(rcastc(&countyCounter), sizeof(countyCounter));
int countyNamelen = 0;
in.read(rcastc(&countyNamelen), sizeof(countyNamelen));
_countyName.resize(countyNamelen);
in.read(rcastc(&_countyName[0]), sizeof(char) * countyNamelen);
loadCitizensList(in);
}
//citizenlist:
void County::AddCitizen(const string& name, int id, int year)
{
Citizen* newCitizen = new Citizen(name, id, year);
_citizenAllowed.push_back(newCitizen);
}
Citizen* County::getCitizenByIndex(int index) const
{
list<Citizen*>::const_iterator it = _citizenAllowed.begin();
advance(it, index);
return *it;
}
void County::PrintList(const string& countyName) const
{
list<Citizen*>::const_iterator itr = _citizenAllowed.begin();
list<Citizen*>::const_iterator end = _citizenAllowed.end();
for (; itr != end; ++itr)
{
cout << **itr;
cout << " County: " << countyName << endl;
}
}
void County::getVotes(vector<int>& voteArr) const
{
int currentVote;
list<Citizen*>::const_iterator itr = _citizenAllowed.begin();
list<Citizen*>::const_iterator end = _citizenAllowed.end();
const Party* PartyVotedTo;
for (; itr != end; ++itr)
{
PartyVotedTo = (*itr)->getVote();
if (PartyVotedTo)
currentVote = PartyVotedTo->getPartySerial();
else
currentVote = -1;
if (currentVote != -1)
{
voteArr[currentVote]++;
voteArr[0]++;
}
}
}
Citizen* County::findCitizen(int id) const
{
list<Citizen*>::const_iterator itr = _citizenAllowed.begin();
list<Citizen*>::const_iterator end = _citizenAllowed.end();
for (; itr != end; ++itr)
{
if ((*itr)->getID() == id)
return *itr;
}
return nullptr;
}
void County::saveCitizensList(ostream& out) const
{
int size = static_cast<int>(_citizenAllowed.size());
out.write(rcastcc(&size), sizeof(size));
list<Citizen*>::const_iterator itr = _citizenAllowed.begin();
list<Citizen*>::const_iterator end = _citizenAllowed.end();
for (; itr != end; ++itr)
(*itr)->saveCitizen(out);
}
void County::loadCitizensList(istream& in)
{
string errorName;
int loadSize = static_cast<int>(_citizenAllowed.size());
in.read(rcastc(&loadSize), sizeof(loadSize));
for (int i = 0; i < loadSize; i++)
{
Citizen* toadd = new Citizen();
toadd->loadCitizen(in);
_citizenAllowed.push_back(toadd);
}
}
}