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
|
#include <iostream>
#include <sstream>
#include <string>
#include "Employee.cpp"
#include "OrdinaryEmployee.cpp"
//#include "SeniorEmployee.h"
using namespace std;
int dataCounter = 0;
const static int MAXIMUM_DATA_SIZE = 100;
Employee empArray[MAXIMUM_DATA_SIZE];
// To prevent invalid input type (int)
int getline_i()
{
while(cin.fail()) {
cin.clear();
}
string s;
getline(cin, s);
stringstream ss;
ss.str(s);
int i;
ss >> i;
return i;
}
// To prevent invalid input type (float)
float getline_f()
{
while(cin.fail()) {
cin.clear();
}
string s;
getline(cin, s);
stringstream ss;
ss.str(s);
float f;
ss >> f;
return f;
}
// Add new employee data function
Employee addEmpData()
{
int id;
string name;
string dob;
string address;
string phoneNo;
string email;
int deptNo;
float basicPay;
cout<<"\n[ Add new employee data ]\n"<<endl
<<"Enter new id : ";
id = getline_i();
cout<<"Enter new name : ";
getline(cin, name);
cout<<"Enter your date of birth : ";
getline(cin, dob);
cout<<"Enter your address : ";
getline(cin, address);
cout<<"Enter your phone number : ";
getline(cin, phoneNo);
cout<<"Enter your email address : ";
getline(cin, email);
cout<<"Enter your dept. number : ";
deptNo = getline_i();
cout<<"Enter your basic pay : ";
basicPay = getline_f();
cout<<"\n\nNew employee data have been keyed into the system...\n";
Employee emp = Employee(id, name, dob, address, phoneNo, email, deptNo, basicPay);
return emp;
}
string toUppercase(string s)
{
string upperS = s;
int sLen = s.length();
for(int i = 0; i < sLen; i++)
{
upperS[i] = toupper(upperS[i]);
}
return upperS;
}
void searchAndDisplayEmployee(string searchFor)
{
bool matchFound = false;
searchFor = toUppercase(searchFor);
for(int i = 0; i < dataCounter; i++)
{
string emp = toUppercase(empArray[i].getName());
if(emp.find(searchFor) != -1)
{
empArray[i].displayData();
cout << endl;
matchFound = true;
}
}
if(!matchFound)
{
cout << "No match found." << endl;
}
}
int main()
{
// Instantiate few employee data
empArray[0] = Employee(1, "John Tan", "12/09/1987", "Yishun", "98765432", "johntan@yahoo.com", 1, 2300.0);
empArray[1] = Employee(2, "Pierre Png", "13/04/1984", "Bedok", "91234567", "pierrepng@yahoo.com", 2, 4000.0);
empArray[2] = Employee(3, "Rose Lau", "17/03/1999", "Pasir Ris", "98674646", "rosly@gmail.com", 3, 1700.0);
empArray[3] = Employee(4, "Vincent Tan", "28/07/1986", "Braddel", "98787878", "vincenttan@live.com", 4, 3600.0);
empArray[4] = Employee(5, "Kelvin Lau", "16/03/1976", "Punggol", "90909093", "kelvinlau@gmail.com", 5, 5000.0);
//empArray[5] = OrdinaryEmployee(6, "Ronald Toh", "10/09/1967", "Yee Tew", "90909898", "ronaldtoh@live.com", 6, 6000.0, 20.0, 3.0, 20000);
//empArray[6] = OrdinaryEmployee(7, "Eric Kwek", "09/09/1983", "Orchard", "91234123", "erickwek@live.com", 7, 7000.0, 30.0, 4.0, 30000);
dataCounter += 5;
int choice = 0;
while(true) {
cout<<"\n\n";
cout<<"Welcome to the Salary System" <<endl
<<"1) Add New Employee Data" <<endl
<<"2) Display Employee's Profile" <<endl
<<"3) Search for Employee by Name" <<endl
<<"4) Quit"
<<endl;
// Waiting for user to enter choice
cout<<"Please enter your choice: ";
choice = getline_i();
if( choice < 1 || choice > 4 ) {
cout<<"Invalid input ! Please enter your choice again...";
choice = 0;
}
if(choice == 1) {
if(dataCounter < MAXIMUM_DATA_SIZE)
{
empArray[dataCounter++] = addEmpData();
}
} // End of option 1
else if(choice == 2) {
for(int i = 0; i < dataCounter; i++)
{
empArray[i].displayData();
}
} // End of Option 2
else if(choice == 3) {
string searchFor;
stringstream ss;
ss.str("");
cout<<"\nSearch by Employee's name : ";
getline(cin, searchFor);
ss << searchFor;
searchFor = ss.str();
cout<<"\n";
searchAndDisplayEmployee(searchFor);
} // End of option 3
else if(choice == 4) {
cout<< "\nHave a nice day, good bye.\n\n";
break;
} // End of option 3
}
}
|