-
Notifications
You must be signed in to change notification settings - Fork 0
/
source code.C
771 lines (449 loc) · 11.8 KB
/
source code.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
/*
* employee application version 1
* using c programming [array,struct,functions,conditions]
* features new disply exit
* new:
* can add till 100 employee
* code must be unique
* salary must be >= 5k
* bonus must be >= 0
* reduction must be < (0.5*salary)
* display:
* when there is no employee yet will disply no employee meg
* press next and last employee is displayed will disply first employee
* press prev and first employee is displayed will disply last employee
* press home then press disply will disply last displayed employee
* exit: will display thanking message
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int currentDisplyEmpNo=0,reqEmpNo=0,empArrSize=0;
struct employee
{
int code;
char name[50];
float salary;
float reduction;
float bonus;
};
struct employee empArr[100];
void drawRect(int st_pos_x,int st_pos_y,int width,int height);
void drawText(int st_pos_x,int st_pos_y,int text_len,char text[],char clor[]);
void drawNoEmpMeg(void);
void drawFullArrMeg(void);
void drawExit(void);
void colorHomeButtons(int positionY);
int moveCurrentPositionY(int positionY);
void executeEnterKey(int positionY);
void runHomePage(void);
int isCodeUnique(int emp_num,int code);
int getUserCode(int emp_num);
float getUserSalary(void);
float getUserBonus(void);
float getUserReduction(float salary);
void addNewEmp(int emp_num);
void drawEmpInfo(int emp_num);
int moveCurrentPositionX(int positionX);
void colorDisplyButtons(int positionX);
void displyNextPrevEmp(int positionX);
void displayEmp(int emp_num);
void drawHomePage(void);
void drawHomeButton(void);
void drawNextButton(void);
void drawPrevButton(void);
void drawNewButton(void);
void drawDisplyButton(void);
void drawExitButton(void);
int main(void)
{
drawHomePage();
getch();
return 0;
}
void drawHomePage(void){
system("cls");
//start position x, start position y, width ,height
drawRect(20,3,40,17); // home page
drawRect(30,7,20,3); // new button
drawRect(30,11,20,3); // display button
drawRect(30,15,20,3); // exit button
drawText(30,4,20,"ITI System","GREEN");
drawText(30,7,20,"New","GREEN");
drawText(30,11,20,"Display","BLUE");
drawText(30,15,20,"Exit","BLUE");
gotoxy(40,8); // go to the middle of first rect (new button)
runHomePage();
}
void runHomePage(void)
{
int currentPosY=wherey();
char arrow;
while(1)
{
colorHomeButtons(currentPosY);
flushall();
arrow=getch();
if(arrow==NULL)
currentPosY=moveCurrentPositionY(currentPosY);
else if (arrow ==13)
{
executeEnterKey(currentPosY);
//when press enter mean another page will disply so break from home page
break;
}
}
}
void colorHomeButtons(int positionY)
{
int currentPosY=positionY;
if(currentPosY ==8) drawNewButton();
else if (currentPosY==12) drawDisplyButton();
else if (currentPosY==16) drawExitButton();
}
int moveCurrentPositionY(int positionY)
{
char arrow;
int currentPosY=positionY;
arrow=getch();
if(arrow==72) //go to up
{
if (currentPosY==8) currentPosY=16;
else currentPosY -=4;
}
else if(arrow==80) //go to down
{
if(currentPosY == 16) currentPosY=8;
else currentPosY +=4;
}
gotoxy(wherex(),currentPosY);
return currentPosY;
}
void executeEnterKey(int positionY)
{
int currentPosY=positionY;
if(currentPosY==8) //add new employee
{
if(empArrSize==100) drawFullArrMeg(); //no space to add new employee
else
{
empArrSize++;
addNewEmp(empArrSize-1);
}
}
else if(currentPosY==12) // display employee
{
if (empArrSize==0) //if there is no employee to disply
{
drawNoEmpMeg();
drawHomePage();
}
else
{
//to print always last displayed employee
reqEmpNo=currentDisplyEmpNo;
displayEmp(reqEmpNo);
}
}
else if(currentPosY==16) drawExit();
}
void addNewEmp(int emp_num)
{
char arrow;
struct employee e;
e.code=getUserCode(emp_num);
drawText(10,8,20,"Emplyee_Name: ","BLUE");
gotoxy(35,9);
flushall();
gets(e.name);
e.salary=getUserSalary();
e.bonus=getUserBonus();
e.reduction=getUserReduction(e.salary);
gotoxy(40,21);
drawRect(35,20,10,3);
drawText(35,20,10,"Save","GREEN");
while (1)
{
flushall();
arrow=getch();
if(arrow==13)
{
empArr[emp_num]=e;//press enter to save data
drawHomePage();
break;
}
}
}
int isCodeUnique(int emp_num,int code)
{
int i;
for (i=0;i<=emp_num;i++)//start from first employee to last one entered
{
if (code ==(empArr[i].code)) return 1;//duplicated code so refuse it
}
return 0;
}
int getUserCode(int emp_num)
{
int code;
while (1)
{
system("cls");
drawRect(5,3,70,21);
drawText(5,4,70,"ITI System","GREEN");
drawText(10,6,20,"Emplyee_ID: ","BLUE");
gotoxy(35,7);
scanf("%d",&code);
if(isCodeUnique(emp_num,code)==0&& code>0) return code; //this code is unique
//so accept it and break to enter the rest of info
drawText(15,8,50,"please enter uniqu code! To continue press enter"
,"RED");
getch();
}
}
float getUserSalary()
{
float salary;
while(1)
{
drawText(10,10,20,"Employee_Salary: ","BLUE");
gotoxy(35,11);
printf(" ");
gotoxy(35,11);
scanf("%f",&salary);
if(salary>=5000) return salary;
drawText(15,13,50,
"can't enter employee'salary less than 5k! To continue press enter"
,"RED");
getch();
gotoxy(6,14);
printf(" ");
}
}
float getUserBonus()
{
float bonus;
while(1)
{
drawText(10,12,20,"Employee_Bonus: ","BLUE");
gotoxy(35,13);
printf(" ");
gotoxy(35,13);
scanf("%f",&bonus);
if(bonus>=0) return bonus;
drawText(15,14,50,"can't enter negative bonus! To continue press enter"
,"RED");
getch();
gotoxy(6,15);
printf(" ");
}
}
float getUserReduction(float salary)
{
float reduction;
while(1)
{
drawText(10,15,20,"Emplyee_Reduction: ","BLUE");
gotoxy(35,16);
printf(" ");
gotoxy(35,16);
scanf("%f",&reduction);
if(reduction>0 && reduction<0.5*salary) return reduction;
drawText(30,16,20,"can't enter reduction negative!"
,"RED");
drawText(30,17,20,"or more than the half of employee salary!","RED");
drawText(30,18,20,"To continue press enter","RED");
getch();
gotoxy(6,17);
printf(" ");
gotoxy(6,18);
printf(" ");
gotoxy(6,19);
printf(" ");
}
}
void displayEmp(int emp_num)
{
int positionX;
char arrow;
currentDisplyEmpNo= emp_num;
drawEmpInfo(emp_num);
positionX=40; //go to home button
while(1)
{
colorDisplyButtons(positionX);
flushall();
arrow=getch();
if(arrow==NULL) positionX=moveCurrentPositionX(positionX);
else if (arrow ==13) //press enter
{
displyNextPrevEmp(positionX);
break;
}
}
}
void drawEmpInfo(int emp_num)
{
double netSalary;
netSalary=empArr[emp_num].salary+empArr[emp_num].bonus
-empArr[emp_num].reduction;
system("cls");
drawRect(10,4,60,18);
gotoxy(40-strlen("EmployeeNo ")/2,6);
textcolor(GREEN);
cprintf("EmployeeNo %d",emp_num+1);
drawText(10,8,25,"Emplyee_Name: ","BLUE");
gotoxy(40,9);
textcolor(GREEN);
cprintf("%s",empArr[emp_num].name);
drawText(10,10,25,"Emplyee_ID: ","BLUE");
gotoxy(40,11);
textcolor(GREEN);
cprintf("%d",empArr[emp_num].code);
drawText(10,12,25,"Employee_Net_Salary: ","BLUE");
gotoxy(40,13);
textcolor(GREEN);
cprintf("%f",netSalary);
}
int moveCurrentPositionX(int positionX)
{
char arrow;
arrow=getch();
if(arrow==77) //go to right
{
if(positionX !=60) positionX +=20;
else positionX =20;
}
else if(arrow == 75)//go to left
{
if (positionX !=20) positionX -=20;
else positionX =60;
}
return positionX;
}
void colorDisplyButtons(int positionX)
{
if (positionX == 20) drawPrevButton();
else if (positionX == 40) drawHomeButton();
else if (positionX == 60) drawNextButton();
}
void displyNextPrevEmp(int positionX)
{
if(positionX == 20) //press previou button
{
reqEmpNo=currentDisplyEmpNo-1; // get previous employee
if (reqEmpNo <0) reqEmpNo =empArrSize-1; //get last employee
displayEmp(reqEmpNo);
}
else if (positionX == 40) //press home button
drawHomePage();
else if (positionX == 60) //press next button
{
reqEmpNo=currentDisplyEmpNo +1;
if(reqEmpNo >= empArrSize) reqEmpNo=0; //get first employee
displayEmp(reqEmpNo);
}
}
void drawFullArrMeg(void)
{
int size;
system("cls");
drawRect(5,5,70,15);
drawText(5,6,70,"ITI System","GREEN");
drawText(5,8,70,"Array is full!","RED");
drawText(5,10,70,"please press any key to back to home","GREEN");
getch();
drawHomePage();
}
void drawNoEmpMeg()
{
char arrow;
system("cls");
drawRect(10,5,60,15);//screen app width=40 height=15
drawText(30,6,20,"ITI System","GREEN");
drawText(25,10,30,"There is no employee yet to disply!","RED");
drawText(25,12,30,"Please add new employees then you can disply them!","GREEN");
drawText(25,13,30,"press enter key to back to home","GREEN");
arrow=getch();
if (arrow==13) return;
}
void drawExit(void)
{
system("cls");
drawRect(20,5,40,15);
drawRect(30,9,20,9);
drawText(20,6,40,"ITI System","GREEN");
drawText(30,10,20,"Thank you! ","BLUE");
drawText(30,13,20," For your time ","BLUE");
}
void drawHomeButton(void)
{
drawRect(35,18,10,3);
drawText(35,18,10,"Home","GREEN");
drawRect(15,18,10,3);
drawText(15,18,10,"Prev","BLUE");
drawRect(55,18,10,3);
drawText(55,18,10,"Next","BLUE");
}
void drawNextButton(void)
{
drawRect(35,18,10,3);
drawText(35,18,10,"Home","BLUE");
drawRect(15,18,10,3);
drawText(15,18,10,"Prev","BLUE");
drawRect(55,18,10,3);
drawText(55,18,10,"Next","GREEN");
}
void drawPrevButton(void)
{
drawRect(35,18,10,3);
drawText(35,18,10,"Home","BLUE");
drawRect(15,18,10,3);
drawText(15,18,10,"Prev","GREEN");
drawRect(55,18,10,3);
drawText(55,18,10,"Next","BLUE");
}
void drawNewButton(void)
{
drawText(30,7,20,"New","GREEN");
drawText(30,11,20,"Display","BLUE");
drawText(30,15,20,"Exit","BLUE");
}
void drawDisplyButton(void)
{
drawText(30,7,20,"New","BLUE");
drawText(30,11,20,"Display","GREEN");
drawText(30,15,20,"Exit","BLUE");
}
void drawExitButton(void)
{
drawText(30,7,20,"New","BLUE");
drawText(30,11,20,"Display","BLUE");
drawText(30,15,20,"Exit","GREEN");
}
void drawRect(int st_pos_x,int st_pos_y,int width,int height)
{
int i=0,j=0,startX=st_pos_x;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
gotoxy(st_pos_x ++,st_pos_y);
if(i==0 || i==height-1) printf("* ");
else if(j==0||j==width-1) printf("* ");
}
st_pos_y++;
st_pos_x=startX;
}
}
void drawText(int st_pos_x,int st_pos_y,int text_len,char text[],char clor[])
{
int strLen=strlen(text);
int startPosX=(text_len-strLen)/2;
gotoxy(st_pos_x+startPosX,st_pos_y+1);
if(strcmp(clor,"BLUE")==0) textcolor(BLUE);
else if(strcmp(clor,"GREEN")==0) textcolor(GREEN);
else if (strcmp(clor,"RED")==0) textcolor(RED);
cprintf("%s",text);
}