-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEmployee.cpp
262 lines (223 loc) · 8.17 KB
/
Employee.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "Employee.h" // class implemented
#include<string>
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Employee Default + Overloaded constructor
Employee::Employee(const string& aName, const string& aNIC, const Date& aDateOfBirth, const Date& aDateOfHire, const Address& aHomeAddress, double aTaxRate, double aBasicSalary) : mName(aName), mNIC(aNIC), mBirthDate(aDateOfBirth), mHireDate(aDateOfHire), mHomeAddress(aHomeAddress), mTaxRate(aTaxRate), mBasicSalary(aBasicSalary) {
// initialization using member intializing list
Employee::msNoOfEmployees++; // increment no. of employees within constructor
}
// end Employee constructor
// Employee Copy constructor
Employee::Employee(const Employee& obj) : mName(obj.GetName()), mNIC(obj.GetNIC()), mBirthDate(obj.GetDOB()), mHireDate(obj.GetDOJ()), mHomeAddress(obj.GetHomeAddress()), mTaxRate(obj.GetTaxRate()), mBasicSalary(obj.GetBasicSalary()) {
Employee::msNoOfEmployees++; // increment no. of employees within constructor
}
// end Employee constructor
// Emplopyee destructor
Employee::~Employee() {
Employee::msNoOfEmployees--; // decrement no. of employees each time destructor is called
}
// end ~Employee destructor
//============================= OPERATORS ====================================
// Stream Insertion
ostream & operator <<(ostream& os, const Employee& e) {
os << "Name: " << e.GetName() << endl;
os << "NIC: " << e.GetNIC() << endl;
os << "DOB: " << e.GetDOB() << endl;
os << "DOJ: " << e.GetDOJ() << endl;
os << "Address: " << e.GetHomeAddress() << endl;
os << "Tax Rate: " << e.GetTaxRate() << endl;
os << "Basic Salary: " << e.GetBasicSalary() << endl;
return os;
}
// end stream insertion
// Stream Extraction
istream & operator >>(istream & is, Employee & e) {
string aName, aNIC;
Address aAddress;
double aTaxrate, aSalary;
cout << "Enter name of Employee: ";
getline(is, aName);
cout << "Enter NIC of Employee: ";
getline(is, aNIC);
is >> aAddress;
cout << "Enter tax rate: ";
is >> aTaxrate;
cout << "Enter basic salary: ";
is >> aSalary;
e.SetEmployee(aName, aNIC, aAddress, aTaxrate, aSalary);
return is;
}
// end Stream Extraction
//============================= OPERATIONS ===================================
// function that prints all the details of the employee.
void Employee::PrintEmployee()const {
cout << "Name: " << this->GetName() << endl;
cout << "NIC: " << this->GetNIC() << endl;
cout << "DOB: " << this->GetDOB() << endl;
cout << "DOJ: " << this->GetDOJ() << endl;
cout << "Address: " << this->GetHomeAddress() << endl;
cout << "Tax Rate: " << this->GetTaxRate() << endl;
cout << "Basic Salary: " << this->GetBasicSalary() << endl;
}
// end function PrintEmployee
//============================= ACESS ===================================
// function that sets name of Employee
void Employee::SetName(const string& aName) {
if (this->IsValidName(aName))
this->mName = aName;
else
cout << "ERROR: Invalid name entered. Cannot set name." << endl;
}
// end function SetName
// function that sets NIC of Employee
void Employee::SetNIC(const string& aNIC) {
if (this->IsValidNIC(aNIC))
this->mNIC = aNIC;
else
cout << "ERROR: Invalid NIC entered. Cannot set NIC." << endl;
}
// end function SetNIC
// function that sets Home Address of Employee
void Employee::SetHomeAddress(int aHouseNo, const string& aStreet, const string& aCity, const string& aProvince, int aPostCode) {
this->mHomeAddress.SetAddress(aHouseNo, aStreet, aCity, aProvince, aPostCode);
}
// end function SetHomeAddress
// overloaded function that sets Home Address of Employee
void Employee::SetHomeAddress(const Address& add) {
this->mHomeAddress.SetAddress(add);
}
// end function SetHomeAddress
// function that set ZIP Code value of Employee Home Address
void Employee::SetZIP(int aZIP) {
this->mHomeAddress.SetPostCode(aZIP);
}
// end function SetZIP
// function that sets Tax Rate of Employee
void Employee::SetTaxRate(double aTaxRate) {
if (aTaxRate < 0.0)
cout << "Tax rate cannot be negative.";
else
this->mTaxRate = aTaxRate;
}
// end function SetTaxRate
// function that sets Basic Salary of Employee
void Employee::SetBasicSalary(double aBasicSalary) {
if (aBasicSalary < 0.0)
cout << "Basic salary cannot be negative.";
else
this->mBasicSalary = aBasicSalary;
}
// end function SetBasicSalary
// function that sets the Employee
void Employee::SetEmployee(const string& aName, const string& aNIC, const Address& aAddress, double aTaxRate, double aBasicSalary) {
this->SetName(aName);
this->SetNIC(aNIC);
this->SetHomeAddress(aAddress);
this->SetTaxRate(aTaxRate);
this->SetBasicSalary(aBasicSalary);
}
// end function SetEmployee
// overloaded function that sets the Employee
void Employee::SetEmployee(const Employee& obj) {
this->SetEmployee(obj.GetName(), obj.GetNIC(), obj.GetHomeAddress(), obj.GetTaxRate(), obj.GetBasicSalary());
}
// end function SetEmployee
// function that gets name of the Employee
const string& Employee::GetName()const {
return this->mName;
}
// end function GetName
// function that gets NIC of the Employee
const string& Employee::GetNIC()const {
return this->mNIC;
}
// end function GetNIC
// function that gets DOB of the Employee
const Date& Employee::GetDOB()const {
return this->mBirthDate;
}
// end function GetDOB
// function that gets DOJ of the Employee
const Date& Employee::GetDOJ()const {
return this->mHireDate;
}
// end function GetDOJ
// function that gets Home Address of the Employee
const Address& Employee::GetHomeAddress()const {
return this->mHomeAddress;
}
// end function GetHomeAddress
// function that gets ZIP code of Employee's Home Address
int Employee::GetZIP()const {
return this->mHomeAddress.GetPostCode();
}
// end function GetZIP
// function that gets Tax Rate of the Employee
double Employee::GetTaxRate()const {
return this->mTaxRate;
}
// end function GetTaxRate
// function that gets Basic Salary of the Employee
double Employee::GetBasicSalary()const {
return this->mBasicSalary;
}
// end function GetBasicSalary
// function that gets the Employee
const Employee& Employee::GetEmployee()const {
return *this;
}
// end function GetEmployee
// static function that gets the total no. of Employees
int Employee::sGetTotalEmployee() {
return Employee::msNoOfEmployees;
}
// end function sGetTotalEmployee
/////////////////////////////// PRIVATE ///////////////////////////////////
//============================= INQUIRY ===================================
/** utility function to check name of employee.
* Only checks for alphabetic characters and first cap letter.
*
* @param testName The reference to the name to be tested.
*
* @return true if testName is valid, false otherwise.
*/
bool Employee::IsValidName(const string& testName)const {
// first letter must be capital
if (!isupper(testName[0]))
return false;
for (int i = 0; i < testName.length(); i++) {
if (!isalpha(testName[i]) && testName[i] != ' ' && testName[i] != '.')
return false;
if (testName[i] == ' ' && islower(testName[i + 1]))
return false;
if (testName[i] == '.' && islower(testName[i + 1]))
return false;
}
return true;
}
// end function IsValidName
/** utility function to check validity of NIC
* Only checks for alphanumeric characters and
* length of NIC (<16 characters). Allows '-' char.
*
* @param testNIC The reference to the NIC to be tested.
*
* @return true if testNIC is valid, false otherwise.
*/
bool Employee::IsValidNIC(const string& testNIC)const {
if (testNIC.length() > 15)
return false;
for (int i = 0; i < testNIC.length(); i++) {
if (islower(testNIC[i]))
return false;
if (!isalnum(testNIC[i]) && testNIC[i] != '-')
return false;
}
return true;
}
// end function IsValidNIC
/*private static member cannot be accessed outside the class except for initialization*/
int Employee::msNoOfEmployees = 0; // intitalize class variable