-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
359 lines (301 loc) · 9.77 KB
/
main.c
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//Employee Record Managemnt System
//main.c
//written by leonotieno27
//Gmail: [email protected]
/*A console-based application to manage employee records for a small company.
It allows users to:
1.Add new employee records.
2.View all employee records.
3.Update employee details.
4.Delete employee records.
5.Save and load data to/from a binary file for persistence.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "structures.h"
// Function prototypes
void welcomeScreen();
void addNewEmployee();
void viewEmployees();
void updateDetails();
void deleteDetails();
void backtoMenu();
void checkPassword();
void sortbyId();
// File pointers
FILE *fptr;
FILE *tempFile;
FILE *ps;
// Main function
int main() {
checkPassword();
return 0;
}
// Welcome screen
void welcomeScreen() {
int choice;
puts("\t\t*********************************");
puts("\t\tEMPLOYEE RECORD MANAGEMENT SYSTEM");
puts("\t\t*********************************\n");
puts("Welcome,\n");
puts("1. Add New Employee.");
puts("2. View Employee Records.");
puts("3. Update Employee Details.");
puts("4. Delete Employee Records.");
puts("5. Exit.");
printf("\nChoose an action (use the numbers): ");
scanf(" %d", &choice);
switch (choice) {
case 1: addNewEmployee(); break;
case 2: viewEmployees(); break;
case 3: updateDetails(); break;
case 4: deleteDetails(); break;
case 5: exit(0);
default: puts("Invalid choice. Try again."); welcomeScreen();
}
}
//back to menu function
void backtoMenu(){
int choice;
puts("\n 1.Back to Main Menu.");
puts("2.Exit");
scanf(" %d", &choice);
if(choice == 1){
system("clear");
welcomeScreen();
}else if(choice == 2){
exit(1);
}else{
exit(1);
}
}
// Add new employee
void addNewEmployee() {
struct employeeinfo employee;
int choice;
fptr = fopen("database.txt", "ab"); // Binary mode for appending
if (fptr == NULL) {
perror("Error opening file");
return;
}
do {
printf("\nEnter Employee ID: ");
scanf("%d", &employee.id);
getchar(); // Clear newline
printf("Enter Employee Name: ");
fgets(employee.name, 50, stdin);
employee.name[strcspn(employee.name, "\n")] = '\0'; // Remove newline
printf("Enter Job Position: ");
fgets(employee.job, 50, stdin);
employee.job[strcspn(employee.job, "\n")] = '\0';
printf("Enter Salary: ");
scanf("%f", &employee.salary);
fwrite(&employee, sizeof(struct employeeinfo), 1, fptr); // Write to file
printf("Employee record added.\n");
printf("Add another employee? (Y/N): ");
scanf(" %d", &choice);
} while (toupper(choice) == 'Y');
fclose(fptr);
void backtoMenu();
}
// View employees
void viewEmployees() {
int sort;
struct employeeinfo employee;
fptr = fopen("database.txt", "rb");
if (fptr == NULL) {
perror("Error opening file");
return;
}
system("clear");
puts("\nEmployee Records: ");
while (fread(&employee, sizeof(struct employeeinfo), 1, fptr)) { //store each record 1 at a time at employee then use it.
printf("ID: %d, Name: %s, Job: %s, Salary: %.2f\n", employee.id, employee.name, employee.job, employee.salary);
}
puts("Press 1 to sort the list.");
scanf("%d", &sort);
if(sort == 1){
sortbyId();
}
else{
viewEmployees();
}
fclose(fptr);
void backtoMenu();
}
// Update employee details
void updateDetails() {
struct employeeinfo employee;
int updateId, found = 0;
fptr = fopen("database.txt", "rb");
tempFile = fopen("tempFile.txt", "wb");
if (fptr == NULL || tempFile == NULL) {
perror("Error opening file");
return;
}
printf("\nEnter ID of the employee to update: ");
scanf("%d", &updateId);
while (fread(&employee, sizeof(struct employeeinfo), 1, fptr)) { //if id match, replace structure with new
if (employee.id == updateId) {
found = 1;
printf("Enter New Employee Name: ");
getchar();
fgets(employee.name, 50, stdin);
employee.name[strcspn(employee.name, "\n")] = '\0';
printf("Enter New Job Position: ");
fgets(employee.job, 50, stdin);
employee.job[strcspn(employee.job, "\n")] = '\0';
printf("Enter New Salary: ");
scanf("%f", &employee.salary);
}
fwrite(&employee, sizeof(struct employeeinfo), 1, tempFile);
}
fclose(fptr);
fclose(tempFile);
remove("database.txt");
rename("tempFile.txt", "database.txt");
if (found) {
puts("Employee details updated.");
} else {
puts("Employee not found.");
}
void backtoMenu();
}
// Delete employee
void deleteDetails() {
struct employeeinfo employee;
int deleteId, found = 0;
fptr = fopen("database.txt", "rb");
tempFile = fopen("tempFile.txt", "wb");
if (fptr == NULL || tempFile == NULL) {
perror("Error opening file");
return;
}
printf("\nEnter ID of the employee to delete: ");
scanf("%d", &deleteId);
while (fread(&employee, sizeof(struct employeeinfo), 1, fptr)) { //can't manipulate directly in c replace with new file
if (employee.id == deleteId) { //skipping the deleted record and rename file
found = 1;
printf("Deleting employee ID: %d\n", deleteId);
continue; // Skip this record
}
fwrite(&employee, sizeof(struct employeeinfo), 1, tempFile);
}
fclose(fptr);
fclose(tempFile);
remove("database.txt");
rename("tempFile.txt", "database.txt");
if (found) {
puts("Employee record deleted.");
} else {
puts("Employee not found.");
}
void backtoMenu();
}
//checkpassword
void checkPassword() {
char pPassword[20];
char pConfirmPassword[20];
int flag = 0;
puts("\t\t*********************************");
puts("\t\tEMPLOYEE RECORD MANAGEMENT SYSTEM");
puts("\t\t*********************************\n");
puts("\nTo Login:");
ps = fopen("ps.txt", "rb");
if (ps == NULL) {
ps = fopen("ps.txt", "ab");
if (ps == NULL) {
puts("Error opening file for writing password.");
return;
}
puts("Enter new password: ");
fgets(pPassword, sizeof(pPassword), stdin);
pPassword[strcspn(pPassword, "\n")] = '\0';
puts("Confirm Password: ");
fgets(pConfirmPassword, sizeof(pConfirmPassword), stdin); //get password from input
pConfirmPassword[strcspn(pConfirmPassword, "\n")] = '\0'; //delete newline char or enter
int passLength = strlen(pPassword);
int confirmLength = strlen(pConfirmPassword);
if (passLength != confirmLength) { //checking length first of two passwords
puts("Passwords don't match in length.");
checkPassword();
return;
}
for (int i = 0; i < passLength; i++) { //checking character by character if they are similar
if (pPassword[i] != pConfirmPassword[i]) {
flag = 1;
break;
}
}
if (flag == 0) {
fprintf(ps, "%s", pPassword); // Save the password to the file
puts("Password set successfully.");
fclose(ps);
system("clear");
welcomeScreen();
} else {
puts("Passwords do not match!");
fclose(ps);
checkPassword();
}
} else {
char storedPassword[20];
puts("Enter password: ");
fgets(pPassword, sizeof(pPassword), stdin); //stdin = take input and store in npassword
pPassword[strcspn(pPassword, "\n")] = '\0';
fgets(storedPassword, sizeof(storedPassword), ps);
storedPassword[strcspn(storedPassword, "\n")] = '\0';
if (strcmp(pPassword, storedPassword) == 0) { //lexicographically compares letter by letter
system("clear");
welcomeScreen();
} else {
puts("Incorrect password.");
}
fclose(ps);
}
}
//sort list by bubblesort algorithm
void sortbyId(){
int choice, outer, inner, didSwap, employeeCount = 0;
struct employeeinfo employees[100];
fptr = fopen("database.txt", "rb");
if(fptr == NULL){
perror("Error opening file");
return;
}
//read array of employees
while(fread(&employees[employeeCount], sizeof(struct employeeinfo), 1, fptr)){
employeeCount++; //count number of employees
}
fclose(fptr);
for(outer = 0; outer < employeeCount - 1; outer++){
didSwap = 0;
for(inner = outer + 1; inner < employeeCount; inner++){
if(employees[inner].id < employees[outer].id){
struct employeeinfo temp = employees[inner];
employees[inner] = employees[outer];
employees[outer] = temp;
didSwap = 1;
}
}
if(didSwap == 0){
break;
}
}
system("clear");
puts("\nSorted Employee Records");
for(int i; i < employeeCount; i++){
printf("ID: %d, Name: %s, Job: %s, Salary: %.2f\n", employees[i].id, employees[i].name,
employees[i].job, employees[i].salary);
}
puts("Press (1) to go back and press(2) to go to Main Menu");
scanf("%d", &choice);
if(choice == 1){
system("clear");
viewEmployees();
}else if(choice == 2){
system("clear");
welcomeScreen();
}
}