-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
312 lines (222 loc) · 10.4 KB
/
main.c
File metadata and controls
312 lines (222 loc) · 10.4 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
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
/*
Candidate Names & Numbers: Kori Pitter 1000391954
Jovan Clue 1000390648
Subject: Computer Science U1
Program Name: George E. Clue Attorney at Law Filing System
Description: This program will allow users (office staff) to enter client case information and in addition
client billing info, which allows for ease in locating files and retrieving case info at short notice.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME_LENGTH 100
#define LINE_LENGTH 200
#define DOWN_PAYMENT_PERCENTAGE 0.1
void password();
void editClientFile(const char*, const char*);
void addClientFile(const char*, const char*);
void printClientFile(const char*, const char*);
int main()
{
char clientFirstName[50];
char clientLastName[50];
char option;
char NewFile;
password();
system("color F0");
printf("\t**WELCOME TO THE GEORGE E. CLUE ATTORNEY AT LAW FILING SYSTEM**\n\n");
printf("Select an option from the choices below:\n");
printf("1. Add a new client file\n2. Read an existing client file\n3. Edit an existing client file\n\n");
scanf(" %c", &option);
switch (option) {
case '1':
printf("Enter the client’s first name: "); // Accepting client information for case 1
scanf("%s", clientFirstName);
printf("Enter the client’s last name: ");
scanf("%s", clientLastName);
addClientFile(clientFirstName, clientLastName); // Calling the function to add client file
break;
case '2':
printf("Enter the client’s first name: "); // Accepting client information for case 2
scanf("%s", clientFirstName);
printf("Enter the client’s last name: ");
scanf("%s", clientLastName);
printClientFile(clientFirstName, clientLastName); // Calling the function to read client file
break;
case '3':
printf("Enter the client’s first name: "); // Accepting client information for case 1
scanf("%s", clientFirstName);
printf("Enter the client’s last name: ");
scanf("%s", clientLastName);
editClientFile(clientFirstName, clientLastName); // Calling the function to add client file
break;
default:
printf("Invalid Option Selected!\n");
printf(" Please Try Again!");
break;
}
return 0;
}
// Password Function for Program
void password () {
char username[10]; char pword[10];
char username_correct[10] = "staff_1"; char pword_correct[10] = "GC@law";
// Prompts user to enter password and username until correct credentials are entered
printf("\t********WELCOME TO THE GEORGE E. CLUE ATTORNEY AT LAW FILING SYSTEM LOGIN PAGE********\n\n");
printf("Please enter your correct username: ");
scanf("%s", username);
printf("Please enter your correct password: ");
scanf("%s", pword);
if ((strcmp(username, username_correct) != 0) && (strcmp(pword, pword_correct) != 0)) {
// Loop to run until correct information is entered
do {
printf("Login info incorrect, TRY AGAIN! \n");
printf("Please enter your correct username: ");
scanf("%s", username);
printf("Please enter your correct password: ");
scanf("%s", pword);
}
while ((strcmp(username, username_correct) != 0) && (strcmp(pword, pword_correct) != 0));
}
else {
printf("Login info correct, ACCESS GRANTED!\n\n");
}
}
// Function to Add a File for a new Client
void addClientFile(const char *clientfirstname, const char *clientlastname) {
char filename[FILE_NAME_LENGTH];
sprintf(filename, "%s_%s.txt", clientfirstname, clientlastname);
FILE *file = fopen(filename, "w");
if (file == NULL) {
perror("\nTHERE WAS AN ERROR OPENING THIS FILE, PLEASE TRY AGAIN\n"); // Prints an error message to the user
exit(EXIT_FAILURE);
}
// Accepting Client Information
printf("Enter the client’s gender: "); // Inputting Client's Gender
char clientGender[10];
scanf(" %s", clientGender);
getchar();
fprintf(file, "Gender:\n%s\n", clientGender);
printf("Enter the client’s address: "); // Inputting Client's Address
char clientAddress[100];
fgets(clientAddress, sizeof(clientAddress), stdin);
fprintf(file, "Address:\n%s", clientAddress);
printf("Enter the client’s phone number: "); // Inputting Client's Phone Number
char clientPhoneNum[20];
scanf(" %s", clientPhoneNum);
getchar();
fprintf(file, "Phone Number:\n%s\n", clientPhoneNum);
printf("Enter the client’s case number: "); // Inputting Client's Case Number
int caseNum;
scanf(" %d", &caseNum);
getchar();
fprintf(file, "Case Number:\n%d\n", caseNum);
printf("Enter the date the case was opened: "); // Inputting the Date Client's Case was Opened
char DateOpened[20];
fgets(DateOpened, sizeof(DateOpened), stdin);
fprintf(file, "Date Case Was Opened:\n%s", DateOpened);
printf("Enter the client’s email address: "); // Inputting Client's Email Address
char clientEmail[50];
scanf(" %s", clientEmail);
getchar();
fprintf(file, "Email Address:\n%s\n", clientEmail);
printf("Enter the client’s card number: "); // Inputting Client's Card Number
char cardNum[20];
scanf(" %s", cardNum);
getchar();
fprintf(file, "Card Number:\n%s\n", cardNum);
printf("Enter the client’s last court date: "); // Inputting Client's Last Court Date
char LastCourtDate[20];
fgets(LastCourtDate, sizeof(LastCourtDate), stdin);
fprintf(file, "Last Court Date:\n%s", LastCourtDate);
printf("Enter the client’s next court date: "); // Inputting Client's Next Court Date
char next_court_date[20];
fgets(next_court_date, sizeof(next_court_date), stdin);
fprintf(file, "Next Court Date:\n%s", next_court_date);
printf("Enter the client’s last arrest date (If any): "); // Inputting Client's Last Arrest Date
char last_arrest[20];
fgets(last_arrest, sizeof(last_arrest), stdin);
fprintf(file, "Last Arrest Date:\n%s", last_arrest);
printf("Enter the settlement amount for the case: $"); // Inputting Client's Settlement Amount
float CaseSettlementAmount;
scanf("%f", &CaseSettlementAmount);
getchar();
fprintf(file, "Settlement Amount:\n$%.2f\n", CaseSettlementAmount);
// Calculating & Displaying Down Payment Amount
float DownPayment = DOWN_PAYMENT_PERCENTAGE * CaseSettlementAmount;
fprintf(file, "Down Payment:\n$%.2f\n", DownPayment);
fclose(file);
printf("File created successfully: %s\n", filename); // Confirmation that file has been made which is displayed to the user
}
// Function to display information on an existing client
void printClientFile(const char *clientfirstname, const char *clientlastname) {
char filename[FILE_NAME_LENGTH];
sprintf(filename, "%s_%s.txt", clientfirstname, clientlastname);
FILE *file = fopen(filename, "r"); // Opening file in read mode
if (file == NULL) {
perror("\nTHERE WAS AN ERROR OPENING THIS FILE, PLEASE CHECK NAME AND TRY AGAIN\n"); // Error to be printed if the file entered doesn't exist
exit(EXIT_FAILURE);
}
// Reading information from the file into a variable and outputting the information
char line[200];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file); // Closing file
}
// Function to adjust an existing client's information
void editClientFile(const char *clientfirstname, const char *clientlastname) {
char filename[FILE_NAME_LENGTH];
sprintf(filename, "%s_%s.txt", clientfirstname, clientlastname);
FILE *file = fopen(filename, "r"); // Opening file in read mode
if (file == NULL) {
perror("THERE WAS AN ERROR OPENING THIS FILE, PLEASE CHECK NAME AND TRY AGAIN\n"); // Error shown if file does not exist
exit(EXIT_FAILURE);
}
// Read the existing file line by line and display the content
char line[LINE_LENGTH];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
// Allow user to edit specific fields
printf("\nWhich field would you like to edit? Enter the corresponding number:\n");
printf("1. Gender\n2. Address\n3. Phone Number\n4. Case Number\n5. Date Case was opened\n6. Email Address\n7. Card Number\n8. Last Court Date\n9. Next Court Date\n10. Last Arrest Date\n11. Settlement Amount\n12. Down Payment\n\t NB - Please update Down Payment Amount if Settlement Amount is updated.\n");
int choice;
scanf("%d", &choice);
getchar();
FILE *tempFile = fopen("temp.txt", "w");
file = fopen(filename, "r");
if (file == NULL || tempFile == NULL) {
perror("THERE WAS AN ERROR OPENING THE FILE(S)\n");
exit(EXIT_FAILURE);
}
int lineNum = 1;
while (fgets(line, sizeof(line), file)) {
// Checks if the line contains the chosen field
if (lineNum == ((choice - 1) * 2 + 2)) {
char newValue[LINE_LENGTH];
// Prompts for edited value
printf("Enter the new value: ");
fgets(newValue, sizeof(newValue), stdin);
// Updates the line with the new value
fprintf(tempFile, "%s", newValue);
} else {
// Copy the line as is to the temporary file
fprintf(tempFile, "%s", line);
}
lineNum++;
}
fclose(file);
fclose(tempFile);
// Rename the temporary file to the original file and remove the original
if (remove(filename) != 0) {
perror("ERROR DELETING THE ORIGINAL FILE\n");
exit(EXIT_FAILURE);
}
if (rename("temp.txt", filename) != 0) {
perror("ERROR RENAMING THE TEMPORARY FILE\n");
exit(EXIT_FAILURE);
}
printf("File updated successfully: %s\n", filename);
}