Advertisement
edgymax

Maximuscode

Jun 3rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.73 KB | None | 0 0
  1. #include <stdio.h> //Standard Input Output Header
  2. #include <windows.h>
  3. #include <stdlib.h> //Standard Library Header
  4. #include <string.h> //String Header
  5. #include <conio.h>
  6. #include <ctype.h> //Include header to handle character for validation purpose
  7. #include <time.h> //Time header for server time option.
  8. /* ASCII Code Declaration */
  9. #define ENTER 13
  10. #define BKSP 8
  11. #define ESC 27
  12.  
  13. /* System Function */
  14. void testing();
  15. void gotoxy(int x, int y);
  16. int escexit(char pkey);
  17. int goback_previousmenu(char pkey);
  18. void cls();
  19. void invalid_msg();
  20. void error_msg();
  21.  
  22. /* Validation Function */
  23. int selection_limiter(int type, int upper_limit, int lower_limit);
  24. char *data_entry(int type, int size_limit);
  25. int alpha_str_validation(char input[]);
  26. int num_str_validation(char input[]);
  27.  
  28. /* Login Function */
  29. void usertype_file_verification();
  30. void usertype_username_retrieval();
  31. void usertype_password_retrieval();
  32. int login_validation();
  33.  
  34. /* Data Entry for Patient */
  35. int pat_numofline_get();
  36. int pat_id_get();
  37. void pat_firstname_get();
  38. void pat_lastname_get();
  39. void pat_name_merge();
  40. void pat_gender_get();
  41. void pat_age_get();
  42. void pat_ic_get();
  43. void pat_contact_get();
  44. int patient_add();
  45.  
  46. /* Data Entry for Appointment */
  47. int app_numofline_get();
  48. int app_id_get();
  49. void app_type_get();
  50. void app_doctor_get();
  51. void app_time_get();
  52. void date_merger(int date[], int month[], int year[]);
  53. int appointment_add();
  54.  
  55. /* Data Modification & Display for Patient */
  56. char *name_splitter(char input[]);
  57. void patient_info_retrieval(int type);
  58. void patient_info_print();
  59. int patient_view();
  60. int pat_line_locator(char input[]);
  61. int patient_modify();
  62. int patient_delete();
  63.  
  64. /* Data Modification & Display for Appointment*/
  65. void appointment_info_retrieval(int type);
  66. void appointment_info_print();
  67. int appointment_view();
  68.  
  69. /* Search Function */
  70. char *search_name();
  71. char *search_id();
  72. int patient_search();
  73. int appointment_search();
  74. int appointment_delete();
  75. /* Navigation Menu */
  76. void usertype_nav();
  77. int actiontype_nav();
  78. int pat_rec_action_nav();
  79. int app_rec_action_nav();
  80. int pat_doc_action_nav();
  81. int app_doc_action_nav();
  82.  
  83. /* Selection Menu */
  84. void usertype_selection();
  85. void actiontype_selection();
  86. int pat_rec_action_selection();
  87. int app_rec_action_selection();
  88. int pat_doc_action_selection();
  89. int app_doc_action_selection();
  90. void pat_search_selection();
  91.  
  92. /* Global Variable for Choice */
  93. char usertype, actiontype, action, search_choice;
  94.  
  95. FILE *fp, *fp2;
  96.  
  97. COORD coord={0,0};
  98.  
  99. /* Structure for Login User */
  100. struct user{
  101. char username[30];
  102. char password[30];
  103. }user;
  104.  
  105. /* Structure for Patient */
  106. struct patient{
  107. char id[10];
  108. char first_name[25];
  109. char last_name[25];
  110. char full_name[50];
  111. char gender;
  112. unsigned int age;
  113. char contact_no[11];
  114. char ic[12];
  115. }pat;
  116.  
  117. /* Structure for Appointment */
  118. struct appointment{
  119. char pat_id[10];
  120. char app_id[10];
  121. char name[50];
  122. char gender;
  123. char type;
  124. char doctor[50];
  125. char time[8];
  126. char date[10];
  127.  
  128. }app;
  129.  
  130. /* ------------------------------------------System Function------------------------------------------ */
  131.  
  132. void testing()
  133. {
  134. printf("lalalalala");
  135. }
  136.  
  137. int escexit(char pkey)
  138. {
  139. char reconfirm='Y';
  140.  
  141. if(pkey==ESC)
  142. {
  143. do{
  144. if(!(toupper(reconfirm)=='Y' || toupper(reconfirm)=='N'))
  145. invalid_msg();
  146.  
  147. printf("\a\n\nAre you sure you want to exit the program? (Y/N)\nAny unsaved data will be lost\n");
  148.  
  149. reconfirm = getch();
  150. }while(!(toupper(reconfirm)=='Y' || toupper(reconfirm)=='N'));
  151.  
  152. if(toupper(reconfirm)=='Y')
  153. {
  154. cls();
  155. printf("Thanks for using.\a");
  156. exit(0);
  157. }
  158. else if(toupper(reconfirm)=='N')
  159. {
  160. cls();
  161. return 1;
  162. }
  163. }
  164. }
  165.  
  166. int goback_previousmenu(char pkey)
  167. {
  168. int result;
  169.  
  170. if(pkey==BKSP)
  171. result = 1;
  172.  
  173. return result;
  174. }
  175.  
  176. void cls()
  177. {
  178. system("cls");
  179. }
  180.  
  181. void invalid_msg()
  182. {
  183. printf("\aInvalid Input\nPlease try again\n\n");
  184. }
  185.  
  186. void error_msg()
  187. {
  188.  
  189. cls();
  190. printf("\aError! Please contact Maximus Ooi!");
  191. exit(1);
  192. }
  193.  
  194. /* ------------------------------------------Validation Function------------------------------------------ */
  195.  
  196. int selection_limiter(int type, int upper_limit, int lower_limit) //Allow users to interact with the system by pressing respective key, BACKSPACE and ESC without carriage return.
  197. {
  198. int goback_check = 0;
  199. int exit_check = 0;
  200.  
  201. char input = '1';
  202.  
  203. do{
  204. input = getch();
  205.  
  206. exit_check = escexit(input); //Check whether input key is ESC
  207.  
  208. if(exit_check==1)
  209. return 0;
  210.  
  211. goback_check = goback_previousmenu(input); //Check whether input is BACKSPACE
  212.  
  213. if ((type==0 || type==3 || type==4) && goback_check==1) //Special outcome for BACKSPACE in type 0, 3, 4
  214. {
  215. printf("\a");
  216. break;
  217. }
  218.  
  219. }while(!(input>=lower_limit && input<=upper_limit));
  220.  
  221. if(type!=0) //Condition type, 0 for only BACKSPACE & ESC; 1 for user type; 2 for action type; 3 for action; 4 for search option
  222. {
  223. if(type==1)
  224. usertype = input;
  225. else if(type==2)
  226. actiontype = input;
  227. else if(type==3)
  228. action = input;
  229. else if(type==4)
  230. search_choice = input;
  231. else
  232. error_msg();
  233. }
  234.  
  235. return goback_check;
  236. }
  237.  
  238. char *data_entry(int type, int size_limit) //Limit user input with ASCII codes, type 1 for unmasked, type 2 for masked
  239. {
  240. char input[30], p=' ' ;
  241. int i=0;
  242.  
  243. while(i<=size_limit)
  244. {
  245. input[i] = getch();
  246. p = input[i];
  247.  
  248. if(i==size_limit)
  249. {
  250. if(p==ENTER)
  251. break;
  252. else if((p>=48 && p<=57)||(p>=65 && p<=90)||(p>=97 && p<=122))
  253. {
  254. i--;
  255. printf("\b \b");
  256. }
  257. }
  258.  
  259. if(p!=13 && p!='\b' && !((p>=48 && p<=57)||(p>=65 && p<=90)||(p>=97 && p<=122)))
  260. {
  261. p='\0';
  262. }
  263.  
  264. else if(p==ENTER)
  265. {
  266. p='\0';
  267. break;
  268. }
  269.  
  270. else if(p==BKSP)
  271. {
  272. i--;
  273. if(i!=-1)
  274. {
  275. p='\0' ;
  276. printf("\b \b");
  277. }
  278. else
  279. {
  280. p='\0' ;
  281. i++;
  282. }
  283. }
  284.  
  285. else
  286. {
  287. if(type==1)
  288. printf("%c", input[i]);
  289. else if(type==2)
  290. printf("*");
  291. else
  292. error_msg();
  293. i++ ;
  294. }
  295. }
  296. input[i] = '\0' ;
  297.  
  298. return input;
  299. }
  300.  
  301. int alpha_str_validation(char input[])
  302. {
  303. int validation, i;
  304.  
  305. for(i=0;i<(strlen(input));i++)
  306. {
  307. if(isalpha(input[i])||(isspace(input[i])))
  308. validation=1;
  309. else
  310. {
  311. validation=0;
  312. break;
  313. }
  314. }
  315.  
  316. return validation;
  317. }
  318.  
  319. int num_str_validation(char input[])
  320. {
  321. int validation, i;
  322.  
  323. for(i=0;i<(strlen(input));i++)
  324. {
  325. if(input[i]>=48 && input[i]<=57)
  326. validation=1;
  327. else
  328. {
  329. validation=0;
  330. break;
  331. }
  332. }
  333.  
  334. return validation;
  335. }
  336.  
  337. /* -----------------------------------------------Main---------------------------------------------------- */
  338.  
  339. int main()
  340. {
  341. int contcheck;
  342. system("COLOR 0B");
  343.  
  344. printf("Welcome to Maximus Hospital.\n");
  345. system("pause");
  346.  
  347.  
  348. do{
  349.  
  350. usertype_selection();
  351. contcheck=login_validation();
  352. }while(contcheck==1);
  353. usertype_nav();
  354. }
  355.  
  356. /* ------------------------------------------Login Function------------------------------------------ */
  357.  
  358. void usertype_file_verification()
  359. {
  360. switch(usertype)
  361. {
  362. case '1':
  363. fp = fopen("r_password.txt", "r+");
  364. break;
  365. case '2':
  366. fp = fopen("d_password.txt", "r+");
  367. break;
  368. case '3':
  369. fp = fopen("a_password.txt", "r+");
  370. break;
  371. }
  372. }
  373.  
  374. void usertype_username_retrieval()
  375. {
  376. if(fp!=NULL)
  377. {
  378. fscanf(fp, "%s %*s\n", user.username);
  379. }
  380. else
  381. error_msg();
  382. }
  383.  
  384. void usertype_password_retrieval()
  385. {
  386. if(fp!=NULL)
  387. {
  388. fscanf(fp, "%*s %s\n", user.password);
  389. }
  390. }
  391.  
  392. int login_validation()
  393. {
  394. char input_name[30], input_pass[30], action;
  395.  
  396. int login, goback_check;
  397.  
  398. do{
  399. usertype_file_verification();
  400.  
  401. int checker=0;
  402. login =0;
  403.  
  404. cls();
  405.  
  406. printf("Username: ");
  407. fflush(stdin);
  408. strcpy(input_name, data_entry(1, sizeof(input_name)));
  409. printf("\nPassword: ");
  410. fflush(stdin);
  411. strcpy(input_pass, data_entry(2, sizeof(input_name)));
  412.  
  413. while(!feof(fp))
  414. {
  415. usertype_username_retrieval();
  416. usertype_password_retrieval();
  417. if((strcmp(user.username, input_name)==0) && (strcmp(user.password, input_pass)==0))
  418. {
  419. checker=1;
  420. rewind(fp);
  421. break;
  422. }
  423. }
  424.  
  425. if(checker==1)
  426. {
  427. fp2 = fopen("Login Log.txt", "a+");
  428. printf("\a\n\nLogin Successful!\n");
  429. time_t mytime;
  430. mytime = time(NULL);
  431. printf(ctime(&mytime));
  432. fprintf(fp2, "%s\t\t%s", user.username, ctime(&mytime));
  433. fclose(fp2);
  434. login=1;
  435. }
  436. else
  437. printf("\a\n\nLogin Failed");
  438.  
  439.  
  440. printf("\n\nPress any key to continue...\nPress BACKSPACE to go back to previous menu\nPress ESC to exit\n");
  441. goback_check = selection_limiter(0,127,0);
  442.  
  443. if (goback_check==1)
  444. break;
  445.  
  446. }while(login==0);
  447.  
  448. fclose(fp);
  449.  
  450. return goback_check;
  451.  
  452. }
  453.  
  454. /* ------------------------------------------Data Entry for Patient------------------------------------------ */
  455.  
  456. int pat_numofline_get()
  457. {
  458. int i=1, checker=1;
  459. char ch;
  460.  
  461. do{
  462. fp2=fopen("patients.txt", "r");
  463.  
  464. if(fp2!=NULL)
  465. {
  466. while(!feof(fp2))
  467. {
  468. ch = fgetc(fp2);
  469. if (ch=='\n')
  470. i++;
  471. }
  472.  
  473. checker = 1;
  474. }
  475.  
  476. else
  477. {
  478. fp=fopen("patients.txt", "w");
  479. fclose(fp);
  480. checker==0;
  481. }
  482.  
  483. fclose(fp2);
  484. }while(checker==0);
  485.  
  486. return i;
  487. }
  488.  
  489. int pat_id_get()
  490. {
  491. int i = pat_numofline_get();
  492.  
  493. sprintf(pat.id, "PAT%04d", i);
  494.  
  495. printf("Patient ID\t: %s", pat.id );
  496.  
  497. return i;
  498. }
  499.  
  500. void pat_firstname_get()
  501. {
  502. int validation=0;
  503.  
  504. do{
  505. fflush(stdin);
  506. printf("\nFirst Name\t: ");
  507. scanf("%[^\n]s", pat.first_name);
  508. validation = alpha_str_validation(pat.first_name);
  509. }while(validation==0);
  510. }
  511.  
  512. void pat_lastname_get()
  513. {
  514. int validation=0;
  515.  
  516. do{
  517. fflush(stdin);
  518. printf("Last Name\t: ");
  519. scanf("%[^\n]s", pat.last_name);
  520. validation = alpha_str_validation(pat.last_name);
  521. }while(validation==0);
  522. }
  523.  
  524. void pat_name_merge() //Merge last name and first name together with delimiters
  525. {
  526. int i;
  527.  
  528. sprintf(pat.full_name, "%s$%s", pat.first_name, pat.last_name);
  529.  
  530. for(i=0;i<strlen(pat.full_name);i++)
  531. {
  532. if (pat.full_name[i]==' ')
  533. pat.full_name[i]=95;
  534. }
  535.  
  536. }
  537.  
  538. void pat_gender_get()
  539. {
  540. do{
  541. fflush(stdin);
  542. printf("Gender(M/F/O)\t: ");
  543. scanf("%c", &pat.gender);
  544. }while(!(toupper(pat.gender)=='M' || toupper(pat.gender)=='F' || toupper(pat.gender)=='O'));
  545. }
  546.  
  547. void pat_age_get()
  548. {
  549. int validation = 0;
  550.  
  551. do{
  552. fflush(stdin);
  553. printf("Age\t\t: ");
  554. scanf("%d", &pat.age);
  555. }while(!(isdigit(pat.age)==0) || pat.age==0);
  556. }
  557.  
  558. void pat_ic_get()
  559. {
  560. int validation = 0;
  561.  
  562. do{
  563. fflush(stdin);
  564. printf("IC No.\t\t: ");
  565. scanf("%s", pat.ic);
  566. validation = num_str_validation(pat.ic);
  567. }while(validation==0 || (strlen(pat.ic)!=12));
  568. }
  569.  
  570. void pat_contact_get()
  571. {
  572. int validation = 0;
  573.  
  574. do{
  575. fflush(stdin);
  576. printf("Contact No.\t: ");
  577. scanf(" %s", pat.contact_no);
  578. validation = num_str_validation(pat.contact_no);
  579. }while(validation==0 || !(strlen(pat.contact_no)>=10 && strlen(pat.contact_no)<=11));
  580. }
  581.  
  582. int patient_add()
  583. {
  584. int goback_check = 0, i;
  585.  
  586. do{
  587. cls();
  588.  
  589. printf("Add Patient\n\n");
  590.  
  591. i=pat_id_get();
  592. pat_firstname_get();
  593. pat_lastname_get();
  594. pat_gender_get();
  595. pat_age_get();
  596. pat_ic_get();
  597. pat_contact_get();
  598.  
  599. pat_name_merge();
  600.  
  601.  
  602. if(i==1)
  603. fp = fopen("patients.txt", "w");
  604. else
  605. fp = fopen("patients.txt", "a+");
  606.  
  607. fprintf(fp, "%s\t\t%s\t\t%c\t\t%d\t\t%s\t\t%s\n", pat.id, pat.full_name, pat.gender, pat.age, pat.ic, pat.contact_no);
  608. fclose(fp);
  609.  
  610. printf("\n\nPress BACKSPACE to select another action\nPress ESC to exit");
  611. goback_check=selection_limiter(0, 0, 0);
  612.  
  613. }while(goback_check==0);
  614.  
  615. return goback_check;
  616. }
  617.  
  618. /* ------------------------------------------Data Entry for Appointment------------------------------------------ */
  619.  
  620. int app_numofline_get()
  621. {
  622. int i=1, checker=1;
  623. char ch;
  624.  
  625. do{
  626. fp2=fopen("appointments.txt", "r");
  627.  
  628. if(fp2!=NULL)
  629. {
  630. while(!feof(fp2))
  631. {
  632. ch = fgetc(fp2);
  633. if (ch=='\n')
  634. i++;
  635. }
  636.  
  637. checker = 1;
  638. }
  639.  
  640. else
  641. {
  642. fp=fopen("appointments.txt", "w");
  643. fclose(fp);
  644. checker==0;
  645. }
  646.  
  647. fclose(fp2);
  648. }while(checker==0);
  649.  
  650. return i;
  651. }
  652.  
  653. int app_id_get()
  654. {
  655. int i = app_numofline_get();
  656.  
  657. sprintf(app.app_id, "APP%04d", i);
  658.  
  659. printf("\nAppointment ID\t: %s", app.app_id );
  660.  
  661. return i;
  662. }
  663.  
  664. void app_type_get()
  665. {
  666. do{
  667. fflush(stdin);
  668. printf("\nType(N/S/H)\t: ");
  669. scanf("%c",&app.type);
  670. }while(!(toupper(app.type)=='N' || toupper(app.type)=='S' || toupper(app.type)=='H'));
  671. }
  672.  
  673. void app_doctor_get()
  674. {
  675. int validation=0;
  676.  
  677. do{
  678. fflush(stdin);
  679. printf("Doctor\t\t: ");
  680. scanf("%[^\n]s", app.doctor);
  681. validation = alpha_str_validation(app.doctor);
  682. }while(validation==0);
  683. }
  684.  
  685. void app_time_get()
  686. {
  687. int validation = 0;
  688.  
  689. do{
  690. fflush(stdin);
  691. printf("Time\t\t: ");
  692. scanf("%s", app.time);
  693. validation = num_str_validation(app.time);
  694. }while(validation==0 || (strlen(app.time)!=4));
  695. }
  696.  
  697. void date_merger(int date, int month, int year)
  698. {
  699. sprintf(app.date, "%d-%d-%d", date, month, year);
  700. }
  701.  
  702. void app_date_get()
  703. {
  704. int date, month, year;
  705.  
  706. fflush(stdin);
  707. printf("Date\t\t: ");
  708. scanf("%d", &date);
  709. printf("Month\t\t: ");
  710. scanf("%d", &month);
  711. printf("Year\t\t: ");
  712. scanf("%d", &year);
  713.  
  714. date_merger(date, month, year);
  715. }
  716.  
  717. int appointment_add()
  718. {
  719. int goback_check = 0, i, validation=0, date, month, year;
  720. char input_id[10], buffer[50];
  721. do{
  722. cls();
  723. fp=fopen("patients.txt", "r");
  724. if(fp!=NULL)
  725. {
  726. do
  727. {
  728. printf("Add Appointment\n\n");
  729. printf("Patient ID\t: ");
  730. fflush(stdin);
  731. strcpy(input_id, data_entry(1, 10));
  732.  
  733. rewind(fp);
  734.  
  735. while(!(feof(fp)))
  736. {
  737. patient_info_retrieval(0);
  738. if(strcmp(pat.id,input_id)==0)
  739. {
  740. validation=1;
  741. break;
  742. }
  743. }
  744. }while(validation==0);
  745. }
  746. else
  747. error_msg();
  748.  
  749. fclose(fp);
  750.  
  751. strcpy(app.pat_id,pat.id);
  752. strcpy(app.name, pat.full_name);
  753. app.gender=toupper(pat.gender);
  754.  
  755. strcpy(buffer, name_splitter(app.name));
  756.  
  757. printf("\n\nPatient ID\t: %s\nPatient Name\t: %s\nGender\t\t: %c\n", app.pat_id, buffer, app.gender);
  758.  
  759. printf("\nAppointment Detail");
  760. i=app_id_get();
  761. app_type_get();
  762. app_doctor_get();
  763. app_date_get();
  764. app_time_get();
  765.  
  766. date_merger(date, month, year);
  767.  
  768. if(i==0)
  769. fp = fopen("appointments.txt", "w");
  770. else
  771. fp = fopen("appointments.txt", "a+");
  772.  
  773.  
  774. fprintf(fp, "%s\t\t%s\t\t%s\t\t%c\t\t%c\t\t%s\t\t%s\t\t%s\n", app.app_id, app.pat_id, app.name, app.gender, app.type, app.doctor, app.time, app.date);
  775. fclose(fp);
  776.  
  777. printf("\n\nPress BACKSPACE to select another action\nPress ESC to exit");
  778. goback_check=selection_limiter(0, 0, 0);
  779.  
  780. }while(goback_check==0);
  781.  
  782. return goback_check;
  783. }
  784.  
  785. int appointment_search()
  786. {
  787. int goback_check;
  788. char keyword[10];
  789.  
  790. cls();
  791.  
  792. printf("Search Appointment\n\n");
  793. strcpy(keyword, search_id());
  794.  
  795. fp=fopen("appointments.txt", "r");
  796. if(fp!=NULL)
  797. {
  798. while(!feof(fp))
  799. {
  800. appointment_info_retrieval(1);
  801. if(strcmp(app.app_id, keyword)==0)
  802. {
  803. appointment_info_print();
  804. break;
  805. }
  806. }
  807. fclose(fp);
  808. }
  809. else
  810. error_msg();
  811.  
  812.  
  813. do{
  814. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  815. goback_check = selection_limiter(0, 0 , 0);
  816. }while(goback_check==0);
  817.  
  818. return goback_check;
  819. }
  820.  
  821.  
  822. /* ------------------------------------------Data Modification & Display for Patient------------------------------------------ */
  823.  
  824. char *name_splitter(char input[]) //Remove delimiters in name
  825. {
  826. int i;
  827. char buffer[50];
  828.  
  829. strcpy(buffer,input);
  830. for(i=0;i<strlen(buffer);i++)
  831. {
  832. if(buffer[i]==95 || buffer[i]==36)
  833. buffer[i]=' ';
  834. }
  835.  
  836. return buffer;
  837. }
  838.  
  839. void patient_info_retrieval(int type) //type determine whether delimiters are added or not
  840. {
  841. char buffer[10];
  842.  
  843. fscanf(fp, "%s %s %c %d %s %s\n", pat.id, pat.full_name, &pat.gender, &pat.age, pat.ic, pat.contact_no);
  844. if(type!=0)
  845. strcpy(pat.full_name, name_splitter(pat.full_name));
  846. }
  847.  
  848. void patient_info_print()
  849. {
  850. printf("\n\nPatient ID\t: %s\nName\t\t: %s\nGender\t\t: %c\nAge\t\t: %d\nIC No.\t\t: %s\nContact No.\t: %s\n\n", pat.id, pat.full_name, toupper(pat.gender), pat.age, pat.ic, pat.contact_no);
  851. }
  852.  
  853. int patient_view()
  854. {
  855. int goback_check;
  856.  
  857. cls();
  858.  
  859. printf("View Patients\n");
  860.  
  861. fp=fopen("patients.txt", "r");
  862. if(fp!=NULL)
  863. {
  864. while(!feof(fp))
  865. {
  866. patient_info_retrieval(1);
  867. patient_info_print();
  868. }
  869.  
  870. fclose(fp);
  871.  
  872. do{
  873. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  874. goback_check = selection_limiter(0, 0 , 0);
  875. }while(goback_check==0);
  876. }
  877. else
  878. error_msg();
  879.  
  880. return goback_check;
  881.  
  882. }
  883.  
  884. int pat_line_locator(char input[])
  885. {
  886. int i, validation=0;
  887.  
  888. rewind(fp);
  889.  
  890. for(i=0;(!feof(fp));i++)
  891. {
  892. fscanf(fp, "%s %*s %*c %*d %*s %*s\n", pat.id);
  893. if(strcmp(pat.id, input)==0)
  894. {
  895. validation=1;
  896. break;
  897. }
  898. }
  899. if(validation==1)
  900. return i;
  901. else
  902. return -1;
  903. }
  904. int patient_modify()
  905. {
  906. char modify_target[10];
  907. int target_line, goback_check;
  908.  
  909. FILE *fp;
  910. fp = fopen("patients.txt", "r");
  911.  
  912. do
  913. {
  914. cls();
  915. printf("Modify Patient\n\n");
  916. fflush(stdin);
  917. strcpy(modify_target, search_id());
  918. target_line = pat_line_locator(modify_target);
  919. }while(target_line<0);
  920.  
  921. if(fp!=NULL)
  922. {
  923. char confirm;
  924.  
  925. rewind(fp);
  926.  
  927. if(target_line!=-1)
  928. {
  929. printf("\nConfirm Edit? (Y/N)");
  930. confirm = getchar();
  931.  
  932. if(toupper(confirm)=='Y')
  933. {
  934. char words[1024];
  935. char *buffer;
  936. char *ptr;
  937. int line_counter = 0;
  938. int checker = 0;
  939.  
  940. buffer=(char *)malloc(1024%sizeof(char));
  941. memset(buffer,0, 1024*sizeof(char));
  942. ptr=buffer;
  943.  
  944.  
  945. while(fgets(words, 1024, fp) != NULL)
  946. {
  947. line_counter++;
  948. if(line_counter != target_line)
  949. {
  950. strcpy(ptr, words);
  951. ptr +=strlen(words);
  952. }
  953. else if(line_counter == target_line)
  954. {
  955. char new_line[1024], buffer2[20];
  956.  
  957. cls();
  958. fflush(stdin);
  959.  
  960. printf("Modify Patient\n");
  961. fscanf(fp, "%s" , pat.id);
  962. pat_firstname_get();
  963. pat_lastname_get();
  964. pat_gender_get();
  965. pat_age_get();
  966. pat_ic_get();
  967. pat_contact_get();
  968.  
  969. pat_name_merge();
  970.  
  971.  
  972. sprintf(buffer2,"%c\t\t%d", pat.gender, pat.age);
  973.  
  974. strcpy(new_line,pat.id);
  975. strcat(new_line,"\t\t");
  976. strcat(new_line,pat.full_name);
  977. strcat(new_line,"\t\t");
  978. strcat(new_line,buffer2);
  979. strcat(new_line,"\t\t");
  980. strcat(new_line,pat.ic);
  981. strcat(new_line,"\t\t");
  982. strcat(new_line,pat.contact_no);
  983. strcat(new_line,"\n");
  984.  
  985. strcpy(ptr, new_line);
  986. ptr +=strlen(new_line);
  987. }
  988.  
  989. }
  990. fclose(fp);
  991.  
  992. fp=fopen("patients.txt","w");
  993. fprintf(fp, "%s", buffer);
  994. fclose(fp);
  995.  
  996. printf("\n\n\aModify is completed.");
  997. }
  998. }
  999. }
  1000. else
  1001. error_msg();
  1002. do{
  1003. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  1004. goback_check = selection_limiter(0, 0 , 0);
  1005. }while(goback_check==0);
  1006. }
  1007.  
  1008. int patient_delete()
  1009. {
  1010. char modify_target[10];
  1011. int target_line, goback_check;
  1012.  
  1013. FILE *fp;
  1014. fp = fopen("patients.txt", "r");
  1015.  
  1016. do
  1017. {
  1018. cls();
  1019. printf("Delete Patient\n\n");
  1020. fflush(stdin);
  1021. strcpy(modify_target, search_id());
  1022. target_line = pat_line_locator(modify_target);
  1023. }while(target_line<0);
  1024.  
  1025. if(fp!=NULL)
  1026. {
  1027. char confirm;
  1028.  
  1029. rewind(fp);
  1030.  
  1031. if(target_line!=-1)
  1032. {
  1033. printf("\nConfirm Delete? (Y/N)");
  1034. confirm = getchar();
  1035.  
  1036. if(toupper(confirm)=='Y')
  1037. {
  1038. char words[1024];
  1039. char *buffer;
  1040. char *ptr;
  1041. int line_counter = 0;
  1042. int checker = 0;
  1043.  
  1044. buffer=(char *)malloc(1024%sizeof(char));
  1045. memset(buffer,0, 1024*sizeof(char));
  1046. ptr=buffer;
  1047.  
  1048. while(fgets(words, 1024, fp) != NULL)
  1049. {
  1050.  
  1051. if(line_counter != (target_line))
  1052. {
  1053. strcpy(ptr, words);
  1054. ptr +=strlen(words);
  1055. }
  1056. line_counter ++;
  1057. }
  1058. fclose(fp);
  1059.  
  1060. fp=fopen("patients.txt","w");
  1061. fprintf(fp, "%s", buffer);
  1062. fclose(fp);
  1063.  
  1064. printf("\n\nDelete completed!\a\n");
  1065. }
  1066. }
  1067. }
  1068. else
  1069. error_msg();
  1070.  
  1071. do{
  1072. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  1073. goback_check = selection_limiter(0, 0 , 0);
  1074. }while(goback_check==0);
  1075. }
  1076. /* ------------------------------------------------Data Modification & Display for Appointment---------------------------*/
  1077.  
  1078. void appointment_info_retrieval(int type)
  1079. {
  1080. fscanf(fp, "%s %s %s %c %c %s %s %s\n", app.app_id, app.pat_id, app.name, &app.gender, &app.type, app.doctor, app.time, app.date);
  1081. if(type!=0)
  1082. strcpy(app.name, name_splitter(app.name));
  1083. }
  1084.  
  1085. void appointment_info_print()
  1086. {
  1087. printf("\n\nAppointment ID\t: %s\nPatient ID\t: %s\nName\t\t: %s\nGender\t\t: %c\nType\t\t: %c\nDoctor\t\t: %s\nDate\t\t: %s\nTime(24hrs)\t: %s\n", app.app_id, app.pat_id, app.name, toupper(app.gender), toupper(app.type), app.doctor, app.time, app.date);
  1088. }
  1089.  
  1090. int appointment_view()
  1091. {
  1092. int goback_check;
  1093.  
  1094. cls();
  1095.  
  1096. printf("View Appointments\n");
  1097.  
  1098. fp=fopen("appointments.txt", "r");
  1099. if(fp!=NULL)
  1100. {
  1101. while(!feof(fp))
  1102. {
  1103. fflush(stdin);
  1104. appointment_info_retrieval(1);
  1105. fflush(stdin);
  1106. appointment_info_print();
  1107. }
  1108.  
  1109. fclose(fp);
  1110.  
  1111. do{
  1112. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  1113. goback_check = selection_limiter(0, 0 , 0);
  1114. }while(goback_check==0);
  1115. }
  1116. else
  1117. error_msg();
  1118.  
  1119. return goback_check;
  1120. }
  1121.  
  1122. int app_line_locator(char input[])
  1123. {
  1124. int i, validation=0;
  1125.  
  1126. rewind(fp);
  1127.  
  1128. for(i=0;(!feof(fp));i++)
  1129. {
  1130. fscanf(fp, "%s %*s %*s %*c %*c %*s %*s %*s\n", app.app_id);
  1131. if(strcmp(app.app_id, input)==0)
  1132. {
  1133. validation=1;
  1134. break;
  1135. }
  1136. }
  1137. if(validation==1)
  1138. return i;
  1139. else
  1140. return -1;
  1141. }
  1142.  
  1143. int appointment_modify()
  1144. {
  1145. char modify_target[10];
  1146. int target_line, goback_check;
  1147.  
  1148. FILE *fp;
  1149. fp = fopen("appointments.txt", "r");
  1150.  
  1151. do
  1152. {
  1153. cls();
  1154. printf("Modify Appointment\n\n");
  1155. fflush(stdin);
  1156. strcpy(modify_target, search_id());
  1157. target_line = app_line_locator(modify_target);
  1158. }while(target_line<0);
  1159.  
  1160. if(fp!=NULL)
  1161. {
  1162. char confirm;
  1163.  
  1164. rewind(fp);
  1165.  
  1166. if(target_line!=-1)
  1167. {
  1168. printf("\nConfirm Edit? (Y/N)");
  1169. confirm = getchar();
  1170.  
  1171. if(toupper(confirm)=='Y')
  1172. {
  1173. char words[1024];
  1174. char *buffer;
  1175. char *ptr;
  1176. int line_counter = 0;
  1177. int checker = 0;
  1178.  
  1179. buffer=(char *)malloc(1024%sizeof(char));
  1180. memset(buffer,0, 1024*sizeof(char));
  1181. ptr=buffer;
  1182.  
  1183.  
  1184. while(fgets(words, 1024, fp) != NULL)
  1185. {
  1186. line_counter++;
  1187. if(line_counter != target_line)
  1188. {
  1189. strcpy(ptr, words);
  1190. ptr +=strlen(words);
  1191. }
  1192. else if(line_counter == target_line)
  1193. {
  1194. char new_line[1024], buffer2[20];
  1195.  
  1196. cls();
  1197. fflush(stdin);
  1198.  
  1199. printf("Modify Appointment\n");
  1200. fscanf(fp, "%s %s %s %c", app.app_id, app.pat_id, app.name, &app.gender);
  1201. app_type_get();
  1202. app_doctor_get();
  1203. app_date_get();
  1204. app_time_get();
  1205.  
  1206. sprintf(buffer2,"%c\t\t%d", app.gender, app.type);
  1207.  
  1208. strcpy(new_line,app.app_id);
  1209. strcat(new_line,"\t\t");
  1210. strcat(new_line,app.pat_id);
  1211. strcat(new_line,"\t\t");
  1212. strcat(new_line,buffer2);
  1213. strcat(new_line,"\t\t");
  1214. strcat(new_line,app.doctor);
  1215. strcat(new_line,"\t\t");
  1216. strcat(new_line,app.date);
  1217. strcat(new_line,"\t\t");
  1218. strcat(new_line,app.time);
  1219. strcat(new_line,"\n");
  1220.  
  1221. strcpy(ptr, new_line);
  1222. ptr +=strlen(new_line);
  1223. }
  1224.  
  1225. }
  1226. fclose(fp);
  1227.  
  1228. fp=fopen("appointments.txt","w");
  1229. fprintf(fp, "%s", buffer);
  1230. fclose(fp);
  1231.  
  1232. printf("\n\n\aModify is completed.");
  1233. }
  1234. }
  1235. }
  1236. else
  1237. error_msg();
  1238. do{
  1239. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  1240. goback_check = selection_limiter(0, 0 , 0);
  1241. }while(goback_check==0);
  1242. }
  1243.  
  1244. int appointment_delete()
  1245. {
  1246. char modify_target[10];
  1247. int target_line, goback_check;
  1248.  
  1249. FILE *fp;
  1250. fp = fopen("appointments.txt", "r");
  1251.  
  1252. do
  1253. {
  1254. cls();
  1255. printf("Delete Patient\n\n");
  1256. fflush(stdin);
  1257. strcpy(modify_target, search_id());
  1258. target_line = app_line_locator(modify_target);
  1259. }while(target_line<0);
  1260.  
  1261. if(fp!=NULL)
  1262. {
  1263. char confirm;
  1264.  
  1265. rewind(fp);
  1266.  
  1267. if(target_line!=-1)
  1268. {
  1269. printf("\nConfirm Delete? (Y/N)");
  1270. confirm = getchar();
  1271.  
  1272. if(toupper(confirm)=='Y')
  1273. {
  1274. char words[1024];
  1275. char *buffer;
  1276. char *ptr;
  1277. int line_counter = 0;
  1278. int checker = 0;
  1279.  
  1280. buffer=(char *)malloc(1024%sizeof(char));
  1281. memset(buffer,0, 1024*sizeof(char));
  1282. ptr=buffer;
  1283.  
  1284. while(fgets(words, 1024, fp) != NULL)
  1285. {
  1286.  
  1287. if(line_counter != (target_line))
  1288. {
  1289. strcpy(ptr, words);
  1290. ptr +=strlen(words);
  1291. }
  1292. line_counter ++;
  1293. }
  1294. fclose(fp);
  1295.  
  1296. fp=fopen("appointments.txt","w");
  1297. fprintf(fp, "%s", buffer);
  1298. fclose(fp);
  1299.  
  1300. printf("\n\nDelete completed!\a\n");
  1301. }
  1302. }
  1303. }
  1304. else
  1305. error_msg();
  1306.  
  1307. do{
  1308. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  1309. goback_check = selection_limiter(0, 0 , 0);
  1310. }while(goback_check==0);
  1311. }
  1312. /* ------------------------------------------Search Function------------------------------------------------ */
  1313.  
  1314. char *search_name()
  1315. {
  1316. int validation=0;
  1317. char input[30];
  1318.  
  1319. do{
  1320. fflush(stdin);
  1321. printf("Search Name\t: ");
  1322. scanf("%[^\n]s", input);
  1323. validation = alpha_str_validation(input);
  1324. }while(validation==0);
  1325.  
  1326. return input;
  1327. }
  1328.  
  1329. char *search_id()
  1330. {
  1331. char input[10];
  1332.  
  1333. fflush(stdin);
  1334. printf("Search ID\t: ");
  1335. strcpy(input,data_entry(1,10));
  1336.  
  1337. return input;
  1338. }
  1339.  
  1340. int patient_search()
  1341. {
  1342. int goback_check;
  1343. char keyword[30];
  1344.  
  1345. pat_search_selection();
  1346.  
  1347. if(search_choice=='0')
  1348. return 1;
  1349.  
  1350. cls();
  1351.  
  1352. printf("Search Patient\n\n");
  1353. switch(search_choice)
  1354. {
  1355. case '1':
  1356. strcpy(keyword, search_name());
  1357. break;
  1358. case '2':
  1359. strcpy(keyword, search_id());
  1360. break;
  1361. default:
  1362. error_msg();
  1363. }
  1364.  
  1365. fp=fopen("patients.txt", "r");
  1366. if(fp!=NULL)
  1367. {
  1368. while(!feof(fp))
  1369. {
  1370. patient_info_retrieval(1);
  1371. if(strstr(pat.full_name, keyword)!=NULL)
  1372. patient_info_print();
  1373. if(strcmp(pat.id, keyword)==0)
  1374. {
  1375. patient_info_print();
  1376. break;
  1377. }
  1378. }
  1379. fclose(fp);
  1380. }
  1381.  
  1382. else
  1383. error_msg();
  1384.  
  1385.  
  1386. do{
  1387. printf("\nPress BACKSPACE to go to previous menu\nPress ESC to exit.");
  1388. goback_check = selection_limiter(0, 0 , 0);
  1389. }while(goback_check==0);
  1390.  
  1391. return goback_check;
  1392. }
  1393.  
  1394. /* ------------------------------------------Navigation Menu------------------------------------------------ */
  1395.  
  1396. void usertype_nav()
  1397. {
  1398. int contcheck=0;
  1399.  
  1400. do
  1401. {
  1402. cls();
  1403.  
  1404. switch(usertype)
  1405. {
  1406. case '1':
  1407.  
  1408. actiontype_selection();
  1409. contcheck = actiontype_nav(); //based on action type, call menu
  1410. break;
  1411. case '2':
  1412. actiontype_selection();
  1413. contcheck = actiontype_nav();
  1414. break;
  1415. case '3':
  1416. testing();
  1417. break;
  1418. }
  1419. }while(contcheck==1);
  1420. }
  1421.  
  1422. int actiontype_nav()
  1423. {
  1424. int contcheck = 0;
  1425.  
  1426. do{
  1427. cls();
  1428.  
  1429. switch(actiontype)
  1430. {
  1431. case '1':
  1432. if(usertype=='1')
  1433. {
  1434. contcheck = pat_rec_action_selection();
  1435. if(contcheck==1)
  1436. return contcheck;
  1437. else
  1438. {
  1439. contcheck = pat_rec_action_nav();
  1440. break;
  1441. }
  1442. }
  1443. else if(usertype=='2')
  1444. {
  1445. contcheck = pat_doc_action_selection();
  1446. if(contcheck==1)
  1447. return contcheck;
  1448. else
  1449. contcheck = pat_doc_action_nav();
  1450. break;
  1451. }
  1452. case '2':
  1453. if(usertype=='1')
  1454. {
  1455. contcheck = app_rec_action_selection();
  1456. if(contcheck==1)
  1457. return contcheck;
  1458. else
  1459. {
  1460. contcheck = app_rec_action_nav();
  1461. break;
  1462. }
  1463. }
  1464. else if(usertype=='2')
  1465. {
  1466. contcheck = app_doc_action_selection();
  1467. if(contcheck==1)
  1468. return contcheck;
  1469. else
  1470. {
  1471. contcheck = app_doc_action_nav();
  1472. break;
  1473. }
  1474. }
  1475. }
  1476. }while(contcheck==1);
  1477. }
  1478.  
  1479. int pat_rec_action_nav()
  1480. {
  1481. int contcheck = 0;
  1482.  
  1483. cls();
  1484.  
  1485. switch(action)
  1486. {
  1487. case '1':
  1488. contcheck = patient_add();
  1489. if(contcheck==1)
  1490. return contcheck;
  1491. case '2':
  1492. contcheck = patient_view();
  1493. if(contcheck==1)
  1494. return contcheck;
  1495. break;
  1496. case '3':
  1497. contcheck = patient_search();
  1498. if(contcheck==1)
  1499. return contcheck;
  1500. break;
  1501. case '4':
  1502. contcheck = patient_modify();
  1503. if(contcheck==1)
  1504. return contcheck;
  1505. break;
  1506. case '5':
  1507. contcheck = patient_delete();
  1508. if(contcheck==1)
  1509. return contcheck;
  1510. break;
  1511. default:
  1512. error_msg();
  1513. }
  1514. }
  1515.  
  1516. int app_rec_action_nav()
  1517. {
  1518. int contcheck = 0;
  1519.  
  1520. cls();
  1521. switch(action)
  1522. {
  1523. case '1':
  1524. contcheck = appointment_add();
  1525. if(contcheck==1)
  1526. return contcheck;
  1527. case '2':
  1528. contcheck = appointment_view();
  1529. if(contcheck==1)
  1530. return contcheck;
  1531. break;
  1532. case '3':
  1533. contcheck = appointment_search();
  1534. if(contcheck==1)
  1535. return contcheck;
  1536. break;
  1537. case '4':
  1538. contcheck = appointment_modify();
  1539. if(contcheck==1)
  1540. return contcheck;
  1541. break;
  1542. case '5':
  1543. contcheck = appointment_delete();
  1544. if(contcheck==1)
  1545. return contcheck;
  1546. break;
  1547. default:
  1548. error_msg();
  1549. }
  1550. }
  1551.  
  1552. int pat_doc_action_nav()
  1553. {
  1554. int contcheck = 0;
  1555.  
  1556. cls();
  1557.  
  1558. switch(action)
  1559. {
  1560. case '1':
  1561. contcheck = patient_view();
  1562. if(contcheck==1)
  1563. return contcheck;
  1564. case '2':
  1565. contcheck = patient_search();
  1566. if(contcheck==1)
  1567. return contcheck;
  1568. break;
  1569. default:
  1570. error_msg();
  1571. }
  1572. }
  1573.  
  1574. int app_doc_action_nav()
  1575. {
  1576. int contcheck = 0;
  1577.  
  1578. cls();
  1579.  
  1580. switch(action)
  1581. {
  1582. case '1':
  1583. contcheck = appointment_view();
  1584. if(contcheck==1)
  1585. return contcheck;
  1586. case '2':
  1587. contcheck = appointment_search();
  1588. if(contcheck==1)
  1589. return contcheck;
  1590. break;
  1591. default:
  1592. error_msg();
  1593. }
  1594. }
  1595.  
  1596. /* --------------------------------------------Selection Menu------------------------------------------------------ */
  1597.  
  1598. void usertype_selection()
  1599. {
  1600. do
  1601. {
  1602. cls();
  1603. printf("1. Receptionist\n2. Doctor\n\nPress respective key to select a type...\nPress ESC to exit\n");
  1604. selection_limiter(1, 50, 49);
  1605. }while(!(usertype>=49 && usertype<=50));
  1606. }
  1607.  
  1608. void actiontype_selection()
  1609. {
  1610. do
  1611. {
  1612. cls();
  1613. printf("1. Patient Management\n2. Appointment Management\n\nPress respective key to select an option...\nPress ESC to logout\n");
  1614. selection_limiter(2, 50, 49);
  1615. }while(!(actiontype>=49 && actiontype<=50));
  1616. }
  1617.  
  1618. int pat_rec_action_selection()
  1619. {
  1620. int goback_check;
  1621.  
  1622. do
  1623. {
  1624. cls();
  1625. printf("1. Add Patient\n2. View Patient\n3. Search Patient\n4. Modify Patient\n5. Delete Patient\n\nPress respective key to select an action...\nPress BACKSPACE to go to previous menu\nPress ESC to exit\n");
  1626. goback_check = selection_limiter(3, 53, 49);
  1627. if(goback_check==1)
  1628. break;
  1629. }while(!(action>=49 && action<=53));
  1630.  
  1631. return goback_check;
  1632. }
  1633.  
  1634. int app_rec_action_selection()
  1635. {
  1636. int goback_check;
  1637.  
  1638. do
  1639. {
  1640. cls();
  1641. printf("1. Add Appointment\n2. View Appointment\n3. Search Appointment\n4. Modify Appointment\n5. Delete Appointment\n\nPress respective key to select action...\nPress BACKSPACE to go to previous menu\nPress ESC to exit\n");
  1642. goback_check = selection_limiter(3, 53, 49);
  1643. if(goback_check==1)
  1644. break;
  1645. }while(!(action>=49 && action<=53));
  1646.  
  1647. return goback_check;
  1648. }
  1649.  
  1650. int pat_doc_action_selection()
  1651. {
  1652. int goback_check;
  1653.  
  1654. do
  1655. {
  1656. cls();
  1657. printf("1. View Patient\n2. Search Patient\n\nPress respective key to select an action...\nPress BACKSPACE to go to previous menu\nPress ESC to exit\n");
  1658. goback_check = selection_limiter(3, 53, 49);
  1659. if(goback_check==1)
  1660. break;
  1661. }while(!(action>=49 && action<=50));
  1662.  
  1663. return goback_check;
  1664. }
  1665.  
  1666. int app_doc_action_selection()
  1667. {
  1668. int goback_check;
  1669.  
  1670. do
  1671. {
  1672. cls();
  1673. printf("1. Add Appointment\n2. View Appointment\n3. Search Appointment\n4. Modify Appointment\n5. Delete Appointment\n\nPress respective key to select action...\nPress BACKSPACE to go to previous menu\nPress ESC to exit\n");
  1674. goback_check = selection_limiter(3, 53, 49);
  1675. if(goback_check==1)
  1676. break;
  1677. }while(!(action>=49 && action<=53));
  1678.  
  1679. return goback_check;
  1680. }
  1681.  
  1682. void pat_search_selection()
  1683. {
  1684. int goback_check;
  1685.  
  1686. do
  1687. {
  1688. cls();
  1689. printf("Search Patient\n\n1. Search with name\n2. Search with patient ID\n\nPress respective key to select action\nPress BACKSPACE to go to previous menu\nPress ESC to exit\n");
  1690. goback_check = selection_limiter(4, 50, 49);
  1691. if(goback_check==1)
  1692. {
  1693. search_choice='0';
  1694. return;
  1695. }
  1696. }while(!(search_choice>=49 && search_choice<=50));
  1697. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement