-
Notifications
You must be signed in to change notification settings - Fork 2
/
order.c
798 lines (646 loc) · 27.2 KB
/
order.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
//ordering mechanics
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "order.h"
#define BASE_PRICE 360 //Rp. / km
#define TRAIN_AVG_SPEED 84 //kph
#define ETA_DEV_PCT 0.36 //probability/coefficient of route time calculation deviation
//ordering vars
const char* day_name[] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
//station list by distance from Gambir
const distance_kmeter distance_list[] = {
0, //0. Gambir
218, //1. Cirebon
442, //2. Semarang
742, //3. Surabaya
870 //4. Malang
};
const char *station_list[] = {
"Gambir",
"Cirebon",
"Semarang",
"Surabaya",
"Malang"
};
void orderDateInput(Order* dateless_order, bool* bypass_bool){
//function's vars
int dt, mt, yr; //date vars
bool date_menu_loop = TRUE; //make loopable
//GET current time
time_t now = time(NULL);
struct tm current_time = *localtime(&now); //months, days in week, days in year start at 0, years start at 1900
while(date_menu_loop){
printf("Masukkan tanggal keberangkatan (dd mm yyyy): ");
scanf("%d%d%d", &dt, &mt, &yr);
//date checking alg.
//1. check if date values are valid
//non zero check
if(dt < 1 || mt < 1 || yr < 1){
printf(INPUT_ERROR); printf("\n");
continue;
} //or just absolutize these values...?
//if two digit number entered as year
if(yr < 100){
yr += 2000;
}
bool is_leap_year = (yr % 400 == 0)? TRUE : FALSE; //initialize leap year boole by checking if it's centurial leap year
if(!is_leap_year){ //if it's not centurial leap year...
if(yr % 100 != 0){ //AND not centurial year...
if(yr % 4 == 0){ //AND divisible by 4...
is_leap_year = TRUE; //it is a leap year
}
}
}
if(yr > 9999){
printf(INPUT_ERROR); printf("\n");
continue;
}
if(mt > 12){ //check if month is valid
printf(INPUT_ERROR); printf("\n");
continue;
}
switch(mt){ //check number of day in month for each
case 1: //jan
case 3: //mar
case 5: //may
case 7: //jul
case 8: //aug
case 10:https://oct
case 12:https://dec
if(dt > 31){
printf(INPUT_ERROR); printf("\n");
continue;
}
break;
case 2: //feb
if(dt > 28 + (int)is_leap_year){ //is_leap_year is a bool that IS also an uint
printf(INPUT_ERROR); printf("\n");
continue;
}
break;
default: //else...
if(dt > 30){
printf(INPUT_ERROR); printf("\n");
continue;
}
}
//2. check if date is not in the past
if(yr < current_time.tm_year + 1900){
printf(TIME_ERROR); printf("\n");
continue;
}
else if(yr == current_time.tm_year + 1900){
if(mt < current_time.tm_mon + 1){
printf(TIME_ERROR); printf("\n");
continue;
}
else if(mt == current_time.tm_mon + 1){
if(dt < current_time.tm_mday){
printf(TIME_ERROR); printf("\n");
continue;
}
}
}
//end of checking stuff
//SET departure date time struct, for stuff
struct tm departure_time = {.tm_mday = dt, .tm_mon = mt - 1, .tm_year = yr - 1900, .tm_isdst = -1};
mktime(&departure_time);
printf("Tanggal yg dipilih: %s, %d-%d-%d.\n\n0: Mengulang pemilihan\n1: Melanjutkan transaksi\nX: Membatalkan transaksi\nMasukkan perintah: ", day_name[departure_time.tm_wday], departure_time.tm_mday, departure_time.tm_mon + 1, departure_time.tm_year + 1900);
//loopback
getchar(); //clears buffer from '\n'(?)
char opt_ctr;
opt_ctr = getchar();
if(opt_ctr == '1'){
//set to new order container
dateless_order->date = dt;
dateless_order->month = mt;
dateless_order->year = yr;
strcpy(dateless_order->day, day_name[departure_time.tm_wday]);
//end this while loop
date_menu_loop = FALSE;
}
else if (opt_ctr == 'X'){
*bypass_bool = TRUE;
date_menu_loop = FALSE;
}
//user still doesn't really need to input 0 to retry...
}
}
void station_list_and_ask(const char* list_head){ //unnecessary, but helps scalability
printf("Pilihan stasiun %s: \n", list_head);
for(index i = 0; i < (sizeof(distance_list)/sizeof(int)); i++){
printf("\t%u.%s\n", i + 1, station_list[i]);
}
/*
printf("\t1.Jakarta\n");
printf("\t2.Cirebon\n");
printf("\t3.Semarang\n");
printf("\t4.Surabaya\n");
printf("\t5.Malang\n");
*/
printf("Pilihan: ");
}
void orderRouteInput(Order* routeless_order, bool* bypass_bool){
//calcellation impl.
if(*bypass_bool){
return;
}
//route func. vars
bool route_menu_loop = TRUE;
index org; //origin station option container
index dst; //dest. station opt. ctr.
system(CLEAR_SCREEN);
//menu loop
while(route_menu_loop){
//origin
station_list_and_ask("asal");
scanf("%u", &org);
//dest.
station_list_and_ask("tujuan");
scanf("%u", &dst);
//index adjustment
--org;
--dst;
//check if input is valid
if((org >= sizeof(distance_list)/sizeof(distance_list[0])) || (dst >= sizeof(distance_list)/sizeof(distance_list[0]))){
printf(INPUT_ERROR);
printf("\n\n");
continue;
}
else if(org == dst){
printf(STAT_ERROR);
printf("\n\n");
continue;
}
else{
routeless_order->origin_idx = org;
routeless_order->destination_idx = dst;
}
printf("\nPesanan:\n");
printf("\tTanggal\t\t: ");
printf("%s, %d-%d-%d.\n", routeless_order->day, routeless_order->date, routeless_order->month, routeless_order->year);
printf("\tStasiun asal\t: %s.\n", station_list[routeless_order->origin_idx]);
printf("\tStasiun tujuan\t: %s.\n", station_list[routeless_order->destination_idx]);
printf("\n0: Mengulang pemilihan\n1: Melanjutkan transaksi\nX: Membatalkan transaksi\nMasukkan perintah: ");
getchar(); //clears buffer from '\n'(?)
char opt_ctr;
opt_ctr = getchar();
if(opt_ctr == '1'){
//end this while loop
route_menu_loop = FALSE;
}
else if(opt_ctr == 'X'){
*bypass_bool = TRUE;
route_menu_loop = FALSE;
}
//user still doesn't really need to input 0 again
}
}
void trainSelector(Order* trainless_order, Train* tgarage[], int tgarage_size, bool* bypass_bool){
//calcellation impl.
if(*bypass_bool){
return;
}
//train select vars.
bool tselect_menu_loop = TRUE;
index optptr; //opt. container
//to supress certain randomness
bool is_repeat = FALSE; //bool for wheter the menu loop happens after an error in input
int eta_ctr[tgarage_size]; //contains previous value of eta time value
float price[tgarage_size]; //contains price
//GET current time
time_t now = time(NULL);
struct tm current_time = *localtime(&now);
system(CLEAR_SCREEN);
while(tselect_menu_loop){
printf("Pilihan kereta\n");
printf("Untuk %s, %d-%d-%d dari %s menuju %s:\n\n", trainless_order->day, trainless_order->date, trainless_order->month, trainless_order->year, station_list[trainless_order->origin_idx], station_list[trainless_order->destination_idx]);
for(index i = 0; i < tgarage_size; i++){
//ETA analysis
//generate time struct for departure
struct tm ttime = {
.tm_year = trainless_order->year - 1900,
.tm_mon = trainless_order->month - 1,
.tm_mday = trainless_order->date,
.tm_hour = tgarage[i]->hour,
.tm_min = tgarage[i]->minute,
.tm_sec = tgarage[i]->second
};
time_t dtime = mktime(&ttime); //departure time value
//ETA calculation
int route_distance = abs(distance_list[trainless_order->destination_idx] - distance_list[trainless_order->origin_idx]);
int route_est_time_seconds; //the eta time
if(!is_repeat){ //if current cycle is not one after input error, eta time randomized as usual
route_est_time_seconds = 3600*(float)route_distance/TRAIN_AVG_SPEED;
route_est_time_seconds = randTimeGen((1.0 - 0.25*ETA_DEV_PCT) * route_est_time_seconds, (1.0 + 0.75*ETA_DEV_PCT) * route_est_time_seconds); //weight deviation range to more late than early by 1:3.
eta_ctr[i] = route_est_time_seconds; //this stores current eta time
}
else{ //if current loop happens after input error, eta time DOES NOT randomized. assigned to value on previous cycle instead
route_est_time_seconds = eta_ctr[i];
}
//ETA
time_t eta_second = dtime + (time_t)route_est_time_seconds; //eta time value
struct tm eta_time = *localtime(&eta_second);
float rtime = difftime(eta_second, dtime);
//price
price[i] = tgarage[i]->price_multiplier * BASE_PRICE * route_distance;
//view train spesc.
printf("%d:", i + 1);
printf("\t[%s]\n", tgarage[i]->train_name);
printf("\tKeberangkatan\t: %02d:%02d:%02d (%s,%02d-%02d-%d)\n", tgarage[i]->hour, tgarage[i]->minute, tgarage[i]->second, trainless_order->day, trainless_order->date, trainless_order->month, trainless_order->year);
printf("\tETA\t\t: %02d:%02d:%02d (%s,%02d-%02d-%d) <~%.1f jam>\n", eta_time.tm_hour, eta_time.tm_min, eta_time.tm_sec, day_name[eta_time.tm_wday], eta_time.tm_mday, eta_time.tm_mon + 1, eta_time.tm_year + 1900, rtime/3600);
printf("\tJumlah gerbong\t: %d\n", tgarage[i]->train_length);
printf("\tJumlah seat\t: %d/%d\n", tgarage[i]->psg_seat_x * tgarage[i]->psg_seat_y, tgarage[i]->train_length * tgarage[i]->psg_seat_x * tgarage[i]->psg_seat_y);
printf("\tSeat tersedia\t: %d\n", freeSeatCalc(tgarage[i]));
printf("\tHarga\t\t: Rp.%.2f\n\n", price[i]);
}
printf("pilihan kereta (1-%d): ", tgarage_size);
scanf("%u", &optptr);
--optptr; //index adj.
//input checker
if(optptr < 0 || optptr >= tgarage_size){
is_repeat = TRUE;
printf(INPUT_ERROR);
printf("\n\n");
continue;
}
else{
trainless_order->train_index = optptr;
trainless_order->hour = tgarage[optptr]->hour;
trainless_order->minute = tgarage[optptr]->minute;
trainless_order->second = tgarage[optptr]->second;
trainless_order->price = price[optptr];
}
printf("\n0: Mengulang pemilihan\n1: Melanjutkan transaksi\nX: Membatalkan transaksi\nMasukkan perintah: ");
getchar(); //clears buffer from '\n'(?)
char opt_ctr;
opt_ctr = getchar();
if(opt_ctr == '1'){
//end this while loop
tselect_menu_loop = FALSE;
}
else if(opt_ctr == 'X'){
*bypass_bool = TRUE;
tselect_menu_loop = FALSE;
}
//user still doesn't really need to input 0 again
}
}
void seatSelector(Order* seatless_order, Train* tgarage[], int tgarage_size, bool* bypass_bool){
//calcellation impl.
if(*bypass_bool){
return;
}
//this func vars.
bool seat_menu_loop = TRUE;
int optptr;
system(CLEAR_SCREEN);
while(seat_menu_loop){
//menu texts
printf("[%s] %02d:%02d:%02d %s, %02d-%02d-%d (%d seat tersedia).\n",
tgarage[seatless_order->train_index]->train_name,
seatless_order->hour,
seatless_order->minute,
seatless_order->second,
seatless_order->day,
seatless_order->date,
seatless_order->month,
seatless_order->year,
freeSeatCalc(tgarage[seatless_order->train_index])
);
printf("Pilihan seat yang tersedia:\n\n");
char ncl; //car char container
int scar; //car number
int scol; //input column (seaty) as char
int srow; //input row (seatx) as int
trainMapper(tgarage[seatless_order->train_index]);
printf("Masukkan pilihan seat [gerbong kolom baris] (contoh: 2 A 5): ");
scanf("%d %c %d", &scar, &ncl, &srow);
//input validator
scol = (int)ncl; //cast chat to int (ASCII)
--srow; //adjust index
--scar; //adjust index
if(scol >= 'A' && scol <= 'Z'){ //ASCII checking
scol -= 'A';
}
else if(scol >= 'a' && scol <= 'z'){
scol -= 'a';
}
if((scar < 0 || scar >= tgarage[seatless_order->train_index]->train_length) && (scol < 0 || scol >= tgarage[seatless_order->train_index]->psg_seat_y) && (srow < 0 || srow >= tgarage[seatless_order->train_index]->psg_seat_x)){ //range checking
printf(INPUT_ERROR);
printf(": gerbong, kolom, dan baris");
printf("(%d %c %d)", scar + 1, ncl, srow + 1);
printf("\n");
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
printf("\n\n");
continue;
}
else if((scol < 0 || scol >= tgarage[seatless_order->train_index]->psg_seat_y) && (srow < 0 || srow >= tgarage[seatless_order->train_index]->psg_seat_x)){ //range checking
printf(INPUT_ERROR);
printf(": kolom dan baris");
printf("(%c %d)", ncl, srow + 1);
printf("\n");
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
printf("\n\n");
continue;
}
else if((scar < 0 || scar >= tgarage[seatless_order->train_index]->train_length) && (scol < 0 || scol >= tgarage[seatless_order->train_index]->psg_seat_y)){ //range checking
printf(INPUT_ERROR);
printf(": gerbong dan kolom");
printf("(%d %c)", scar + 1, ncl);
printf("\n");
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
printf("\n\n");
continue;
}
else if((scar < 0 || scar >= tgarage[seatless_order->train_index]->train_length) && (srow < 0 || srow >= tgarage[seatless_order->train_index]->psg_seat_x)){
printf(INPUT_ERROR);
printf(": gerbong dan baris");
printf("(%d %d)", scar + 1, srow + 1);
printf("\n");
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
printf("\n\n");
continue;
}
else if(scol < 0 || scol >= tgarage[seatless_order->train_index]->psg_seat_y){ //range checking
printf(INPUT_ERROR);
printf(": kolom");
printf("(%c)", ncl);
printf("\n");
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
printf("\n\n");
continue;
}
else if(srow < 0 || srow >= tgarage[seatless_order->train_index]->psg_seat_x){ //range checking
printf(INPUT_ERROR);
printf(": baris");
printf("(%d)", srow + 1);
printf("\n");
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
printf("\n\n");
continue;
}
else if(scar < 0 || scar >= tgarage[seatless_order->train_index]->train_length){
printf(INPUT_ERROR);
printf(": gerbong");
printf("(%d)", scar + 1);
printf("\n");
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
printf("\n\n");
continue;
}
else{
printf("Kursi tersedia...\n");
}
if(seatSetter(tgarage[seatless_order->train_index], DRIFT, scar, srow, scol)){ //sets seat to DRIFT to detect in mapper func.
system(CLEAR_SCREEN);
printf("Pemilihan seat berhasil!\n");
seatless_order->pcar = scar;
seatless_order->pseatx = srow ;
seatless_order->pseaty = scol;
trainMapper(tgarage[seatless_order->train_index]);
printf("\nSeat terpilih ditandai '#'\n\n");
printf("\n0: Mengulang pemilihan\n1: Melanjutkan transaksi\nX: Membatalkan transaksi\nMasukkan perintah: ");
getchar(); //clear \n
char opt_ctr;
opt_ctr = getchar();
if(opt_ctr == '1'){
seatSetter(tgarage[seatless_order->train_index], TRUE, scar, srow, scol); //sets DRIFT-ed seat to TRUE
seat_menu_loop = FALSE;
}
else if(opt_ctr == 'X'){
*bypass_bool = TRUE;
seat_menu_loop = FALSE;
}
else{
seatSetter(tgarage[seatless_order->train_index], FALSE, scar, srow, scol); //reset seat to empty
}
}
else{
printf("Gagal memilih seat...\n");
printf("Masukkan 0 untuk mengulangi: ");
pause_scr('0');
continue;
}
}
}
void finalizeOrder(Order* props, Train* tgarage[], int tgarage_size, bool* bypass_bool){
//calcellation impl.
if(*bypass_bool){
return;
}
int optvar;
char name[50];
char phone[20];
bool final_menu_loop = TRUE;
while(final_menu_loop){
system(CLEAR_SCREEN);
//summary viewer
printf("Ringkasan pesanan:\n");
printf("\tAsal\t: %s\n", station_list[props->origin_idx]);
printf("\tTujuan\t: %s\n", station_list[props->destination_idx]);
printf("\tKereta\t: %s\n", tgarage[props->train_index]->train_name);
printf("\tJadwal\t: %s, %02d-%02d-%d @ %02d:%02d:%02d\n", props->day, props->date, props->month, props->year, props->hour, props->minute, props->second);
printf("\tSeat\t: %d%c%d\n", props->pcar + 1, props->pseaty + 'A', props->pseatx + 1);
printf("\tHarga\t: Rp.%.2d", props->price);
printf("\n\n#######################################################\n\n");
printf("\t1. Lanjut.\n");
printf("\t2. Perbaiki jadwal.\n");
printf("\t3. Perbaiki rute.\n");
printf("\t4. Pilih ulang kereta.\n");
printf("\t5. Pilih ulang seat.\n");
printf("\t6. Batalkan transaksi.\n");
printf("\nOpsi: ");
scanf("%d", &optvar);
//function reading bool
bool is_passable = FALSE;
switch(optvar){
case 1:
getchar(); //catch wild "\n"
printf("Masukkan nama\t\t: ");
scanf("%[^\n]%*c", name);
printf("Masukkan no telp.\t: ");
scanf("%s", phone);
strcpy(props->name, name);
strcpy(props->phone_num, phone);
final_menu_loop = FALSE;
break;
case 2:
orderDateInput(props, &is_passable);
orderRouteInput(props, &is_passable);
trainSelector(props, tgarage, tgarage_size, &is_passable);
seatSelector(props, tgarage, tgarage_size, &is_passable);
break;
case 3:
orderRouteInput(props, &is_passable);
trainSelector(props, tgarage, tgarage_size, &is_passable);
seatSelector(props, tgarage, tgarage_size, &is_passable);
break;
case 4:
trainSelector(props, tgarage, tgarage_size, &is_passable);
seatSelector(props, tgarage, tgarage_size, &is_passable);
break;
case 5:
seatSelector(props, tgarage, tgarage_size, &is_passable);
break;
case 6:
*bypass_bool = TRUE;
final_menu_loop = FALSE;
return; //to end this func.
default:
printf(INPUT_ERROR);
printf("Masukkan 0 untuk lanjut: ");
pause_scr('0');
finalizeOrder(props, tgarage, tgarage_size, is_passable); //a recursion
continue;
}
//order ID setter
char order_ID_c[20];
//two-last-digit-of-year setter
char year[10];
sprintf(year, "%d", props->year); //int to char*
sprintf(year, "%c%c", year[strlen(year) - 2], year[strlen(year) - 1]); //get 2 last digit
sprintf( //assign formatted string to ordid
order_ID_c,
"%02d%02d%s%02d%02d%02d%c%d%c", //"ddmmyyhhmmssTxy"
props->date,
props->month,
year,
props->hour,
props->minute,
props->second,
tgarage[props->train_index]->train_name[0],
props->pseatx,
(char)(props->pseaty + 'A')
);
strcpy(props->order_ID, order_ID_c);
srand(time(NULL)); //seed randomization
props->is_occupied_L = randTimeGen(RAND_MAX/2, RAND_MAX);
props->is_occupied_R = props->is_occupied_L;
}
}
int orderListArr(Order order_list_arr[], index order_arr_size){ //order array placement func. return -1 when array full
for(index i = 0; i < order_arr_size; i++){
if(order_list_arr[i].is_occupied_L > 0 || order_list_arr[i].is_occupied_R > 0){ //if either pair is non-zero (to check if order initialized to trash in memory)
if(order_list_arr[i].is_occupied_L != order_list_arr[i].is_occupied_R){
return i;
}
}
else if(order_list_arr[i].is_occupied_L == 0 && order_list_arr[i].is_occupied_R == 0){ //if both pair is 0. if struct member assigned to 0 when initialized.
return i;
}
}
return -1;
}
void ticketView(Order* props, int ord_idx, Train* tgarage[], int tgarage_size, bool* bypass_bool){
//calcellation impl.
if(*bypass_bool){
return;
}
char str_ctr[100]; //temporary string container
int str_sz; //temporary
system(CLEAR_SCREEN);
printf("//TIKET//\n\n");
for(int i = 0; i < 50; i++){ //print upper border: 50 space width
printf("#");
}
printf("\n");
printf("# %09d%*c\n", ord_idx + 1, 39, '#');
printf("# ID pesanan : %s%*c\n", props->order_ID, 50 - (int)strlen(props->order_ID) - 18, '#');
printf("# Nama : %-20.20s%*c\n", props->name, 12, '#');
printf("# Telepon : %s%*c\n", props->phone_num, 50 - (int)strlen(props->phone_num) - 18, '#');
printf("# %*c\n", 48, '#');
printf("# Asal : %s%*c\n", station_list[props->origin_idx], 50 - (int)strlen(station_list[props->origin_idx]) - 18, '#');
printf("# Tujuan : %s%*c\n", station_list[props->destination_idx], 50 - (int)strlen(station_list[props->destination_idx]) - 18, '#');
printf("# Tanggal : %s, %02d-%02d-%04d%*c\n", props->day, props->date, props->month, props->year, 50-(int)strlen(props->day)-30, '#');
printf("# Waktu : %02d:%02d:%02d%*c\n", props->hour, props->minute, props->second, 24, '#');
printf("# %*c\n", 48, '#');
printf("# Kereta : [%s]%*c\n", tgarage[props->train_index]->train_name, 50 - (int)strlen(tgarage[props->train_index]->train_name) - 20, '#');
printf("# Seat : %-2d%-2c%-2d%*c\n", props->pcar + 1, props->pseaty + 'A', props->pseatx + 1, 26, '#');
for(int i = 0; i < 50; i++){ //print upper border: 50 space width
printf("#");
}
printf("\n\n");
printf("Harga tiket : Rp.%.2d\n\n", props->price);
printf("Masukkan 1 untuk lanjut: ");
pause_scr('1');
}
void newOrder(Order order_list_arr[], index order_arr_size, Train* train_garage[], int train_garage_size){
bool is_bypassed = FALSE; //implements ordering cancellation
//position in array
int posarr = orderListArr(order_list_arr, order_arr_size); //get empty position in array
if(posarr == -1){ //check if no element assignable
printf(ORDER_FULL);
return;
}
system(CLEAR_SCREEN);
printf("order #%09d\n\n", posarr + 1); //views order number (index)
for(int i = 0; i < train_garage_size; i++){ // generates random seat occupation on every order
seatAvlGenerator(train_garage[i]);
}
Order new_order_ctr;
printf("Pemesanan\n");
orderDateInput(&new_order_ctr, &is_bypassed); //call date setter func.
orderRouteInput(&new_order_ctr, &is_bypassed); //call route setter func.
trainSelector(&new_order_ctr, train_garage, train_garage_size, &is_bypassed); //call train setter func.
seatSelector(&new_order_ctr, train_garage, train_garage_size, &is_bypassed); //seat setter.
finalizeOrder(&new_order_ctr, train_garage, train_garage_size, &is_bypassed); //finalize
//Ticket view
ticketView(&new_order_ctr, posarr, train_garage, train_garage_size, &is_bypassed); //print ticket
//if cancelled, bypass order queueing
if(!is_bypassed){
order_list_arr[posarr] = new_order_ctr; //assign new order to provided position in array
}
}
void searchOrder(Order order_list_arr[], index size, Train* train_garage[], int train_garage_size){
system(CLEAR_SCREEN);
index i;
char ordID[20];
printf("Lihat Pesanan\n\n");
printf("Masukkan ID pesanan: ");
scanf("%s", ordID);
bool is_ID_unique = DRIFT; //to check if ID's appears once in list...
for(i = 0; i < size; i++){
if((strcmp(order_list_arr[i].order_ID, ordID) == 0)){
bool is_passable = FALSE;
ticketView(&order_list_arr[i], i, train_garage, train_garage_size, &is_passable);
is_ID_unique++;
}
}
if(is_ID_unique > 0){
printf("\nID IS NOT UNIQUE (%u) DUE TO LIMITATION IN SIMULATION MECHANICS.\n", is_ID_unique + 1);
}
if(i == size && is_ID_unique == DRIFT){
printf("ID NOT FOUND!\n INPUT X TO EXIT: ");
pause_scr('X');
}
}
void listOrders(Order order_list_arr[], index size){
system(CLEAR_SCREEN);
printf("DEBUG TOOLS\n");
printf("ORDER LIST:\n\n");
index idx = 0;
while(idx < size){
long lkey = order_list_arr[idx].is_occupied_L;
long rkey = order_list_arr[idx].is_occupied_R;
if(lkey != rkey){break;} //assert lkey == rkey
if(lkey <= 0 || rkey <= 0){break;} //assert lkey > 0 && rkey > 0
printf("===========================================================\n");
printf("order #%09d [%16s] <%-20.20s>.\n", idx + 1, order_list_arr[idx].order_ID, order_list_arr[idx].name);
printf("===========================================================\n\n");
idx++;
}
printf("TOTAL ORDERS: %d\n", idx);
printf("INPUT X TO CONTINUE: ");
pause_scr('X');
}