-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementation.cpp
More file actions
263 lines (234 loc) · 6.73 KB
/
Implementation.cpp
File metadata and controls
263 lines (234 loc) · 6.73 KB
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
263
//
// Created by miji-ustad on 12/16/24.
//
#include <iostream>
#include <limits>
#include <iomanip>
#include "List.h"
using namespace std;
// ================== Node ======================
Node::Node() {
id = 0;
name = " ";
salary = 0.0;
next = nullptr;
}
Node::Node(int id, string name, double salary) {
this->id = id;
this->name = name;
this->salary = salary;
this->next = NULL;
}
int Node::getId() {
return id;
}
string Node::getName() {
return name;
}
double Node::getSalary() {
return salary;
}
Node* Node::getNext() {
return next;
}
void Node::setId(int id) {
this->id = id;
}
void Node::setName(string name) {
this->name = name;
}
void Node::setSalary(double salary) {
this->salary = salary;
}
void Node::setNext(Node* next) {
this->next = next;
}
// =========================== List ============================
List::List() {
head = NULL;
tail = NULL;
}
bool List::isEmpty() {
if (head == NULL) {
return true;
}
else {
return false;
}
}
void List::insert(int id, string name, double salary) {
Node* temp = new Node(id, name, salary);
if (isEmpty()) {
head = temp;
tail = temp;
} else {
tail->setNext(temp);
tail = temp;
}
}
Node* List::update(int value, string tempName, double tempSalary) {
Node* temp = head;
while (temp != nullptr) {
if (temp->getId() == value) {
temp->setName(tempName);
temp->setSalary(tempSalary);
cout << "Data Updated Successfully...." << endl;
return temp;
}
temp = temp->getNext();
}
cout << "Data Update Failed...." << endl;
return nullptr;
}
Node* List::search(int value) {
Node* temp = head;
while (temp != nullptr) {
if (temp->getId() == value) {
cout << "ID : " << temp->getId() << endl;
cout << "Name : " << temp->getName() << endl;
cout << "Salary : " << temp->getSalary() << endl;
return temp; // Exit after finding the node
}
temp = temp->getNext();
}
cout << "No Data Found...." << endl;
return nullptr;
}
void List::print() {
Node* temp = head;
int count = 1;
// Set fixed column widths for alignment
const int indexWidth = 10;
const int idWidth = 10;
const int nameWidth = 20;
const int salaryWidth = 15;
// Print header
cout << "\n\n"
<< std::left << std::setw(indexWidth) << "Index"
<< std::setw(idWidth) << "ID"
<< std::setw(nameWidth) << "Name"
<< std::setw(salaryWidth) << "Salary"
<< "\n"
<< std::string(indexWidth + idWidth + nameWidth + salaryWidth, '-') << endl;
// Print rows
while (temp != nullptr) {
cout << std::left
<< std::setw(indexWidth) << count
<< std::setw(idWidth) << temp->getId()
<< std::setw(nameWidth) << temp->getName()
<< std::setw(salaryWidth) << temp->getSalary() << endl;
temp = temp->getNext();
count++;
}
}
void List::remove(int target) {
Node* temp = head;
Node* previous = nullptr;
while (temp != nullptr && temp->getId() != target) {
previous = temp;
temp = temp->getNext();
}
if (temp == nullptr) {
cout << "ID not found. Removal failed." << endl;
return;
}
if (temp == head) {
head = head->getNext();
} else {
previous->setNext(temp->getNext());
}
if (temp == tail) {
tail = previous;
}
delete temp;
cout << "Employee with ID " << target << " removed successfully." << endl;
}
void List::mainMenu() {
system("clear");
int option = 0;
while (true) {
title();
menuOptions();
cout << "\t\t\t" << "Please Enter Your Choice : ";
if (!(cin >> option)) {
cin.clear(); // Clear the error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore invalid input
cout << "Invalid input. Please enter a number." << endl;
continue;
}
switch (option) {
case 1: {
int tempID;
string tempName;
double tempSalary;
cout << "Enter Employee ID : ";
cin >> tempID;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter Employee Name : ";
getline(cin, tempName);
cout << "Enter Employee Salary : ";
cin >> tempSalary;
insert(tempID, tempName, tempSalary);
cout << "\t\t\t" << "Data Inserted Successfully...." << endl;
break;
}
case 2: {
int targetID;
cout << "Enter Employee ID to Remove: ";
cin >> targetID;
remove(targetID);
break;
}
case 3: {
int searchID;
cout << "Enter Employee ID to Search: ";
cin >> searchID;
search(searchID);
break;
}
case 4: {
int updateID;
string newName;
double newSalary;
cout << "Enter Employee ID to Update: ";
cin >> updateID;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter New Name: ";
getline(cin, newName);
cout << "Enter New Salary: ";
cin >> newSalary;
update(updateID, newName, newSalary);
break;
}
case 5: {
print();
break;
}
case 6: {
cout << "\t\t\t" << "==================================" << endl;
cout << "\t\t\t" << "|| Goodbye! Have a Nice Day ||" << endl;
cout << "\t\t\t" << "==================================" << endl;
exit(0);
}
default: {
cout << "Invalid Choice. Please Try Again...." << endl;
break;
}
}
}
}
void List::title() {
cout << "\n\n";
cout << "\t\t\t" << "==============================================" << endl;
cout << "\t\t\t" << "================== MAIN MENU =================" << endl;
cout << "\t\t\t" << "==============================================" << endl;
cout << "\n\n";
}
void List::menuOptions() {
cout << "\t\t\t" << "1 | Add New Employee" << endl;
cout << "\t\t\t" << "2 | Delete Employee" << endl;
cout << "\t\t\t" << "3 | Search Employee" << endl;
cout << "\t\t\t" << "4 | Update Employee" << endl;
cout << "\t\t\t" << "5 | Show All Employees" << endl;
cout << "\t\t\t" << "6 | Exit" << endl;
}