-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
3465 lines (2855 loc) · 102 KB
/
main.cpp
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _WIN32_WINNT 0x0500
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <conio.h>
#include <conio2.h>
#include <string>
#include <windows.h>
#include <ctime>
using namespace std;
// inisialisasi nama file database.
char* f_fasilitas = "fasilitas.dat";
char* f_tipekamar = "tipekamar.dat";
char* f_kamar = "kamar.dat";
char* f_makanan = "makanan.dat";
char* f_paketmakanan = "paketmakanan.dat";
char* f_pelanggan = "pelanggan.dat";
// define sleep
#define Delay Sleep
#define mydelay 0
#define LSDelay 0
// define view dan layout.
#define MAIN_FRAME_BGCOLOR LIGHTGRAY
#define MAIN_FRAME_X 36
#define MAIN_FRAME_Y 5
#define MAIN_FRAME_WIDTH 95
#define MAIN_FRAME_HEIGHT 35
#define MENU_FRAME_BGCOLOR RED
#define MENU_FRAME_X 1
#define MENU_FRAME_Y 5
#define MENU_FRAME_WIDTH 34
#define MENU_FRAME_HEIGHT 35
// define keyboard.
#define KEY_ENTER 13
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_RIGHT 77
#define KEY_LEFT 75
#define KEY_ESC 27
#define KEY_BSP 8
// define maksimum data.
#define MAX_FASILITAS 20
#define MAX_TIPEKAMAR 10
#define MAX_KAMAR 100
#define MAX_FASILITAS_TIPEKAMAR 10
#define MAX_PELANGGAN 500
#define MAX_PAKETMAKANAN 15
#define MAX_MAKANAN 15
#define MAX_KAMAR_DISEWA 50
#define MAX_PAKETMAKANAN_DIPESAN 50
// define tanda pemisah tanggal.
#define TGL_TANDA '-'
// define status terisi kamar.
#define KMR_TERISI "TERISI"
#define KMR_KOSONG "TERSEDIA"
// define error.
#define FILE_FAULT "[Gagal] Kesalahan file. "
#define FILE_RETRIEVED "[Selesai] "
// define message
#define DATA_ADDED "Data baru ditambahkan. "
#define DATA_CHANGED "Data terubah. "
// buffer yg digunakan untuk fungsi atoi
char buffer[100];
// STRUKTUR DATA LAYOUT ========================================================
struct Layout {
int w; // width
int h; // height
int x;
int y;
int edge;
int fg1;
int fg2;
int bg1;
int bg2;
};
// STRUKTUR DATA TANGGAL =======================================================
struct Tanggal {
int wd, dd, mm, yy;
char tanda;
};
// STRUKTUR DATA HOTEL. ========================================================
#define MAX_STRING 16
#define MAX_IDENTITAS 9
#define MAX_NMR_KAMAR 9
struct Fasilitas {
int id;
char nama_fasilitas[MAX_STRING];
} FASILITAS[MAX_FASILITAS];
int JML_FASILITAS = 0;
struct TipeKamar {
int id;
char nama_tipe[MAX_STRING];
int fasilitas[MAX_FASILITAS_TIPEKAMAR][2]; // [0]:id fasilitas, [1]:jumlah unit fasilitas.
int jml_fasilitas;
long harga;
float diskon;
int kelipatan_hari;
} TIPE_KAMAR[MAX_TIPEKAMAR];
int JML_TIPEKAMAR = 0;
struct Kamar {
int id;
char nomor_kamar[MAX_NMR_KAMAR];
int tipe_kamar;
bool ditempati;
} KAMAR[MAX_KAMAR];
int JML_KAMAR = 0;
struct Makanan {
int id;
char nama_makanan[MAX_STRING];
} MAKANAN[MAX_MAKANAN];
int JML_MAKANAN = 0;
struct PaketMakanan {
int id;
char nama_paket[MAX_STRING];
int makanan[MAX_MAKANAN][2];
int jml_makanan;
long harga;
} PAKET_MAKANAN[MAX_PAKETMAKANAN];
int JML_PKTMAKANAN = 0;
struct Pelanggan {
int id;
char nama[MAX_STRING];
char no_identitas[MAX_IDENTITAS];
char alamat[MAX_STRING];
int lama_inap;
Tanggal tgl_checkin;
Tanggal tgl_checkout;
int kamar[MAX_KAMAR_DISEWA];
int jml_kamar;
// pelanggan membeli 1 atau banyak paket makanan.
int paket_makanan[MAX_PAKETMAKANAN_DIPESAN][2];
int jml_pktmakanan;
} PELANGGAN[MAX_PELANGGAN];
int JML_PELANGGAN = 0;
// prototipe geje.
void SetWindow(int Width, int Height);
void coutxy(string s);
void coutxy(int x, int y, string s);
void logo();
// Protipe fungsi Tanggal.
Tanggal newtgl(int dd, int mm, int yy);
string weekdaytostr(int wd);
Tanggal curtgl();
string tgltostr(Tanggal t);
double tglDiff(Tanggal t1, Tanggal t2);
// VIEW AND LAYOUT =============================================================
void tampilHeader()
{
int x = 1, y = 1, w = 130, h = MAIN_FRAME_Y-1;
textbackground(LIGHTGRAY); textcolor(BLACK);
char c = ' ';
for (int i = 0; i < h; i++) {
gotoxy(x, i + y);
if (i >= h-2) {c = '_';}
if (i >= h-1) {textbackground(DARKGRAY);}
cout << setw(w) << setfill(c) << "";
}
cout << setw(0) << setfill(' ');
gotoxy(1, 1);
}
void tampilFooter()
{
int x = 1, y = MENU_FRAME_Y + MENU_FRAME_HEIGHT, w = 129, h = 1;
textbackground(LIGHTGRAY); textcolor(BLACK);
char c = ' ';
for (int i = 0; i < h; i++) {
gotoxy(x, i + y);
if (i >= h-2) {c = '_';}
if (i >= h-1) {textbackground(DARKGRAY);}
cout << setw(w) << setfill(c) << "";
}
cout << setw(0) << setfill(' ');
gotoxy(1, 1);
}
void clearMainFrame() {
int x = MAIN_FRAME_X, y = MAIN_FRAME_Y, h = MAIN_FRAME_HEIGHT, w = MAIN_FRAME_WIDTH;
textbackground(MAIN_FRAME_BGCOLOR); textcolor(MAIN_FRAME_BGCOLOR);
for (int i = 0; i < h; i++) {
gotoxy(x, i + y);
cout << setw(w) << setfill('_') << "";
}
cout << setw(0) << setfill(' ');
gotoxy(1, 1);
}
void clearMenuFrame() {
int x = MENU_FRAME_X, y = MENU_FRAME_Y, h = MENU_FRAME_HEIGHT, w = MENU_FRAME_WIDTH;
textbackground(MENU_FRAME_BGCOLOR); textcolor(MENU_FRAME_BGCOLOR);
for (int i = 0; i < h; i++) {
gotoxy(x, i + y);
cout << setw(w) << setfill('_') << "";
}
tampilHeader();
tampilFooter();
// tampil tanggal sekarang.
textbackground(BLACK); textcolor(YELLOW);
gotoxy(x,y+0); cout<<setw(MENU_FRAME_WIDTH)<<setfill(' ')<<left<<" "+tgltostr(curtgl());
gotoxy(x,y+1); cout<<setw(MENU_FRAME_WIDTH)<<setfill(' ')<<left<<"";
cout << setw(0) << setfill(' ');
gotoxy(1, 1);
}
void drawTableLayout(Layout l, string header) {
int x = l.x, y = l.y;
textbackground(l.edge); textcolor(l.fg2);
gotoxy(x, y); cout << setw(l.w) << setfill(' ') << "";
gotoxy(x, y); cout << header;
if (l.h == 0) {
textbackground(l.bg1); textcolor(l.bg1);
gotoxy(x, ++y); cout << setw(l.w) << setfill('_') << "";
}
for (int i = 0; i < l.h; i++) {
if (i % 2 == 0) {
textbackground(l.bg1);
} else {
textbackground(l.bg2);
}
textcolor(BLACK);
gotoxy(x, ++y); cout << setw(l.w-1) << setfill(' ') <<left<< "|" << "|";
}
textbackground(l.edge); textcolor(l.edge);
gotoxy(x, ++y); cout << setw(l.w) << setfill('_') << "";
gotoxy(1, 1);
}
void drawFormLayout(Layout l, string title) {
int x = l.x, y = l.y;
textbackground(l.edge); textcolor(l.fg2);
gotoxy(x, y); cout << setw(l.w) << setfill(' ') << "";
gotoxy(x, y); cout << "> " << title;
gotoxy(x, ++y); cout << setw(l.w) << setfill('=') << "";
textbackground(l.bg1); textcolor(l.fg1);
for (int i = 0; i < l.h; i++) {
gotoxy(x, ++y); cout << setw(l.w-1) << setfill(' ') <<left<< "|" << "|";
}
textbackground(l.edge); textcolor(l.fg2);
gotoxy(x, ++y); cout << setw(l.w) << setfill('=') << "";
gotoxy(1, 1);
}
void drawFormLayoutSimple(Layout l, string title) {
textbackground(l.edge);
textcolor(l.fg2);
int x = l.x, y = l.y;
gotoxy(x, y);
cout << setw(l.w) << setfill(' ') << "";
gotoxy(x, y);
cout << ("> " + title);
textbackground(l.bg1); textcolor(l.fg1);
if (l.h == 0){
gotoxy(x, ++y); cout <<setw(l.w)<<setfill(' ')<<"";
}
for (int i = 0; i < l.h; i++) {
gotoxy(x, ++y); cout << setw(l.w-1) << setfill(' ') <<left<< "|" << "|";
}
textbackground(l.edge); textcolor(l.fg2);
gotoxy(x, ++y); cout << setw(l.w) << setfill('=') << "";
gotoxy(1, 1);
}
void clearLine(int x, int y, int w, int color)
{
textbackground(color);
gotoxy(x, y); cout << setw(w) << setfill(' ') << "";
gotoxy(x, y);
}
void clearArea(int x, int y, int w, int h, int color) {
for (int i = 0; i < h; i++) {
clearLine(x, y + i, w, color);
}
}
void loadingSlash(string msg) {
cout << msg;
for (int i = 0; i < 3; i++) {
coutxy("|\b");
Delay(LSDelay);
cout << "/\b";
Delay(LSDelay);
cout << "-\b";
Delay(LSDelay);
cout << "\\\b";
Delay(LSDelay);
}
cout << "\b\n";
}
// OTHER FUNCTION ==============================================================
bool isNumber(char text[]) {
int len = strlen(text);
bool valid;
for (int i = 0; i < len; i++){
if (i == 0 && (text[0] == '-' || text[0] == '+')) continue;
if (i+1 < len && text[i] == '.' && isdigit(text[i+1])) continue;
if (!isdigit(text[i])) {
textcolor(RED);
cout << "Tidak valid. ";
return false;
}
}
return true;
}
bool isNumberPosZ(char text[]) {
if (!isNumber(text)) return false;
if (atol(text) < 0) {
textcolor(RED);
cout << "Harus >= 0. ";
return false;
}
return true;
}
bool isNumberPos(char text[]) {
if (!isNumber(text)) return false;
if (atol(text) <= 0) {
textcolor(RED);
cout << "Harus > 0. ";
return false;
}
return true;
}
bool isEmptyText(char text[]) {
int len = strlen(text);
for (int i = 0; i < len; i++)
if (text[i] != ' ') return false;
textcolor(RED); cout << "Tidak valid. ";
return true;
}
bool isOverDigit(char text[], int size)
{
if (strlen(text) < size) return false;
textcolor(RED); cout << "Over digit. ";
return true;
}
bool isValidForm(string menus[], int size) {
int score = 0;
for (int i = 0; i < size; i++) {
if (menus[i][0] == 'v') score++;
}
if (score != size) {
textcolor(RED);
cout << "Isi data dengan benar! Coba lagi. ";
return false;
}
return true;
}
bool isEqual(char str1[], char str2[])
{
return (strcmp(str1,str2)==0);
}
string numtostr(long num)
{
char buf[100];
return itoa(num,buf,10);
}
string strrev(string s) {
string result="";
for (int i=0; i<s.length( ) ; i++) {
result = s[ i ] + result;
}
return result;
}
string strfill(string s, int n)
{
string sfill;
int len = s.length();
for (int i=0;i<n;i++){
if (i <= len) sfill += s[i];
else sfill += " ";
}
return sfill;
}
string strlow(string s)
{
string news = "";
for (int i=0; i<s.length(); ++i)
news += tolower(s[i]);
return news;
}
string numtocurr(long num)
{
string s = strrev(numtostr(num));
string fulus = "Rp ";
int nseparator = (s.length()-1)/3;
for (int i=1;i<=nseparator;i++){
s.insert(4*i-1,".");
}
s = strrev(s);
for (int i=0;i<(11-s.length());i++){
fulus += " ";
}
s = fulus + s;
return s;
}
char pressanykey()
{
//cout<<"(tekan sembarang...)";
return getch();
}
char confirm(int x, int y, string pesan, int bgcol) {
string menus[] = {" YA ", "TIDAK"};
int peslen = pesan.length();
int totlen = peslen + 2*menus[0].length();
textbackground(bgcol); textcolor(BLACK);
coutxy(x, y, string(pesan));
int pilihan = 1;
int maxpilihan = 2;
while (true) {
for (int i = 0; i < 2; i++) {
textbackground(bgcol); textcolor(BLACK);
if (i + 1 == pilihan) {
textbackground(BLUE); textcolor(WHITE);
}
coutxy(x + peslen + i * 5, y, menus[i]);
}
char p = getch();
if (int(p) == KEY_LEFT) {
pilihan = (pilihan - 1 == 0) ? maxpilihan : pilihan - 1;
} else if (int(p) == KEY_RIGHT) {
pilihan = (pilihan + 1 > maxpilihan) ? 1 : pilihan + 1;
} else if (int(p) == KEY_ENTER) break;
}
clearLine(x, y, totlen, bgcol);
return (pilihan == 1 ? 'y' : 't');
}
// Fungsi Tanggal ===============================================================
Tanggal newtgl(int dd, int mm, int yy)
{
tm a = {0,0,0,dd,mm-1,yy-1900};
time_t mytime = mktime(&a);
tm *ltm = localtime(&mytime);
Tanggal t = {
ltm->tm_wday,
ltm->tm_mday,
ltm->tm_mon + 1,
ltm->tm_year + 1900,
TGL_TANDA};
return t;
}
string weekdaytostr(int wd)
{
string weekday[7] = {"Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu"};
return weekday[wd];
}
Tanggal curtgl()
{
time_t now = time(0);
tm *ltm = localtime(&now);
Tanggal t = {
ltm->tm_wday,
ltm->tm_mday,
ltm->tm_mon + 1,
ltm->tm_year + 1900,
TGL_TANDA};
return t;
}
string tgltostr(Tanggal t)
{
string wd = numtostr(t.wd);
string d = numtostr(t.dd);
string m = numtostr(t.mm);
string y = numtostr(t.yy);
char c[2] = {t.tanda,'\0'};
string tnd = string(c);
return (weekdaytostr(t.wd)+", "+d+tnd+m+tnd+y);
}
double tglDiff(Tanggal t1, Tanggal t2)
{
tm a = {0,0,0,t1.dd,t1.mm-1,t1.yy-1900};
time_t time_a = mktime(&a);
tm b = {0,0,0,t2.dd,t2.mm-1,t2.yy-1900};
time_t time_b = mktime(&b);
double diff = difftime(time_a,time_b) / (60.0 * 60.0 * 24.0);
//tm *local_time = localtime(&sec);
return diff;
}
Tanggal tglnext(Tanggal t, int days)
{
tm a = {0,0,0,t.dd,t.mm-1,t.yy-1900};
time_t time_a = mktime(&a);
time_a += (time_t)(days * 60.0 * 60.0 * 24.0);
tm *ltm = localtime(&time_a);
Tanggal newt = {
ltm->tm_wday,
ltm->tm_mday,
ltm->tm_mon + 1,
ltm->tm_year + 1900,
TGL_TANDA};
return newt;
}
int Menuselector_bg = WHITE;
int Menuselector_fg = BLACK;
int Menuselector_bghover = LIGHTBLUE;
int Menuselector_fghover = WHITE;
void setMenuSelectorColor(int bg, int fg, int bghover, int fghover)
{
Menuselector_bg = bg;
Menuselector_fg = fg;
Menuselector_bghover = bghover;
Menuselector_fghover = fghover;
}
void setMenuSelectorColor(int bg, int fg)
{
Menuselector_bg = bg;
Menuselector_fg = fg;
Menuselector_bghover = CYAN;
Menuselector_fghover = WHITE;
}
void menuSelector(string menus[], int *pilihan, int maxpilihan, int spacing, int maxshow)
{
if (maxpilihan == 0) return;
maxshow = maxshow>maxpilihan? maxpilihan : maxshow;
int x = wherex(), y = wherey();
int bg = Menuselector_bg, fg = Menuselector_fg, bghover = Menuselector_bghover, fghover = Menuselector_fghover;
int start = 0;
while (true) {
int row=0;
if (*pilihan == 1) start = 0;
else if (*pilihan == maxpilihan) start = maxpilihan-maxshow;
else if ( *pilihan > start+maxshow) start++;
else if ( *pilihan-1 < start ) start--;
for (int i = start; i < maxshow+start; i++) {
if (i + 1 == *pilihan) {textbackground(bghover); textcolor(fghover);}
else {textbackground(bg); textcolor(fg);
if (menus[i][0] == '<' || menus[i][0] == '>') textbackground(LIGHTGRAY);
}
for (int j = 0; j < spacing; j++) {
gotoxy(x, y + row * spacing + j);
cout << setw(menus[i].length()) << setfill(' ') << "";
}
coutxy(x, y + row*spacing, menus[i]);
row++;
}
gotoxy(1, 1);
char p = getch();
if (int(p) == KEY_UP) {
*pilihan = (*pilihan - 1 == 0) ? maxpilihan : *pilihan - 1;
while (menus[*pilihan - 1].length() == 0 || menus[*pilihan - 1][0] == '@')
*pilihan = (*pilihan - 1 == 0) ? maxpilihan : *pilihan - 1;
} else if (int(p) == KEY_DOWN) {
*pilihan = (*pilihan + 1 > maxpilihan) ? 1 : *pilihan + 1;
while (menus[*pilihan - 1].length() == 0 || menus[*pilihan - 1][0] == '@')
*pilihan = (*pilihan + 1 > maxpilihan) ? 1 : *pilihan + 1;
} else if (int(p) == 71){
*pilihan = 1;
} else if (int(p) == 79){
*pilihan = maxpilihan;
} else if (int(p) == KEY_ENTER) break;
}
// reset color.
textbackground(bg); textcolor(fg);
}
void menuSelector(string menus[], int *pilihan, int maxpilihan, int spacing)
{
menuSelector(menus,pilihan,maxpilihan,spacing,maxpilihan);
}
// @FASILITAS ===================================================================
bool addFasilitas(Fasilitas f);
bool loadFasilitasFromFile();
bool saveFasilitasToFile();
void tampilFasilitas();
Fasilitas newFasilitas()
{
int new_id = JML_FASILITAS == 0 ? 0 : FASILITAS[JML_FASILITAS - 1].id + 1; // last_id+1
Fasilitas f;
f.id = new_id;
strcpy(f.nama_fasilitas,"");
return f;
}
Fasilitas* getFasilitasByNama(string nama_fas) {
for (int i = 0; i < JML_FASILITAS; i++) {
if (FASILITAS[i].nama_fasilitas == nama_fas) return &FASILITAS[i];
}
return NULL;
}
Fasilitas* getFasilitasById(int id) {
for (int i = 0; i < JML_FASILITAS; i++) {
if (FASILITAS[i].id == id) return &FASILITAS[i];
}
return NULL;
}
bool addFasilitas(Fasilitas f) {
// jika array FASILITAS sudah penuh, maka penambahan digagalkan.
if (JML_FASILITAS == MAX_FASILITAS) {
textcolor(RED);
cout << "Gagal. Tabel penuh. ";
textcolor(WHITE);
return false;
}
// jika sudah ada fasilitas dg id yg sama, maka penambahan digagalkan.
for (int i = 0; i < JML_FASILITAS; i++) {
if (f.id == FASILITAS[i].id) {
textcolor(RED);
cout << "Gagal. Duplikasi data. zcx";
textcolor(WHITE);
return false;
}
}
FASILITAS[JML_FASILITAS] = f;
JML_FASILITAS++;
textcolor(GREEN); cout << DATA_ADDED;
textcolor(WHITE);
return true;
}
bool editFasilitas(Fasilitas *f, Fasilitas buf) {
*f = buf;
textcolor(GREEN); cout << DATA_CHANGED;
textcolor(WHITE);
return true;
}
bool deleteFasilitas(int id_fas) {
for (int i = 0; i < JML_TIPEKAMAR; i++) {
for (int j = 0; j < TIPE_KAMAR[i].jml_fasilitas; j++) {
if (TIPE_KAMAR[i].fasilitas[j][0] == id_fas) {
TipeKamar t = TIPE_KAMAR[i];
textcolor(RED);
cout << "Gagal. Data terpakai tipe kamar " << t.nama_tipe << ". ";
return false;
}
}
}
bool found = false;
Fasilitas *prev_fas;
for (int i = 0; i < JML_FASILITAS; i++) {
if (found) {
*prev_fas = FASILITAS[i];
prev_fas++;
} else if (FASILITAS[i].id == id_fas) {
found = true;
prev_fas = &FASILITAS[i];
}
}
if (found) {
cout << "Terhapus. ";
JML_FASILITAS--;
} else {
textcolor(RED);
cout << "Data tidak ditemukan. ";
}
return found;
}
bool loadFasilitasFromFile() {
// read from file.
ifstream file(f_fasilitas, ios::binary);
if (!file) {
cout << FILE_FAULT;
return false;
}
Fasilitas f;
file.read((char*) &f, sizeof (Fasilitas));
while (!file.eof()) {
Delay(mydelay);
addFasilitas(f);
cout <<" : "<< f.id << " " << f.nama_fasilitas << endl;
file.read((char*) &f, sizeof (Fasilitas));
}
file.close();
cout << FILE_RETRIEVED;
return true;
}
bool saveFasilitasToFile() {
// write to file.
ofstream file(f_fasilitas, ios::binary);
if (!file) {
textcolor(RED);
cout << "Gagal. Kesalahan file. ";
return false;
}
Fasilitas f;
for (int i = 0; i < JML_FASILITAS; i++) {
f = FASILITAS[i];
file.write((char*) &f, sizeof (Fasilitas));
}
file.close();
textcolor(GREEN);
cout << "Tersimpan. ";
return true;
}
void tampilFormInputFasilitas(int _x, int _y, Fasilitas *f)
{ // w h x y bg fg0 fg1 bgrow0 bgrow1
Layout l = {MAIN_FRAME_WIDTH, 3, _x, _y, RED, BLACK, WHITE, WHITE, LIGHTGRAY};
int x, y;
int headersize = 2;
int padx = 2;
int pady = headersize + 0;
bool editing = f==NULL ? false : true;
string menus[] = {
"o Nama Fasilitas :",
"<< Kembali "};
for (int i=0;editing && i<1;i++) menus[i][0] = 'v';
int pilihan = 1;
int maxpilihan = 2;
drawFormLayout(l,
editing ?
"FORMULIR PERUBAHAN DATA FASILITAS ID = " + numtostr((*f).id) + ", BERNAMA = " + (*f).nama_fasilitas:
"FORMULIR PENAMBAHAN DATA FASILITAS");
bool valid_pilih;
char simpan, kembali = 't';
do {
Fasilitas buf = editing? *f : newFasilitas();
x = padx + l.x;
y = pady + l.y;
clearArea(MAIN_FRAME_X, MAIN_FRAME_Y + 5, MAIN_FRAME_WIDTH, MAIN_FRAME_HEIGHT - (y + 5), MAIN_FRAME_BGCOLOR);
gotoxy(MAIN_FRAME_X, MAIN_FRAME_Y + 5); tampilFasilitas();
x+=22;
// tampilkan data yg sudah diinput di formulir.
textbackground(l.bg1); textcolor(l.fg1);
if(menus[0][0]!='x') coutxy(x,y,buf.nama_fasilitas);
gotoxy(x-22,y);
setMenuSelectorColor(l.bg1,l.fg1);
menuSelector(menus, &pilihan, maxpilihan, 1);
// input user.
int spc = l.w-(padx+22)-1;
clearLine(_x,y+2,l.w,l.bg1); // clear message.
textbackground(l.bg1); textcolor(l.fg1);
switch(pilihan){
case 1:
clearLine(x, y, spc, l.bg1);
gotoxy(x, y); cout << setw(sizeof(buf.nama_fasilitas)-1) << setfill(' ') << "" << "|";
gotoxy(x, y); cin.getline(buffer, sizeof(buffer)); fflush(stdin);
clearLine(x, y, spc, l.bg1);
//validasi.
if (isEmptyText(buffer) || isOverDigit(buffer, sizeof(buf.nama_fasilitas))) {
menus[0][0] = 'x';
continue;
}
// salin hasil input dari buffer ke rekord.
strcpy(buf.nama_fasilitas, buffer);
menus[0][0] = 'v';
// konfirmasi simpan.
simpan = confirm(x, y, "Simpan '"+string(buf.nama_fasilitas)+"' ? ", l.bg1);
if (simpan == 'y') {
gotoxy(x,y+2);
if (!editing && addFasilitas(buf)) {
saveFasilitasToFile();
} else if (editing && editFasilitas(f,buf)){
saveFasilitasToFile();
}
//getch();
}
break;
default: // batal?
kembali = 'y';
break;
}
} while (kembali == 't');
}
void tampilFormDeleteEditFasilitas(int _x, int _y)
{ // w h x y bg fg0 fg1 bgrow0 bgrow1
Layout l = {MAIN_FRAME_WIDTH, 3, _x, _y, RED, BLACK, WHITE, WHITE, LIGHTGRAY};
int x, y;
int headersize = 2;
int padx = 2;
int pady = headersize + 0;
string menus[] = {
"Ubah By ID :",
"Hapus By ID :",
"<< Kembali "};
int pilihan = 1;
int maxpilihan = 3;
clearMainFrame();
drawFormLayout(l, "Operasi Data Fasilitas. ");
// kriteria data yg akan diedit atau dihapus.
Fasilitas *f_edit;
int id_fas;
bool valid_pilih;
char hapus, kembali = 't';
do {
x = padx + l.x;
y = pady + l.y;
clearArea(MAIN_FRAME_X, MAIN_FRAME_Y + 5, MAIN_FRAME_WIDTH, MAIN_FRAME_HEIGHT - (y + 5), MAIN_FRAME_BGCOLOR);
gotoxy(MAIN_FRAME_X, MAIN_FRAME_Y + 5); tampilFasilitas();
gotoxy(x,y);
setMenuSelectorColor(l.bg1,l.fg1);
menuSelector(menus, &pilihan, maxpilihan, 1);
x+=22;
int spc = l.w-(padx+22)-1;
// input user.
textbackground(l.bg1); textcolor(l.fg1);
switch (pilihan) {
case 1:
clearLine(x, y, spc, l.bg1);
gotoxy(x, y); cin.getline(buffer, sizeof (buffer)); fflush(stdin);
clearLine(x, y, spc, l.bg1);
//validasi.
if (isEmptyText(buffer) || !isNumber(buffer)) {
continue;
}
id_fas = atoi(buffer);
f_edit = getFasilitasById(id_fas);
if (f_edit==NULL){
coutxy(x,y,"Data tidak ditemukan. ");
continue;
}
tampilFormInputFasilitas(_x,_y, f_edit);
clearMainFrame();
drawFormLayout(l, "Operasi Data Fasilitas. ");
break;
case 2:
clearLine(x, y+1, spc, l.bg1);
gotoxy(x, y+1); cin.getline(buffer, sizeof (buffer)); fflush(stdin);
clearLine(x, y+1, spc, l.bg1);
//validasi.
if (isEmptyText(buffer) || !isNumber(buffer)) {
continue;
}
id_fas = atoi(buffer);
// konfirmasi hapus.
hapus = confirm(x, y+1, "Hapus ID "+numtostr(id_fas)+" ? ", l.bg1);
if (hapus == 'y') {
if (deleteFasilitas(id_fas)) {
saveFasilitasToFile();
}
}
break;
case 3:
kembali = 'y';
break;
}
} while (kembali == 't');
}
void tampilFasilitas()
{
int _x = wherex(), _y = wherey();
Layout l = {MAIN_FRAME_WIDTH, 38-_y, _x, _y, RED, BLACK, WHITE, WHITE, LIGHTGRAY};
int x = l.x, y = l.y;