-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst-version.c
468 lines (290 loc) · 10.4 KB
/
first-version.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAX_MEDS 100
#define MAX_NAME_LENGTH 60
#define MAX_BRAND_LENGTH 60
#define MAX_DAY 31
#define MAX_MONTH 12
#define MIN_YEAR 2000
typedef int date[3]; // dd/mm/yy
typedef struct // a function that holds medication infos
{
char* medication_name;
int available_quantity;
int unit_price;
date manufacturing_date;
date expiry_date;
char medication_brand[60];
}med_info;
void ctr_date(date d);
int isExpired(med_info d);
void add_medication(med_info m);
void format_date(int date[], char formatted_date[]);
void display_stock(med_info m[]); // theres a display bug here some lines is messy and some are not
void update_medication_quantity(med_info* medication, int new_quantity);
void initialize_medication(med_info* m, const char* name, int quantity, int unit_price, int year, int month, int day, const char* brand);
int main()
{
med_info m[MAX_MEDS]; // Use the predefined array instead of allocating memory
initialize_medication(&m[0], "paracetamol", 100, 10, 2024, 1, 1, "panadol");
initialize_medication(&m[1], "ibuprofen", 100, 15, 2024, 2, 15, "advil");
initialize_medication(&m[2], "Cetirizine", 30, 20, 2024, 3, 10, "zyrtec");
initialize_medication(&m[3], "Aspirin", 80, 8, 2024, 4, 5, "bayer");
initialize_medication(&m[4], "Diphenhydramine", 60, 12, 2024, 4, 30, "benadryl");
initialize_medication(&m[5], "Imane", 19, 12, 2024, 4, 30, "benadryl");
// Initialize medication_name and medication_brand to empty strings
for (int i = 0; i < MAX_MEDS; ++i)
{
m[i].medication_name[0] = '\0';
m[i].medication_brand[0] = '\0';
}
strcpy(m[0].medication_name, "paracetamol");
m[0].available_quantity = 100;
m[0].unit_price = 10;
m[0].manufacturing_date[0] = 1;
m[0].manufacturing_date[1] = 1;
m[0].manufacturing_date[2] = 2024;
m[0].expiry_date[0] = 31;
m[0].expiry_date[1] = 12;
m[0].expiry_date[2] = 2026;
strcpy(m[0].medication_brand, "panadol");
strcpy(m[1].medication_name, "ibuprofen");
m[1].available_quantity = 100;
m[1].unit_price = 15;
m[1].manufacturing_date[0] = 15;
m[1].manufacturing_date[1] = 2;
m[1].manufacturing_date[2] = 2024;
m[1].expiry_date[0] = 31;
m[1].expiry_date[1] = 1;
m[1].expiry_date[2] = 2027;
strcpy(m[1].medication_brand, "advil");
strcpy(m[2].medication_name, "Cetirizine");
m[2].available_quantity = 30;
m[2].unit_price = 20;
m[2].manufacturing_date[0] = 10;
m[2].manufacturing_date[1] = 3;
m[2].manufacturing_date[2] = 2024;
m[2].expiry_date[0] = 28;
m[2].expiry_date[1] = 2;
m[2].expiry_date[2] = 2027;
strcpy(m[2].medication_brand, "zyrtec");
strcpy(m[3].medication_name, "Aspirin");
m[3].available_quantity = 80;
m[3].unit_price = 8;
m[3].manufacturing_date[0] = 5;
m[3].manufacturing_date[1] = 4;
m[3].manufacturing_date[2] = 2024;
m[3].expiry_date[0] = 3;
m[3].expiry_date[1] = 3;
m[3].expiry_date[2] = 2027;
strcpy(m[3].medication_brand, "bayer");
strcpy(m[4].medication_name, "Diphenhydramine");
m[4].available_quantity = 60;
m[4].unit_price = 12;
m[4].manufacturing_date[0] = 30;
m[4].manufacturing_date[1] = 4;
m[4].manufacturing_date[2] = 2024;
m[4].expiry_date[0] = 30;
m[4].expiry_date[1] = 4;
m[4].expiry_date[2] = 2027;
strcpy(m[4].medication_brand, "benadryl");
strcpy(m[5].medication_name, "imane");
m[5].available_quantity = 19;
m[5].unit_price = 12;
m[5].manufacturing_date[0] = 30;
m[5].manufacturing_date[1] = 4;
m[5].manufacturing_date[2] = 2024;
m[5].expiry_date[0] = 30;
m[5].expiry_date[1] = 4;
m[5].expiry_date[2] = 2027;
strcpy(m[5].medication_brand, "benadryl");
display_stock(m);
/*
3. Search for a medication by the first characters of its name:
- The user should be able to enter the first characters of the medication name they are
If multiple medications match these characters, the program should provide a list of
suggestions to the user to choose the desired medication.
*/
printf("\n\n");
int found_count=0;
med_info found_meds[MAX_MEDS]; // Array to store found medications
printf("Hello, enter please the first letter of the medication you want to find: ");
char first_letter;
scanf(" %c", &first_letter); // Use space before %c to consume newline from previous input
for (int i = 0; i < MAX_MEDS; i++)
{
if (m[i].medication_name[0] == first_letter){
// Copy entire medication name, not just the first letter
strcpy(found_meds[found_count].medication_name, m[i].medication_name);
found_count++;
}
}
if (found_count > 0) {
printf("Found medications:\n");
for (int i = 0; i < found_count; i++)
{
printf("%s\n", found_meds[i].medication_name);
}
}
else {
printf("\nNo medication found starting with '%c'.\n", first_letter);
}
printf("Enter the number corresponding to your desired medication (or 0 to cancel): ");
int user_choice;
scanf("%d", &user_choice);
int selected_index = user_choice - 1;
if (user_choice > 0 && user_choice <= found_count)
{
// User chose a valid suggestion
// Convert choice to index (0-based)
// Access the selected medication using found_meds[selected_index]
printf("You selected: %s\n", found_meds[selected_index].medication_name);
// Perform actions based on the selected medication (optional)
} else if (user_choice == 0) {
printf("Selection cancelled.\n");
} else {
printf("\n\n");
printf("Invalid choice. Please enter a number between 1 and %d (or 0 to cancel).\n", found_count);
}
/*4. Update the available quantity of a medication in stock:
- The program should allow the user to update the available (quantity) of a specific
medication in the stock.
*/
int desired_choice;
int yes = 1;
int no = 0;
int cancel = -1;
int new_quantity = 0;
printf("\n\n");
printf("* Do you want to update the quantity of the medication %s (yes: 1, no: 0, cancel: -1): ", found_meds[selected_index].medication_name);
scanf("%d", &desired_choice);
if (desired_choice == yes) {
printf("The current quantity of this medication is %d. Enter the new quantity: ", found_meds[selected_index].available_quantity);
scanf("%d", &new_quantity);
// Update the medication quantity using a pointer
update_medication_quantity(&found_meds[selected_index], new_quantity);
} else if (desired_choice == no) {
// ... (rest of your code for choosing another medication remains the same)
} else {
printf("Update cancelled.\n");
}
return 0;}
void ctr_date(date d) // a function that controls the date given by the user
{
int bool=0;
while(bool==0)
{
printf("give the date:");
scanf("%d",d[0]);
if((d[0]<=0)|| (d[0]>31))
{
printf("the day should be between 1 - 31 \n");
bool =0;
}
else
bool =1;
}
bool=0;
while(bool==0)
{
printf("enter the month :");
scanf("%d",&d[1]);
if((d[1]<=0)|| (d[1]>12))
{
printf("the month should be a value in between 1 -12 \n");
}
else bool=1;
}
bool =0;
while(bool==0)
{
printf("enter the year :");
scanf("%d",&d[2]);
if((d[2]<2000))
{
printf("the year should be superior than 2000.\n");
}
else bool=1;
}
}
int isExpired(med_info d)
{
time_t now = time(NULL);
struct tm *local_time = localtime(&now);
if (local_time == NULL) {
printf("Failed to get the current time.\n");
return 0;
}
int current_day = local_time->tm_mday;
int current_month = local_time->tm_mon + 1; // Month is 0-indexed
int current_year = local_time->tm_year + 1900; // Years since 1900
if (current_year > d.expiry_date[2] ||
(current_year == d.expiry_date[2] && current_month > d.expiry_date[1]) ||
(current_year == d.expiry_date[2] && current_month == d.expiry_date[1] && current_day > d.expiry_date[0])) {
return 1; // Item is expired
}
return 0; // Item is not expired
}
void add_medication(med_info m)
{
if (isExpired(m)==0)
{
m.available_quantity++;
}
printf("the available quantity is : %d\n",m.available_quantity);}
void format_date(int date[], char formatted_date[]) {
sprintf(formatted_date, "%02d/%02d/%04d", date[0], date[1], date[2]);
}
void display_stock(med_info m[]) {
printf("Name\t\tAvailable Quantity\tUp\tMnf Date\t\tExp Date\t\t\tBrand\n");
printf("\n");
for (int i = 0; i < 6; i++) {
char manufacturing_date_str[11], expiry_date_str[11];
format_date(m[i].manufacturing_date, manufacturing_date_str);
format_date(m[i].expiry_date, expiry_date_str);
printf("%-20s\t%-3d\t\t%-7d\t%-20s\t%-20s\t\t%s\n",
m[i].medication_name,
m[i].available_quantity,
m[i].unit_price,
manufacturing_date_str,
expiry_date_str,
m[i].medication_brand);
}
}
void update_medication_quantity(med_info* medication, int new_quantity) {
if (medication == NULL) {
printf("Error: Medication pointer is NULL\n");
return;
}
medication->available_quantity = new_quantity;
printf("Quantity updated successfully!\n");
}
void initialize_medication(med_info* m, const char* name, int quantity, int unit_price, int year, int month, int day, const char* brand)
{
strcpy(m->medication_name, name);
m->available_quantity = quantity;
m->unit_price = unit_price;
m->manufacturing_date[0] = day;
m->manufacturing_date[1] = month;
m->manufacturing_date[2] = year;
m->expiry_date[0] = day;
m->expiry_date[1] = month;
m->expiry_date[2] = year + 2; // Expiry date is two years after manufacturing date
strcpy(m->medication_brand, brand);
}
/*
printf("enter the medication name : \n");
scanf("%s",m[i].medication_name);
printf("enter the medication brand : \n");
scanf("%s",m[i].medication_brand);
printf("enter the quantity of this medication : \n");
scanf("%d",&m[i].available_quantity);
printf("enter the unit prince : \n");
scanf("%d",m[i].unit_price);
printf("enter the manufacturing date : \n");
scanf("%d",&m[i].manufacturing_date);
printf("enter the expiry date : \n");
scanf("%d",&m[i].expiry_date);
*/