-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
590 lines (504 loc) · 19.3 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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
//GLOBAL DATA
int map[10][2] = {{-1, -1}, //i don't think these -1s are strictly necessary BUT they are now used for the check rooms
{-1, -1}, //used subroutine so they must stay this way.
{-1, -1},
{-1, -1},
{-1, -1},
{-1, -1},
{-1, -1},
{-1, -1},
{-1, -1},
{-1, -1}}; //map of booking IDs to indices .. -- matthew: i had to pre-fill due to return index find matches from old memory. -1 should never be used in code as this stands for error
int tables[2][3] = {}; //boolean list of tables
int partyIndex = -1; //tells checkIn what index to insert at
//PARTY DATA - maximum 24 parties
char surname[24][127] = {};
int dateOfBirth[24][3] = {};
int guests[24][2]; //please don't put test values and then give the code and not remove them // array hold guest numbers
char boardType[24][2] = {};
int length[24] = {};
int dailyWakeUpCall[24] = {};
int roomsUsed[24][6] = {};
int digits_only(const char *s) {
for (int i = 0; i < strlen(s); i++) {
if (isdigit(s[i]) == 1) {
return 1;
}
}
return 0;
}
int returnIndex(int reference) { //returns index from the map
for (int i = 0; i < 10; i++) {
if (map[i][0] == reference) {
return map[i][1];
}
}
return -1;
}
int TotalGuests(int index) { //totals adult and child guests
return guests[index][0] + guests[index][1];
}
//for sanitation of inputs
int input_int(char *text) {
int run = 1;
int num = 0;
while (run == 1) {
printf("%s", text);
int scanfError = scanf("%5d", &num);
if (scanfError != 1) {
fflush(stdin);
fflush(stdout);
printf("Input error. Please enter an integer.\n");
num = 0;
} else {
fflush(stdin);
fflush(stdout);
run = 0;
}
}
return num;
}
char *input_char(char *text) {
fflush(stdin);
char *string = malloc(25);
printf("%s", text);
fgets(string, 25, stdin);
fflush(stdin);
fflush(stdout);
if (string[strlen(string) - 1] == '\n') {
string[strlen(string) - 1] = '\0';
}
fflush(stdin);
return string;
}
void BookTable() {
char names[3][32] = {"Endor", "Naboo", "Tatooine"};
char bookingId[256] = {};
strcpy(bookingId, input_char("Enter your booking ID?"));
// how do i put this this solution did not work at all probably because clion handles this type clever stuff different.
//old solution:
//int numbers = sscanf("%d",
// bookingId + strlen(bookingId) - 4); //use pointer arithmetic to get the last three letters
// of the string, use sscanf to convert last three digits to integer
//new solution this saved me so many headaches
char *p = bookingId;
while (isalpha(*p)) ++p;
int numbers = strtol(p, NULL, 10); //change to strtol to correctly report conversion errors.
if (numbers == 0) {
printf("Invalid booking ID - must be in format surnameXXX\n");
return;
}
int index = returnIndex(numbers); //fetch the index for this booking ID
if (index == -1) {
printf("Invalid booking ID\n");
return;
}
if (strcmp(boardType[index], "BB") == 0) {
printf("Only full and half board can book dinner\n");
return;
}
if (TotalGuests(index) > 4) {
printf("Tables can only seat 4\n");
return;
}
int checkAmount = 0;
for (int setting = 0; setting < 2; ++setting) { //iterate through the two settings
for (int i = 0; i < 3; ++i) { //iterate through the three tables
if (tables[setting][i] == 0) { //if this table is available
printf("Table %s is available at %d\n", names[i],
(setting * 2) + 7); //maths is to correctly convert from 0/1 to 7/9
checkAmount = checkAmount + 1;
}
}
printf("\n");
}
int timeIn = 0;
char table[64];
int flag = 0;
if (checkAmount == 0) {
printf("there are no tables at the moment");
} else {
do { //validation - will loop until a table is successfully booked
timeIn = input_int("What time do you want to book a table, 7 or 9?\n");
if (timeIn != 7 && timeIn != 9) { //invalid id
printf("Invalid time, try again\n");
continue;
}
timeIn = timeIn / 9; //int division trick to get the times to 0 or 1:
// 7/9 < 1 so it gets round down to 0, 9/9 = 1 so it stays as 1
strcpy(table, input_char("What table would you like to book?\n"));
switch (tolower(
table[0])) { //since we can't switch on a string, we use the 0th char as it is different for each table.
case 'e':
if (tables[timeIn][0] == 1) {
printf("This table is already booked\n");
} else {
tables[timeIn][0] = 1;
flag = 1;
}
break;
//fixed the lowercase case input thing you only had one working - matthew
case 'n':
if (tables[timeIn][1] == 1) {
printf("This table is already booked\n");
} else {
tables[timeIn][1] = 1;
flag = 1;
}
break;
//fixed the lowercase case input thing you only had one working - matthew
case 't':
if (tables[timeIn][2] == 1) {
printf("This table is already booked\n");
} else {
tables[timeIn][2] = 1;
flag = 1;
}
break;
default:
printf("That table doesn't exist\n");
break;
}
} while (flag == 0);
}
printf("Table booked.\n");
}
int boolCheck(char *string) {
while (1) {
char *getsp;
char answer;
getsp = input_char(string);
answer = toupper(getsp[0]);
if (answer == 'Y' || answer == 'N') {
if (answer == 'Y') {
return 1;
} else {
return 0;
}
} else {
printf("Not valid. Please enter y/n.\n");
return -1;
}
}
}
void GenerateID() {
srand(time(0));
partyIndex++;
int id = (rand() % (999 - 100 + 1)) + 100; //inclusive
map[partyIndex][0] = id;
map[partyIndex][1] = partyIndex; //set the second int to the index - this will be used for further lookup
}
int RoomInUseCheckAll(int numRoom) {
int counter = 0;
while (map[counter][0] != -1) { //iterate through all the booked rooms
for (int i = 0; i < 6; ++i) {
if (roomsUsed[counter][i] == numRoom) {
//the room is in use
return 1;
}
}
counter++;
}
return 0;
}
int IsLeapYear(int year) {
return (((year % 4 == 0) &&
(year % 100 != 0)) ||
(year % 400 == 0));
}
// returns 1 if given date is valid.
int IsValidDate() {
int MAX_YR = 2004;
int MIN_YR = 1900;
//check range of year,month and day
if (dateOfBirth[partyIndex][2] > MAX_YR || //check date of birth is in valid range
dateOfBirth[partyIndex][2] < MIN_YR) {
return 0;
}
if (dateOfBirth[partyIndex][1] < 1 || dateOfBirth[partyIndex][1] > 12) { //check year is valid
return 0;
}
if (dateOfBirth[partyIndex][0] < 1 || dateOfBirth[partyIndex][0] > 31) { //check month is valid
return 0;
}
//Handle feb days in leap year
if (dateOfBirth[partyIndex][1] == 2) {
if (IsLeapYear(dateOfBirth[partyIndex][2])) {
return (dateOfBirth[partyIndex][0] <= 29);
} else {
return (dateOfBirth[partyIndex][0] <= 28);
}
}
//handle months which has only 30 days
if (dateOfBirth[partyIndex][1] == 4 || dateOfBirth[partyIndex][1] == 6 ||
dateOfBirth[partyIndex][1] == 9 || dateOfBirth[partyIndex][1] == 11) {
return (dateOfBirth[partyIndex][0] <= 30);
} else {
return 1;
}
}
void checkin() {
if (RoomInUseCheckAll(1) == 1 &&
RoomInUseCheckAll(2) == 1 &&
RoomInUseCheckAll(3) == 1 &&
RoomInUseCheckAll(4) == 1 &&
RoomInUseCheckAll(5) == 1 &&
RoomInUseCheckAll(6) == 1
) {
printf("\nall rooms taken. Ending checkin.\n");
} else {
fflush(stdout);
int numbers = -1;
int numbers_child = -1;
int time = -1;
int numberOfRooms = -1;
int run = 1;
char *surinput;
GenerateID();
for (int i = 0; i < 7; i++) {
roomsUsed[partyIndex][i] = 0;
}
//gets surname input
do {
surinput = input_char("what is your surname?");
} while (digits_only(surinput) == 1);
int len = strlen(surinput);
if (surinput[len - 1] == '\n')
surinput[len - 1] = '\0';
//copies to surname
strcpy(surname[partyIndex], surinput);
//gets number of rooms
do {
printf("\nHow many rooms would you like to book? 1-6.");
numberOfRooms = input_int("\n");
} while (numberOfRooms > 6 || numberOfRooms < 1);
do {
time = input_int("how many days are you here for?");
if (time > 30 || time < 1) {
printf("\ntoo large or small time 1-30\n");
}
} while (time > 30 || time < 1);
printf("\n");
int hold_int = numberOfRooms;
for (int i = 0; i < hold_int; i++) {
//checks for if the number lies between 1 & 6
int whatRoom = input_int(
"what room would you like prices are:\nRoom 1 & 2 = 100,\nRoom 3 = 85,\nRoom 4 & 5 = 75,\nRoom 6 = 50,\n?");
if (whatRoom > 6 || whatRoom < 1) {
printf("error\n");
i--;
continue;
}
if (RoomInUseCheckAll(whatRoom) == 1) {
printf("invalid room in already in use\n");
i--;
continue;
} else {
roomsUsed[whatRoom][partyIndex] = 1;
}
do {
//gets inputs for number of people and length of stay.
numbers = input_int("how many adults are with you?");
numbers_child = input_int("how many children are with you?");
} while (time > 30 || time < 1 || numbers_child > 5 || numbers > 5 || numbers_child < 0 || numbers < 1);
length[partyIndex] = time;
guests[partyIndex][0] = guests[partyIndex][0] + numbers;
guests[partyIndex][1] = guests[partyIndex][1] + numbers_child;
roomsUsed[partyIndex][i] = whatRoom;
printf("\n");
int wakeUpHolder = 0;
do { //validation loop for wake up call input
wakeUpHolder = boolCheck("do you want a daily wake up call?");
} while (wakeUpHolder == -1);
dailyWakeUpCall[partyIndex] = wakeUpHolder;
}
int dateCheck = 0;
do {
printf("\n\n");
//gets date of birth
dateOfBirth[partyIndex][0] = input_int("what is your date of birth DD?");
dateOfBirth[partyIndex][1] = input_int("what is your date of birth MM?");
dateOfBirth[partyIndex][2] = input_int("what is your date of birth YYYY?");
dateCheck = IsValidDate();
if (dateCheck == 0) {
printf("\ninvalid date must be at least 16 years old.");
}
} while (dateCheck == 0);
do { //validation loop for taking in board type
printf("\n\n");
strcpy(boardType[partyIndex], input_char("Enter your board type, FB, HB or BB?\n"));
} while (!(
(tolower(boardType[partyIndex][0]) == 'f' || tolower(boardType[partyIndex][0]) == 'h' ||
tolower(boardType[partyIndex][0]) == 'b') &&
tolower(boardType[partyIndex][1] ==
'b'))); //not at all overcomplicated way to make sure it is FB, HB or BB, capitals no ignored.
printf("your booking ID is:\n%s%d\n", surname[partyIndex], map[partyIndex][0]);
fflush(stdout);
}
}
int AgeDifference(const int date1[3], const int date2[3]) {
int day1 = date1[0], mon1 = date1[1], year1 = date1[2];
int day2 = date2[0], mon2 = date2[1], year2 = date2[2];
int day_diff, mon_diff, year_diff;
if (day2 < day1) {
//borrow days from february
if (mon2 == 3) {
//check whether year is a leap year
if ((year2 % 4 == 0 && year2 % 100 != 0) || (year2 % 400 == 0)) {
day2 += 29;
} else {
day2 += 28;
}
}
//borrow days from April or June or September or November
else if (mon2 == 5 || mon2 == 7 || mon2 == 10 || mon2 == 12) {
day2 += 30;
}
//borrow days from Jan or Mar or May or July or Aug or Oct or Dec
else {
day2 += 31;
}
mon2 = mon2 - 1;
}
if (mon2 < mon1) {
mon2 += 12;
year2 -= 1;
}
day_diff = day2 - day1;
mon_diff = mon2 - mon1;
year_diff = year2 - year1;
printf("you are %d years old", year_diff);
return year_diff;
}
void Checkout(int bookingID) {
// Initialising all the variables
float lengthCost = 0;
float adultBoardRate = 0;
float adultBoardCost = 0; //multiplying by 0.5f means theses must be floats
float childBoardRate = 0;
float childBoardCost = 0;
float totalBoardCost = 0;
float wakeUpCost = 0;
float overallCost = 0;
int index = returnIndex(bookingID);
int roomPrices[6][2] = {
{1, 100},
{2, 100},
{3, 85},
{4, 75},
{5, 75},
{6, 50}
};
// This loop is used to go through all data in the roomsUsed array to check which rooms are used
for (int i = 0; i < 6; i++) {
// This loops loops through the first column of the roomPrices array every time the
// roomsUsed array goes to the next value. This way it checks every value in that array against the first column
for (int j = 0; j < 6; j++) {
// When the values are equal it will multiply the cost by the amount of days stayed to get the room price
if (roomsUsed[index][i] == roomPrices[j][0]) {
lengthCost = lengthCost + (roomPrices[j][1] * length[index]);
}
}
}
int current_time[3] = {15, 11, 2020};
if (AgeDifference(dateOfBirth[index], current_time) > 65) {
lengthCost = lengthCost * 0.9;
}
if (strcmp((const char *) boardType[index], "FB") == 0) {
adultBoardRate = guests[index][0] * 20;
adultBoardCost = adultBoardRate * length[index];
childBoardRate = guests[index][1] * 20;
childBoardCost = childBoardRate * length[index] * 0.5;
totalBoardCost = adultBoardCost + childBoardCost;
} else if (strcmp((const char *) boardType[index], "HB") == 0) {
adultBoardRate = guests[index][0] * 15;
adultBoardCost = adultBoardRate * length[index];
childBoardRate = guests[index][1] * 15;
childBoardCost = childBoardRate * length[index] * 0.5;
totalBoardCost = adultBoardCost + childBoardCost;
} else if (strcmp((const char *) boardType[index], "BB") == 0) {
adultBoardRate = guests[index][0] * 5;
adultBoardCost = adultBoardRate * length[index];
childBoardRate = guests[index][1] * 5;
childBoardCost = childBoardRate * length[index] * 0.5;
totalBoardCost = adultBoardCost + childBoardCost;
}
if (dailyWakeUpCall[index] == 1) {
wakeUpCost = 5;
}
printf("\nBILL FOR %s%d:", surname[partyIndex], map[partyIndex][0]);
if (lengthCost > 0) {
printf("\nTotal cost for rooms stayed in is: GBP%.2f", lengthCost);
}
if (totalBoardCost > 0) {
printf("\nTotal cost for meals is: GBP%.2f", totalBoardCost);
printf("\n\tCost for adults' meals was: GBP%.2f", adultBoardCost);
printf("\n\tCost for children's meals was: GBP%.2f", childBoardCost);
}
if (wakeUpCost > 0) {
printf("\nTotal cost for wake up calls is: GBP%.2f", wakeUpCost);
}
overallCost = lengthCost + totalBoardCost + wakeUpCost;
printf("\n\nOverall cost for the stay is: GBP%.2f", overallCost);
//you made do sneak?>
}
}
int main() {
setbuf(stdout, NULL);
//gets tests
char reference[8];
char idChecked[26] = {"test"};
int run = 1;
while (run == 1) {
//gets input and start switch case
printf("\n");
switch (input_int(
"\nwhat would you like to do?\n1.check in\n2.book a table\n3.check out\n4.quit the program\n?")) {
case 1:
//starts the checkin subroutine
checkin();
break;
case 2:
//starts the subroutine
BookTable();
break;
case 3:
//checks if contains numbers
strcpy(idChecked, input_char("\nwhat is your bookingID?"));
if (digits_only(idChecked) == 1) {
//all hall stackoverflow for this fix. It took some time
//gets numbers from string
char *p = idChecked;
while (isalpha(*p)) ++p;
int numbers = atoi(p);
if (returnIndex(numbers) != -1) { //fetch the index for this booking ID
//if yes then passes into sub routine.
Checkout(numbers);
} else {
//if no then returns a error
printf("\nerror invalid bookingID\n");
}
} else {
//if no then the case and switch goes to start and return not valid
printf("\nnot valid\n");
}
break;
case 4:
//this stops the program
run = 0;
case 5:
printf("\nmade by following\nprogrammers:\ncharles\nphilip\nmatthew\nRandom bug tester:\njackus: 'there is always bugs!' like this one\n");
break;
default:
//input wasn't any of the cases
printf("\nINPUT error.\n");
break;
}
}
return 0;
}