-
Notifications
You must be signed in to change notification settings - Fork 0
/
Citizen.cpp
96 lines (85 loc) · 2.26 KB
/
Citizen.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
#include "Citizen.h"
#include "District.h"
namespace elc {
Citizen::Citizen(string name, District* district, int id, int bday, int districtid)
: _name(name), _District(district), _ID(id), _BDay(bday), _DistrictID(districtid)
{
try {
if (to_string(id).length() != 9) {
_name.clear();
throw invalid_argument(" Invalid ID input");
}
}
catch (bad_alloc& ex) {
cout << ex.what() << endl;
exit(1);
}
}
Citizen::Citizen(const Citizen& c) :
Citizen(c.getName(), c.Citizen::getDistrict(), c.getID(), c.getDate(), c.getDistrictID())
{
}
Citizen::Citizen(istream& in) : Citizen("", nullptr, 999999999, 0, 0)
{
load(in);
}
Citizen::~Citizen()
{
}
const Citizen& Citizen::operator=(const Citizen& other)
{
if (this != &other)
{
_name = other.getName();
_District = other.getDistrict();
_ID = other.getID();
_BDay = other.getDate();
_Voted = other.getVoted();
}
return *this;
}
bool Citizen::operator==(const Citizen& c) const
{
return (this->getID() == c.getID());
}
ostream& Citizen::print(ostream& os) const
{
os << "\t ID:" << this->getID() << endl;
os << "\t Name: " << this->getName() << endl;
os << "\t Birth Year: " << this->getDate() << endl;
os << "\t District : " << this->getDistrict()->getName() << endl;
os << "\t Not Representative" << endl;
return os;
}
void Citizen::load(istream& in)
{
int len;
in.read(rcastc(&len), sizeof(len));
char* c;
try{
c = new char[len + 1];
}catch (bad_alloc& ex) {
cout << ex.what() << endl;
exit(1);
}
in.read(rcastc(c), len);
c[len] = '\0';
_name = c;
delete c;
in.read(rcastc(&_DistrictID), sizeof(_DistrictID));
in.read(rcastc(&_ID), sizeof(_ID));
in.read(rcastc(&_BDay), sizeof(_BDay));
in.read(rcastc(&_Voted), sizeof(_Voted));
}
void Citizen::save(ostream& out) const
{
int len = _name.size();
out.write(rcastcc(&len), sizeof(len));
const char* c = _name.c_str();
out.write(rcastcc(c), len);
out.write(rcastcc(&_DistrictID), sizeof(_DistrictID));
out.write(rcastcc(&_ID), sizeof(_ID));
out.write(rcastcc(&_BDay), sizeof(_BDay));
out.write(rcastcc(&_Voted), sizeof(_Voted));
}
}