Advertisement
Guest User

Dentist-System.c

a guest
Dec 24th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.99 KB | None | 0 0
  1. /**
  2. THIS CODE IS DESIGNED TO RUN ON UNIX SYSTEMS (MAC OR LINUX)
  3. IF YOU ARE USING WINDOWS TO COMPLIE THIS CODE, YOU'LL NEED TO MAKE A COUPLE CANGES.
  4.  
  5. THESE CHANGES ARE:
  6. 1. REPLACE <unistd.h> WITH <time.h>
  7. 2. REPLACE <termios.h> WITH <conio.h>
  8. 3. REMOVE THE GETCH FUNCTION FROM THE CODE (IT IS INDECATED IN THE CODE AS "GETCH FUNCTION")
  9. 4. REPLACE ALL INSTANCES OF sleep() WITH Sleep()
  10. 5. IN THE SLEEP FUNCTION, THE DIGIT WHICH INDICATES THE NUMBER OF SECONDS TO PAUSE FOR SHOULD BE IN MILLISECONDS.
  11.    I.E. 1 SECOND = 1000 MILLISECONDS
  12. 7. ALL INSTANCES OF "clear" SHOULD BE REPLACED WITH "cls"
  13. 8. SEE COMMENTS FOR MORE CHANGES.. (LOGIN FUNCTION)
  14. **/
  15.  
  16. //--- Header Files ----
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <unistd.h>  /**This library is only for unix users (Mac or Linux) for windows users this library would have to be
  22.                      replaced with the time.h library to use the sleep() function**/
  23.  
  24. #include <termios.h> /**This library is only for unix users (Mac or Linux) for windows users this library would have to be
  25.                      replaced with the conio.h library to use the getch() function**/
  26. //---------------------
  27.  
  28. #define MIN -1          //Set minium value in range
  29. #define MAX 1000000     //Set maxium value in range
  30. int count=0;
  31.  
  32. //--- Declare Structures---
  33. struct patientInfo
  34. {
  35.     int patient_no;
  36.     int treatment_cost;
  37.     char insurance_name[25];
  38.     char address[70];
  39.     char name[80];
  40.     char treatment[50];
  41. }Patient[MAX];                 //Global Structure Array
  42. //------------------------------------------------
  43.  
  44. //--- Function Proto-types-----
  45. void login();               //Login Function username: Admin password: P@$$w0rd (case sensitive)
  46. int displaymenu();          //Display the menu
  47. void menu();                //Calls the respective function based on the option selected in the menu displayed
  48. void add();                 //Allows the user to add patients into a structure array
  49. /**void search();           //ALlows the user to search for a patients based on patient number
  50. void display(); **/         //Displays all patient detals
  51. //---------------------------------------------------------------------------------------------------
  52.  
  53. //------------------------- MAIN FUNCTION ------------------------
  54. int main()
  55. {
  56.     int choice;
  57.    // login();                //Calling the login function
  58.     choice=displaymenu();   //Calling the display menu function and storing the return valued the the variable "choice"
  59.     menu(choice);           //Calling the "choice" option function from the menu function
  60.     getch();                //Holds the information on the screen until a key is pressed
  61.     return 0;
  62. }
  63. //--------------------------------------------------------------------------------------------------
  64.  
  65. void add(){
  66.     char op, answer, insurance;
  67.     int x;
  68.     static int i=MIN; //Static variable 'i' will remember where it stop counting to prevent the data from being overwritten
  69.     i++;
  70.     for(i;i<=MAX;i++){ //MAX value = 1000000
  71.         printf("\n\nEnter data for record #%d\n", i + 1);
  72.  
  73.         printf("\nEnter Patient Number :");
  74.         scanf("%d", &Patient[i].patient_no);
  75.         fflush(stdin);
  76.  
  77.         printf("\nEnter Patient Full Name : ");
  78.         scanf(" %[^\n]%*c", Patient[i].name);
  79.  
  80.         printf("\nEnter the Patient's Address : ");
  81.         scanf(" %[^\n]%*c", Patient[i].address);
  82.  
  83.         printf("\nEnter the treatment the patient recieved : ");
  84.         scanf(" %[^\n]%*c", Patient[i].treatment);
  85.  
  86.         printf("\nEnter the cost for treatment : ");
  87.         scanf("%d", &Patient[i].treatment_cost);
  88.  
  89.         printf("\nDo you have insurance, y or n? ");
  90.         scanf(" %c",&answer);
  91.         answer=tolower(answer);
  92.  
  93.         if (answer == 'y')
  94.         {
  95.             while(1){
  96.                 printf("\nWhich of these insurance to you belong to?\n");
  97.                 printf("Select the letter that corresponds with your Insurance\n");
  98.                 printf("a. Guardian Life\n");
  99.                 printf("b. Victoria Mutual\n");
  100.                 printf("c. Life Time Insurance\n");
  101.                 printf("> ");
  102.                 scanf(" %c",&insurance);
  103.                 insurance=tolower(insurance);
  104.  
  105.                 if(insurance=='a'){
  106.                     strcpy(Patient[i].insurance_name, "Guardian Life");
  107.                     Patient[i].treatment_cost = (Patient[i].treatment_cost - (Patient[i].treatment_cost * 0.15));
  108.                     break;
  109.  
  110.                 }else if(insurance=='b'){
  111.                     strcpy(Patient[i].insurance_name, "Victoria Mutual");
  112.                     Patient[i].treatment_cost = (Patient[i].treatment_cost - (Patient[i].treatment_cost * 0.20));
  113.                     break;
  114.  
  115.                 }else if(insurance=='c'){
  116.                     strcpy(Patient[i].insurance_name, "Life Time Insurance");
  117.                     Patient[i].treatment_cost = (Patient[i].treatment_cost - (Patient[i].treatment_cost * 0.16));
  118.                     break;
  119.                 }
  120.                 else{
  121.                     printf("\a \t \t|    Invaild Option Selection, Try Again    |\n");
  122.                 }
  123.             }
  124.             printf("\nThe final cost for treatment is %d", Patient[i].treatment_cost);
  125.         }else{
  126.             strcpy(Patient[i].insurance_name, "NONE");
  127.             printf("\nThe final cost for treatment is %d", Patient[i].treatment_cost);
  128.         }
  129.  
  130.         printf("\n\n\t     Press 'y' to Add more records or \'n\' to stop\n\n");
  131.         printf("> ");
  132.         scanf(" %c",&op);
  133.         op=tolower(op);
  134.         count++;                                               //Makes users input in "op" lowercased
  135.         if (op=='n')
  136.         {
  137.             printf("\t\t\t   Total Records added: %d\n\n",i+1);        //Displays the total number of records added to the text file
  138.             getchar();
  139.             break;                                                     //Ends loop
  140.         }
  141.     }
  142.     system("clear");        //Clear Screen
  143.     menu(displaymenu());
  144. }
  145.  
  146. //---------------------- MENU FUNCTION ---------------------------
  147. void menu(int x)                                    //Function accepts an integer value of "choice" from the main function
  148. {
  149.     switch(x)
  150.     {
  151.         case 1:                                     //Executes the following block of code if the integer value x is 1
  152.         {
  153.             system("clear");                        //Clears the screen
  154.             puts("\n \t \t  |=======================================|");
  155.             puts("\t \t  |\t         ADD PATIENT     \t  |");
  156.             puts("\t \t--|=======================================|--");
  157.             add();                              //Add function called to add records Patient structure
  158.             break;                                  //Ends this instance of the program
  159.         }
  160.  
  161.         case 2:                                     //Executes the following block of code if the integer value x is 2
  162.         {
  163.             system("clear");            //Clears the screen
  164.             int j, number,l;
  165.             l=0;
  166.             puts("\n \t \t  |=======================================|");
  167.             puts("\t \t  |\t       LOOK UP PATIENT    \t  |");
  168.             puts("\t \t--|=======================================|--");
  169.  
  170.             if(count==0){
  171.                 printf("No records Added yet!");
  172.             }
  173.             else{
  174.                 printf("Enter Patient Number: ");
  175.                 scanf("%d",&number);
  176.                 for(j=0;j<count;j++)                                    //Makes sure x is less than the current array posstion
  177.                     {
  178.                         if(Patient[j].patient_no==number)       //Checks structure array if case number entered already exists
  179.                         {
  180.                             printf("\nPatient Number: %d\n", Patient[j].patient_no);
  181.                             printf("Name: %s\n", Patient[j].name);
  182.                             printf("Address: %s\n", Patient[j].address);
  183.                             printf("Treatmet: %s\n", Patient[j].treatment);
  184.                             printf("Insurance Name: %s\n", Patient[j].insurance_name);
  185.                             printf("Treatment Cost: %d\n", Patient[j].treatment_cost);
  186.                             l++;
  187.                         }
  188.                     }
  189.                     if(l==0){
  190.                     printf("No match for patient number %d was found!", number);
  191.                     }
  192.                 }
  193.  
  194.             getchar();
  195.             getchar();
  196.             system("clear");                        //Clear Screen
  197.             menu(displaymenu());
  198.             break;                                  //Ends this instance of the program
  199.         }
  200.  
  201.         case 3:                                     //Executes the following block of code if the integer value x is 3
  202.         {
  203.             system("clear");        //Clears the screen
  204.             int d;
  205.             puts("\n \t \t  |=======================================|");
  206.             puts("\t \t  |\t       PATIENT DETAILS \t\t  |");
  207.             puts("\t \t--|=======================================|--");
  208.             if(count==0){
  209.                 printf("No records Added yet!");
  210.             }
  211.             else{
  212.                 for(d=0;d<count;d++){
  213.                     printf("\nPatient Number: %d\n", Patient[d].patient_no);
  214.                     printf("Name: %s\n", Patient[d].name);
  215.                     printf("Address: %s\n", Patient[d].address);
  216.                     printf("Treatmet: %s\n", Patient[d].treatment);
  217.                     printf("Insurance Name: %s\n", Patient[d].insurance_name);
  218.                     printf("Treatment Cost: %d\n", Patient[d].treatment_cost);
  219.                 }
  220.             }
  221.             getchar();
  222.             getchar();
  223.             system("clear");
  224.             menu(displaymenu());
  225.             break;                                  //Ends this instance of the program
  226.         }
  227.  
  228.         case 4:                                     //Executes the following block of code if the integer value x is 4
  229.         {
  230.             printf("\t\t\t\tLogin Out...\n");       //Message to user
  231.             sleep(2);                               //Pause program for 2 seconds
  232.             exit(0);                                //Close program
  233.             break;                                  //Ends this instance of the program
  234.         }
  235.         default:                                    //Executes the following block of code if the integer value x is not any of the instances above
  236.             {
  237.                 printf("\a \t \t|    Invaild Option Selection, Try Again    |\n");
  238.                 puts("\t \t|-------------------------------------------|");
  239.                 sleep(1);                         //Pause program for 1 second
  240.                 system("clear");                 //Clears Screen
  241.                 menu(displaymenu());            //Calls the menu function using the return value from the displaymenu function as arugment
  242.                 break;                         //Ends this instance of the program
  243.             }
  244.     }
  245. }
  246. //-------------------------------------------------------------------------------------
  247.  
  248. //---------------- DISPLAY MENU FUNCTION -----------------
  249. int displaymenu()
  250. {
  251.     int option;
  252.     puts("\n \t \t  |=======================================|");
  253.     puts("\t \t  | \t           MAIN MENU \t \t  |");
  254.     puts("\t \t--|=======================================|--");
  255.     printf("\t \t|  Select the number that corresponds with  |\n \t \t|\t \t your option.\t \t    |\n");
  256.     puts("\t \t|-------------------------------------------|");
  257.     printf("\t \t|\t  1. Add Patient \t\t    |\n");
  258.     printf("\t \t|\t  2. Look up Patient \t \t    |\n");
  259.     printf("\t \t|\t  3. Display Patient Details\t    |\n");
  260.     printf("\t \t|\t  4. Exit\t \t \t    |\n");
  261.     puts("\t \t|-------------------------------------------|");
  262.     printf("\t \t| Option: ");
  263.     scanf("%d",&option);
  264.     puts("\t \t|-------------------------------------------|");
  265.     return option;  //Returns the number the user chooses and stores it in the "choice" variable in the main function
  266. }
  267. //--------------------------------------------------------------------------------------
  268.  
  269. //------------------- LOGIN FUNCTION -----------------------------
  270. void login()
  271. {
  272.     int i;                                       //Variable Declaration
  273.     char uid[25],pwd[25],s_uid[25]={"Admin"};    /**Created three variables one for user ID (uid[25]) and one user password
  274.                                                  (pwd[25]). s_uid[25] is the varible holding the user ID the user is suppose to
  275.                                                  enter to get in the system. Anything other that user ID (Admin) will be not
  276.                                                  have access to the system**/
  277.  
  278.     char s_pwd[25]={"p@$$w0rd"},ch=' ';          /**Created a variable (s_pwd[25]) to hold the password the user is suppose to
  279.                                                  enter to get access to the system. Anything other that user password (p@$$w0rd)
  280.                                                  will be not have access to the system. **/
  281.     puts("\n\n\n\n\n\t\t\t\t    WELCOME");
  282.     puts("\n\t\t    CHAPELTON DENTISTRY REGISTRATION SYSTEM");
  283.     puts("\n\t \t \t |-----------------------------|");
  284.     puts("\t \t \t |             LOGIN           |");
  285.     puts("\t \t \t |-----------------------------|");
  286.     printf(" \t \t \t |Enter the User ID : ");
  287.     scanf("%s",uid);
  288.     getchar();
  289.     printf(" \t \t \t |Enter the Password : ");
  290.     i=0;
  291.     while(1)     //Loops code forever
  292.     {
  293.         ch=getch();
  294.         if(ch==10)           //10 is the ASCII value for the "Enter" key. Windows users would use the value 13 instead.
  295.             break;           //Exit loop
  296.         else if(ch==127)     //127 is the ASCII value for the "backspace" key. Windows users would use the value 8 instead.
  297.         {       if(i!=0)     //This is for avoiding the input from being deleted
  298.             {
  299.                 printf("\b");       //'\b' represents blackspace escape character
  300.                 printf(" ");
  301.                 printf("\b");       //'\b' represents blackspace escape character
  302.                 i--;                //Go back to the array position that had the character that was removed by backspace
  303.                 pwd[i]='\0';        //End of array
  304.             }
  305.             else
  306.                 continue;
  307.         }
  308.         else
  309.         {
  310.             putchar('*');         //Replaces each character type by a *
  311.             pwd[i]=ch;            //Adds the character typed to the pwd array
  312.             i++;                  //Move to the next postion in the array
  313.         }
  314.     }
  315.     puts("\n \t \t \t |-----------------------------|");
  316.     pwd[i]='\0';
  317.     if((strcmp(uid,s_uid))==0 && (strcmp(pwd,s_pwd))==0) //checks if the user input is the same as what was set in the code (Admin, p@$$20rd)
  318.     {
  319.         printf("\t \t \t |\t USER AUTHENTICATED    |");
  320.         puts("\n \t \t \t |-----------------------------|");
  321.         sleep(1); //Pause the program for 1 second. Windows users would have to change this function to Sleep(1000) to get the same result
  322.         system("clear"); //"clear" is for unix users. Windows users whould use "cls" instead of "clear"
  323.     }
  324.     else
  325.     {
  326.         printf("\t \a \t \t |UNABLE TO AUTHENTICATE USER, |\n \t \t \t | \t    Try again\t       |");
  327.         puts("\n \t \t \t |-----------------------------|");
  328.         sleep(2); //Pause the program for 2 second. Windows users would have to change this function to Sleep(2000) to get the same result
  329.         system("clear"); //"clear" is for unix users. Windows users whould use "cls" instead of "clear"
  330.         fflush(stdin);
  331.         login(); //Recall the login function (Recursion)
  332.     }
  333. }
  334. //-----------------------------------------------------------------------------------------
  335.  
  336. //------------------- GETCH FUNCTION -----------------------------
  337. /**BECAUSE UNIX SYSTEMS DON'T SUPPORT THE CONIO.H LIBRARY THAT CONTAINS IT'S OWN BUILT IN GETCH() FUNCTION
  338. WE HAVE TO MANUALLY BUILD A GETCH FUNCTION SUPPORTED BY THE TERMIOS.H LIBRARY. WINDOWS USERS CAN USE INCLUDE
  339. THE CONIO.H AND REMOVE THIS FUNCTION**/
  340.  
  341. int getch(void)
  342. {
  343.     struct termios oldattr, newattr;
  344.     int ch;
  345.     tcgetattr( STDIN_FILENO, &oldattr );
  346.     newattr = oldattr;
  347.     newattr.c_lflag &= ~( ICANON | ECHO);//knock down keybuffer
  348.     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
  349.     system("stty -echo");//shell out to kill echo
  350.     ch = getchar();
  351.     system("stty echo");
  352.     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
  353.     return ch;
  354. }
  355. //-----------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement