CrimsonCare
CrimsonCare is a C project designed to provide a robust solution for blood management.
 
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
35#include "include/misc.h"
36
37int main(void) {
38 loadBloodGroups();
39 loadHospitals();
40 loadAdminCredentials();
41
42 displayWelcomeMessage();
43
44 uint32_t choice;
45 bool isAdmin = false;
46 char currentAdminUsername[MAX_USERNAME_LENGTH];
47
48 while (1) {
49 displayUserMenu();
50
51 if (scanf("%u", &choice) != 1) {
52 printf("Error: Invalid input.\n");
53 clearInputBuffer();
54 continue;
55 }
56
57 clearInputBuffer();
58
59 switch (choice) {
60 case 1: {
61 TransactionType transactionType = BUY;
62 if (!isBloodAvailable(NULL, transactionType)) {
63 printf("No blood available for purchase.\n");
64 break;
65 }
66 char hospitalCode[MAX_TRANSACTION_NAME_LENGTH];
67 uint32_t bloodGroupId;
68 uint32_t quantity;
69 printf("Enter Entity Name (Hospital Code): ");
70 fgets(hospitalCode, sizeof(hospitalCode), stdin);
71 hospitalCode[strcspn(hospitalCode, "\n")] = 0;
72 if (containsPipe(hospitalCode)) {
73 printf("Error: Hospital code cannot contain a pipe character.\n");
74 break;
75 }
76 if (!validateHospitalCode(hospitalCode)) {
77 printf("Error: Invalid hospital code.\n");
78 break;
79 }
80
81 displayBloodStocks();
82 printf("Enter Blood Group ID: ");
83 scanf("%u", &bloodGroupId);
84 clearInputBuffer();
85 if (!isValidBloodGroup(bloodGroupId)) {
86 printf("Error: Invalid blood group.\n");
87 break;
88 }
89 if (!isBloodAvailable(&bloodGroupId, transactionType)) {
90 printf("Blood %s is not available for purchase.\n", getBloodGroupById(bloodGroupId));
91 break;
92 }
93 printf("Enter Quantity: ");
94 scanf("%u", &quantity);
95 clearInputBuffer();
96 if (addTransaction(transactionType, hospitalCode, bloodGroupId, quantity)) {
97 printf("Transaction successful for %s (%s).\n", getHospitalNameByCode(hospitalCode), hospitalCode);
98 } else {
99 printf("Error: Transaction failed.\n");
100 }
101 break;
102 }
103 case 2: {
104 TransactionType transactionType = SELL;
105 if (!isBloodAvailable(NULL, transactionType)) {
106 printf("No blood available for sale.\n");
107 break;
108 }
109 char donorName[MAX_TRANSACTION_NAME_LENGTH];
110 uint32_t bloodGroupId;
111 uint32_t quantity;
112 printf("Enter Donor Name: ");
113 fgets(donorName, sizeof(donorName), stdin);
114 donorName[strcspn(donorName, "\n")] = 0;
115 if (containsPipe(donorName)) {
116 printf("Error: Donor name cannot contain a pipe character.\n");
117 break;
118 }
119 displayBloodStocks();
120 printf("Enter Blood Group ID: ");
121 scanf("%u", &bloodGroupId);
122 clearInputBuffer();
123 if (!isValidBloodGroup(bloodGroupId)) {
124 printf("Error: Invalid blood group.\n");
125 break;
126 }
127 if (!isBloodAvailable(&bloodGroupId, transactionType)) {
128 printf("Blood %s is not available for sale.\n", getBloodGroupById(bloodGroupId));
129 break;
130 }
131
132 printf("Enter Quantity: ");
133 scanf("%u", &quantity);
134 clearInputBuffer();
135 if (addTransaction(transactionType, donorName, bloodGroupId, quantity)) {
136 printf("Transaction successful for %s.\n", donorName);
137 } else {
138 printf("Error: Transaction failed.\n");
139 }
140 break;
141 }
142 case 3:
143 displayBloodStocks();
144 break;
145 case 4: {
146 char adminPassword[MAX_PASSWORD_LENGTH];
147 printf("Enter Admin Username: ");
148 fgets(currentAdminUsername, sizeof(currentAdminUsername), stdin);
149 currentAdminUsername[strcspn(currentAdminUsername, "\n")] = 0;
150 if (!checkUsername(currentAdminUsername)) {
151 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
152 break;
153 }
154 printf("Enter Admin Password: ");
155 getPassword(adminPassword, sizeof(adminPassword));
156
157 if (validateAdmin(currentAdminUsername, adminPassword)) {
158 isAdmin = true;
159 printf("Admin %s logged in successfully.\n", currentAdminUsername);
160
161 while (isAdmin) {
162 displayAdminMenu();
163
164 if (scanf("%u", &choice) != 1) {
165 printf("Error: Invalid input.\n");
166 clearInputBuffer();
167 continue;
168 }
169
170 clearInputBuffer();
171
172 switch (choice) {
173 case 1: {
174 char hospitalName[MAX_HOSPITAL_NAME_LENGTH];
175 char hospitalLocation[MAX_HOSPITAL_LOCATION_LENGTH];
176 printf("Enter Hospital Name: ");
177 fgets(hospitalName, sizeof(hospitalName), stdin);
178 hospitalName[strcspn(hospitalName, "\n")] = 0;
179 if (containsPipe(hospitalName)) {
180 printf("Error: Hospital name cannot contain a pipe character.\n");
181 break;
182 }
183 printf("Enter Hospital Location: ");
184 fgets(hospitalLocation, sizeof(hospitalLocation), stdin);
185 hospitalLocation[strcspn(hospitalLocation, "\n")] = 0;
186 if (containsPipe(hospitalLocation)) {
187 printf("Error: Hospital location cannot contain a pipe character.\n");
188 break;
189 }
190 char* hospitalCode = addHospital(hospitalName, hospitalLocation);
191 if (hospitalCode) {
192 printf("Hospital %s added successfully by %s.\n", hospitalName, currentAdminUsername);
193 printf("Hospital Code: %s\n", hospitalCode);
194 } else {
195 printf("Error: Could not add hospital.\n");
196 }
197 break;
198 }
199 case 2: {
200 displayBloodGroups();
201 uint32_t bloodGroupId;
202 uint32_t newQuantity;
203 printf("Enter Blood Group ID: ");
204 scanf("%u", &bloodGroupId);
205 clearInputBuffer();
206 if (!isValidBloodGroup(bloodGroupId)) {
207 printf("Error: Invalid blood group.\n");
208 break;
209 }
210 if (!isBloodAvailable(&bloodGroupId, SELL)) {
211 float newPrice;
212 printf("Price for %s is not set.\n", getBloodGroupById(bloodGroupId));
213 printf("Enter New Price: ");
214 scanf("%f", &newPrice);
215 clearInputBuffer();
216 if (updateBloodPrice(bloodGroupId, newPrice)) {
217 printf("Blood price for %s updated successfully by %s.\n", getBloodGroupById(bloodGroupId), currentAdminUsername);
218 } else {
219 printf("Error: Could not update blood price.\n");
220 }
221 }
222 printf("Enter New Quantity: ");
223 scanf("%u", &newQuantity);
224 clearInputBuffer();
225 if (updateBloodQuantity(bloodGroupId, newQuantity)) {
226 printf("Blood quantity for %s updated successfully by %s.\n", getBloodGroupById(bloodGroupId), currentAdminUsername);
227 } else {
228 printf("Error: Could not update blood quantity.\n");
229 }
230 break;
231 }
232 case 3: {
233 displayBloodGroups();
234 uint32_t bloodGroupId;
235 float newPrice;
236 printf("Enter Blood Group ID: ");
237 scanf("%u", &bloodGroupId);
238 clearInputBuffer();
239 if (!isValidBloodGroup(bloodGroupId)) {
240 printf("Error: Invalid blood group.\n");
241 break;
242 }
243 printf("Enter New Price: ");
244 scanf("%f", &newPrice);
245 clearInputBuffer();
246 if (updateBloodPrice(bloodGroupId, newPrice)) {
247 printf("Blood price for %s updated successfully by %s.\n", getBloodGroupById(bloodGroupId), currentAdminUsername);
248 } else {
249 printf("Error: Could not update blood price.\n");
250 }
251 break;
252 }
253 case 4: {
254 char adminOldPassword[MAX_PASSWORD_LENGTH];
255 char adminNewPassword[MAX_PASSWORD_LENGTH];
256 printf("Enter Old Password: ");
257 getPassword(adminOldPassword, sizeof(adminOldPassword));
258 printf("Enter New Password: ");
259 getPassword(adminNewPassword, sizeof(adminNewPassword));
260 if (changeAdminPassword(currentAdminUsername, adminOldPassword, adminNewPassword)) {
261 printf("Password for %s updated successfully.\n", currentAdminUsername);
262 } else {
263 printf("Error: Could not change admin password.\n");
264 }
265 break;
266 }
267 case 5: {
268 char currentAdminPassword[MAX_PASSWORD_LENGTH];
269 char newAdminUsername[MAX_USERNAME_LENGTH];
270 char newAdminPassword[MAX_PASSWORD_LENGTH];
271 char confirmNewAdminPassword[MAX_PASSWORD_LENGTH];
272 printf("To continue, please enter your (%s) current password: ", currentAdminUsername);
273 getPassword(currentAdminPassword, sizeof(currentAdminPassword));
274 if (validateAdmin(currentAdminUsername, currentAdminPassword)) {
275 printf("Password verified successfully.\n");
276 } else {
277 printf("Error: Invalid password.\n");
278 break;
279 }
280 printf("Enter New Admin Username: ");
281 fgets(newAdminUsername, sizeof(newAdminUsername), stdin);
282 newAdminUsername[strcspn(newAdminUsername, "\n")] = 0;
283 if (!checkUsername(newAdminUsername)) {
284 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
285 break;
286 }
287 printf("Enter New Admin Password: ");
288 getPassword(newAdminPassword, sizeof(newAdminPassword));
289 printf("Confirm New Admin Password: ");
290 getPassword(confirmNewAdminPassword, sizeof(confirmNewAdminPassword));
291 if (strcmp(newAdminPassword, confirmNewAdminPassword) != 0) {
292 printf("Error: Passwords do not match.\n");
293 break;
294 }
295 if (addAdmin(newAdminUsername, newAdminPassword, currentAdminUsername, currentAdminPassword)) {
296 printf("New admin %s added successfully by %s.\n", newAdminUsername, currentAdminUsername);
297 } else {
298 printf("Error: Could not add new admin.\n");
299 }
300 break;
301 }
302 case 6: {
303 char delAdminUsername[MAX_USERNAME_LENGTH];
304 char currentAdminPassword[MAX_PASSWORD_LENGTH];
305 printf("To continue, please enter your (%s) current password: ", currentAdminUsername);
306 getPassword(currentAdminPassword, sizeof(currentAdminPassword));
307 if (validateAdmin(currentAdminUsername, currentAdminPassword)) {
308 printf("Password verified successfully.\n");
309 } else {
310 printf("Error: Invalid password.\n");
311 break;
312 }
313 printf("Enter Admin Username to Delete: ");
314 fgets(delAdminUsername, sizeof(delAdminUsername), stdin);
315 delAdminUsername[strcspn(delAdminUsername, "\n")] = 0;
316 if (!checkUsername(delAdminUsername)) {
317 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
318 break;
319 }
320 if (deleteAdmin(delAdminUsername, currentAdminUsername, currentAdminPassword)) {
321 printf("Admin %s deleted successfully by %s.\n", delAdminUsername, currentAdminUsername);
322 } else {
323 printf("Error: Could not delete admin.\n");
324 }
325 break;
326 }
327 case 7: {
328 char delHospitalCode[MAX_HOSPITAL_CODE_LENGTH];
329 char currentAdminPassword[MAX_PASSWORD_LENGTH];
330 printf("To continue, please enter your (%s) current password: ", currentAdminUsername);
331 getPassword(currentAdminPassword, sizeof(currentAdminPassword));
332 if (validateAdmin(currentAdminUsername, currentAdminPassword)) {
333 printf("Password verified successfully.\n");
334 } else {
335 printf("Error: Invalid password.\n");
336 break;
337 }
338 printf("Enter Hospital Code to Delete: ");
339 fgets(delHospitalCode, sizeof(delHospitalCode), stdin);
340 delHospitalCode[strcspn(delHospitalCode, "\n")] = 0;
341 if (containsPipe(delHospitalCode)) {
342 printf("Error: Hospital code cannot contain a pipe character.\n");
343 break;
344 }
345 if (deleteHospital(delHospitalCode, currentAdminUsername, currentAdminPassword)) {
346 printf("Hospital %s deleted successfully by %s.\n", delHospitalCode, currentAdminUsername);
347 } else {
348 printf("Error: Could not delete hospital.\n");
349 }
350 break;
351 }
352 case 8: {
353 displayAdmin();
354 break;
355 }
356 case 9: {
357 displayHospitals();
358 break;
359 }
360 case 10: {
361 displayBloodStocks();
362 break;
363 }
364 case 11: {
366 break;
367 }
368 case 12:
369 printf("Exiting admin panel...\n");
370 isAdmin = false;
371 break;
372 default:
373 printf("Error: Invalid option. Please try again.\n");
374 }
375 }
376 } else {
377 printf("Error: Invalid admin credentials.\n");
378 }
379 break;
380 }
381 case 5:
382 printf("Exiting...\n");
383 break;
384 default:
385 printf("Error: Invalid option. Please try again.\n");
386 }
387 if (choice == 5) {
388 break;
389 }
390 }
391 freeAdmin();
392 freeHospital();
393 freeBloodList();
394 return 0;
395}
Admin manager header file.
#define MAX_PASSWORD_LENGTH
Maximum password length.
#define MAX_USERNAME_LENGTH
Maximum username length.
Blood manager header file.
Hospital manager header file.
#define MAX_HOSPITAL_CODE_LENGTH
Maximum hospital code length.
#define MAX_HOSPITAL_LOCATION_LENGTH
Maximum hospital location length.
#define MAX_HOSPITAL_NAME_LENGTH
Maximum hospital name length.
Misc header file.
Transaction manager header file.
#define MAX_TRANSACTION_NAME_LENGTH
Maximum transaction name length.
TransactionType
Transaction type enum.
void displayTransactions(void)
Display all transactions.