Skip to content

Commit 16dfb4d

Browse files
fixed folder structure
1 parent cb0df5d commit 16dfb4d

File tree

1 file changed

+330
-0
lines changed

1 file changed

+330
-0
lines changed

src/employees.cpp

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <numeric>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <iterator>
7+
#include <fstream>
8+
#include <sstream>
9+
10+
using namespace std;
11+
12+
class Employee{
13+
private:
14+
int id, salary;
15+
string name, department;
16+
17+
public:
18+
// constructor
19+
Employee(int id, string n, string d, int s) : id(id), name(n), department(d), salary(s) {}
20+
21+
// Getter
22+
int getID() const{
23+
return id;
24+
}
25+
int getSalary() const{
26+
return salary;
27+
}
28+
string getName() const{
29+
return name;
30+
}
31+
string getDepartment() const{
32+
return department;
33+
}
34+
35+
// Setter
36+
void setSalary(int sal) {
37+
salary = sal;
38+
}
39+
void setName(string n) {
40+
name = n;
41+
}
42+
void setDepartement(string dep) {
43+
department = dep;
44+
}
45+
46+
47+
// methods to be used
48+
49+
// display details of an employee
50+
void displayEmployee() const{
51+
cout << "ID: " << id <<endl;
52+
cout << "Name: " << name <<endl;
53+
cout << "Department: " << department <<endl;
54+
cout << "Salary: Rs " << salary <<endl<<endl;
55+
}
56+
57+
// update existing employee
58+
void updateEmpData(string n, string depart, int sal){
59+
name=n;
60+
department=depart;
61+
salary=sal;
62+
63+
cout << "Data of emp id " << id << " is updated\n" ;
64+
}
65+
66+
};
67+
68+
69+
// Utility fucntions
70+
71+
// Functions to take inputs
72+
int takeID(){
73+
int id;
74+
cin >> id;
75+
return id;
76+
}
77+
int takeSalary(){
78+
int sal;
79+
cin >> sal;
80+
return sal;
81+
}
82+
string takeName(){
83+
string name;
84+
cin.ignore(); // will be used after some int input everytime
85+
getline(cin, name);
86+
return name;
87+
}
88+
string takeDepartment(){
89+
string dep;
90+
getline(cin, dep); // will be used after takeName() so no cin.ignore() req
91+
return dep;
92+
}
93+
94+
// Check if the user exist - this will be used by other functions and methods
95+
// it is not of bool type or Employee type for their lack of functionality
96+
Employee* empExist(vector<Employee> &emps, int id){
97+
for(auto &e : emps){
98+
if (e.getID()==id){
99+
return &e;
100+
}
101+
}
102+
return nullptr;
103+
}
104+
105+
// Quick function to print the emp not found message
106+
void empNotFoundMsg(int id){ cout << "Employee with ID " << id << " not found.\n"; }
107+
108+
109+
110+
// All the performable operations
111+
// 1 - Display all employees
112+
void displayAll(vector<Employee> &emps){
113+
for (const auto &e : emps){
114+
e.displayEmployee();
115+
}
116+
}
117+
// 2 - Search and employee
118+
void searchEmp(vector<Employee> &emps){
119+
cout << "Enter employee id to be searched: ";
120+
int id = takeID();
121+
Employee* empPtr = empExist(emps, id);
122+
if (empPtr) {
123+
cout << "Employee found here are the details:-\n";
124+
empPtr->displayEmployee();
125+
} else {
126+
empNotFoundMsg(id);
127+
}
128+
}
129+
130+
// 3 - Add employee
131+
void addEmps(vector<Employee> &emps){
132+
int n;
133+
cout << "Enter number of employees you want to add: ";
134+
cin >> n;
135+
for (int i = 0; i < n; i++){
136+
cout << "Enter the employee id: ";
137+
int id = takeID();
138+
cout << "Enter the employee name: ";
139+
string n = takeName();
140+
cout << "Enter the employee department: ";
141+
string dep = takeDepartment();
142+
cout << "Enter the employee salary: ";
143+
int sal = takeSalary();
144+
145+
emps.push_back(Employee(id, n, dep, sal));
146+
}
147+
cout << "\nAll employees added!\n";
148+
}
149+
150+
// 4 - Update an employee detail
151+
void updateEmp(vector<Employee> &emps){
152+
cout << "Enter employee id to be updated: ";
153+
int id = takeID();
154+
155+
Employee* empPtr = empExist(emps, id);
156+
if (empPtr){
157+
cout << "Enter the updated name: ";
158+
string n = takeName();
159+
160+
cout << "Enter the updated department: ";
161+
string dep = takeDepartment();
162+
163+
cout << "Enter the updated salary: ";
164+
int sal = takeSalary();
165+
166+
empPtr->updateEmpData(n, dep, sal);
167+
} else {
168+
empNotFoundMsg(id);
169+
}
170+
171+
}
172+
173+
// 5 - Removing an employee by searching id
174+
void deleteEmp(vector<Employee> &emps){
175+
cout << "Enter id of the employee to be deleted: ";
176+
int id = takeID();
177+
if (empExist(emps,id)){
178+
emps.erase(
179+
remove_if(emps.begin(), emps.end(), [id](const Employee &e){ return e.getID() == id; }),
180+
emps.end()
181+
);
182+
cout << "Data of employee with id " << id << " is deleted";
183+
} else {
184+
empNotFoundMsg(id);
185+
}
186+
}
187+
188+
// 6 - Custom sort method to sort according to id (joining date) or salary
189+
// Oldest -> newest
190+
// or, highest salary -> least salary
191+
void sortEmps(vector<Employee>& emps){
192+
int by;
193+
while (true){
194+
cout << "How do you want to sort the employees? (1 - by salary, 2 - by joining date or id)";
195+
cin >> by;
196+
197+
if (by==1){ // sort by salary
198+
sort(emps.begin(), emps.end(), [](const Employee& e1, const Employee& e2){
199+
return e1.getSalary() > e2.getSalary();
200+
});
201+
202+
cout << "Employees sorted by descending order of salary\n";
203+
break;
204+
} else if (by==2) { // sort by joining date (emp id)
205+
sort(emps.begin(), emps.end(), [](const Employee& e1, const Employee& e2){
206+
return e1.getID() < e2.getID();
207+
});
208+
cout << "Employees sorted by accending order of joining date\n";
209+
break;
210+
} else {
211+
cout << "\tInvalid sorting type\n";
212+
}
213+
}
214+
}
215+
216+
// 7 - Save changes
217+
void saveChanges(vector<Employee> &emps){
218+
ofstream out("../data/employees.csv");
219+
if (out){
220+
for (const auto &e : emps){
221+
int id = e.getID();
222+
string n = e.getName();
223+
string dep = e.getDepartment();
224+
int sal = e.getSalary();
225+
226+
out << id << ',' << n << ',' << dep << ',' << sal << endl ;
227+
}
228+
cout << "All the changes are saved!\n";
229+
} else {
230+
cout << "Unable to save changes - cant open the file ../data/employees.csv";
231+
}
232+
}
233+
234+
235+
// Saving file data
236+
void saveData(vector<Employee> &emps){
237+
ofstream out("../data/employees.csv");
238+
if (out){
239+
for (const auto &e : emps){
240+
out << e.getID() << ',' << e.getName() << ',' << e.getDepartment() << ',' << e.getSalary() << '\n';
241+
}
242+
out.close();
243+
cout << "Data saved successfully!!\n";
244+
} else{
245+
cout << "!!! Cant open the ../data/employees.csv !!!";
246+
}
247+
}
248+
249+
// Loading file data
250+
void loadData(vector<Employee> &emps){
251+
ifstream in("../data/employees.csv");
252+
if (in){
253+
string line;
254+
while (getline(in, line)){
255+
stringstream word(line);
256+
257+
int id, sal;
258+
string n, dep;
259+
char comma; // to pass the delimiter comma
260+
261+
word >> id >> comma;
262+
getline(word, n, ',');
263+
getline(word, dep, ',');
264+
word >> sal;
265+
266+
emps.push_back(Employee(id, n, dep, sal));
267+
}
268+
in.close();
269+
} else {
270+
cout << "../data/employees.csv file doesn't exist.";
271+
}
272+
}
273+
274+
// Main interaction ui
275+
void ui(vector<Employee> &emps){
276+
while (true){
277+
cout << "\n\t\tWELCOME TO EMPLOYEE MANAGEMENT SYSTEM\n";
278+
cout << "Please enter what operation you want to perform\n";
279+
cout << "1 - Display all employees\n";
280+
cout << "2 - Search employee\n";
281+
cout << "3 - Add employees\n";
282+
cout << "4 - Update an employee\n";
283+
cout << "5 - Delete an employee\n";
284+
cout << "6 - Sort Employees\n";
285+
cout << "7 - Save all changes\n";
286+
cout << "8 - Exit the program\n";
287+
288+
int choice;
289+
cin >> choice;
290+
291+
switch (choice){
292+
case 1:
293+
displayAll(emps);
294+
break;
295+
case 2:
296+
searchEmp(emps);
297+
break;
298+
case 3:
299+
addEmps(emps);
300+
break;
301+
case 4:
302+
updateEmp(emps);
303+
break;
304+
case 5:
305+
deleteEmp(emps);
306+
break;
307+
case 6:
308+
sortEmps(emps);
309+
break;
310+
case 7:
311+
saveChanges(emps);
312+
break;
313+
case 8:
314+
cout << "\t\tExiting the program!!";
315+
return;
316+
default:
317+
cout << "\t!!! Invalid input !!!\n";
318+
break;
319+
}
320+
}
321+
}
322+
323+
324+
int main() {
325+
vector<Employee> employees;
326+
loadData(employees);
327+
328+
ui(employees);
329+
return 0;
330+
}

0 commit comments

Comments
 (0)