Advertisement
Guest User

Untitled

a guest
May 14th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.57 KB | None | 0 0
  1. /*********************************************************
  2. *Patient Data Management System                         *
  3. *Developed for Dr. Grace-Ann Cooper                     *
  4. *Jonathan Cooper                                        *
  5. *13-1                                                   *
  6. *Ms Jones                                               *
  7. *Computer Science                                       *
  8. /*********************************************************/
  9.  
  10.  
  11. //header files
  12. #include<stdio.h>
  13. #include<conio.h>
  14. #include<stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17.  
  18. //inline functions
  19. #define TIME() time_t rawtime; time(&rawtime); printf("The current local time is: %s\n\n", ctime(&rawtime));
  20.  
  21. //Data structure to hold Patient Data
  22. typedef struct {
  23.         int patientID; //Unique Patient ID
  24.         char fname[40]; //First Name
  25.         char lname[40]; //Last Name
  26.         char *service; //Service
  27.         float price; //Price of Services
  28.         float paid; //Amount Paid
  29.         float owing; //Amount Owing
  30. }pData;
  31.  
  32. //Function prototype declarations
  33. void login(void);
  34. void header(char *text);
  35. void menu(void);
  36. void config(void);
  37. void manageData(int opt);
  38. pData saveData(pData Info, char *filename);
  39. pData printData(pData Info);
  40. void readData(char *filename);
  41. pData appendData(pData Info, char *filename);
  42.  
  43. //Global Variables
  44. int option;
  45. pData Patient;
  46.  
  47. int main (void) {
  48.     login();
  49. }
  50.  
  51.  
  52. void login(void) {
  53.    
  54.     char valid[20], vpass[20];
  55.    
  56.     FILE *info;
  57.    
  58.     if((info = fopen("login.txt","r"))) {  // Check if file exists / is valid
  59.              fscanf(info,"%s",&valid); //Get the Username and Password
  60.              fscanf(info,"%s",&vpass);
  61.              fclose(info);        
  62.     }
  63.    
  64.     else {
  65.          header("E R R O R!");
  66.          printf("Program not configured!\nPress Enter to begin configuration . . .");
  67.          getch();
  68.          system("cls");
  69.          config();
  70.     }
  71.      
  72.    
  73.     int i = 0;
  74.    
  75.     char user[7], pass[6];
  76.     header("L O G I N");
  77.    
  78.     printf("Welcome to the Cooper Patient Data Payment Management System\n\nPlease enter your credentials:\n\n");
  79.    
  80.     printf("User Name:\t");
  81.     scanf("%s",&user);
  82.    
  83.     printf("\n\nPassword:\t");
  84.    
  85.     scanf("%s",&pass);
  86.    
  87.     if(strcmp(user,valid)==0 && strcmp(pass,vpass)==0 ) { //ensure validity of username AND password
  88.                                 system("cls");
  89.                                 header("S U C C E S S");
  90.                                 printf("\n\n\n\n\n\n\t\tPress Enter To Continue to Menu. . . ");
  91.                                 getch();
  92.                                 system("cls");
  93.                                 menu();
  94.     }
  95.    
  96.     /* Create a proper error message here */
  97.     else { //If User info is invalid
  98.          system("cls");
  99.          header("E R R O R ");
  100.          printf("Invalid username or password.  Press Enter to try again . . . ");
  101.          getch();
  102.          system("cls");
  103.          login();
  104.     }
  105. }
  106.  
  107. /* Menu function */
  108. void menu(void) {
  109.      system("cls");
  110.      header("M E N U");
  111.      printf("Options:\n\
  112.     [1]\tNew Patient\n\
  113.     [2]\tExisting Patient\n\
  114.     [3]\tCurrent Patient Information\n\
  115.     [4]\tExit Program\n\
  116.     [5]\tSettings\n");
  117.      
  118.      scanf("%d",&option);
  119.      fflush(stdin); //flush input stream to avoid fatal error if user enters a character
  120.      system("cls");
  121.      
  122.      manageData(option); //passes the user selection to this function
  123.      
  124. }
  125.  
  126. //header for menus, etc
  127. void header(char *text) {
  128.      printf("\t\t====================================================\n");
  129.      printf("\t\t\t\t\t%s\n\n\t\t", text);
  130.      TIME()
  131.      printf("\t\t====================================================\n\n\n\n");
  132. }
  133.  
  134. //allows for setup of user data
  135. void config(void) {
  136.      
  137.      char username[20], password[20], confirm[20];
  138.      FILE *config, *pID;
  139.      
  140.      header("C O N F I G");
  141.      printf("Enter Desired Username:\t");
  142.      scanf("%s",&username);
  143.      printf("\nEnter Desired Password:\t");
  144.      scanf("%s",&password);
  145.      printf("\nEnter Password Again:\t");
  146.      scanf("%s",&confirm);
  147.      
  148.      if(strcmp(confirm, password)==0) {
  149.                        
  150.                         if(config = fopen("login.txt","w")) {
  151.                                   if(pID = fopen("ID.txt","w")) {
  152.                                          fprintf(config,"%s\t%s",username,password);
  153.                                          fprintf(pID,"1");
  154.                                          fclose(pID);
  155.                                          fclose(config);
  156.                                                                    
  157.                                          system("cls");
  158.                                          header("S U C C E S S");
  159.                                          printf("\n\n\nFile Write Successful!\n\nPress enter to access the menu . . .");
  160.                                          getch();
  161.                                          system("cls");
  162.                                          menu();
  163.                                   }
  164.                         }
  165.                                  
  166.                         else {
  167.                              system("cls");
  168.                              header("F A I L U R E");
  169.                              printf("\n\nID File Write Failed\n");
  170.                              getch();
  171.                              system("cls");
  172.                              login();
  173.                         }
  174.      }            
  175.      
  176.      
  177.      else {
  178.           system("cls");
  179.           header("F A I L U R E");
  180.           printf("Passwords do not match!\nPress enter to try again . . .");
  181.           getch();
  182.           system("cls");
  183.           login();
  184.      }
  185. }
  186.  
  187. void manageData(int opt) {
  188.      
  189.      char patient[50];
  190.      int ID;
  191.      FILE *pID, *IDWrite;
  192.      if(pID = fopen("ID.txt","r")) {
  193.             fscanf(pID,"%d",&ID);
  194.             fclose(pID);
  195.      }      
  196.      
  197.      if(IDWrite = fopen("ID.txt","w")) {
  198.                 ID++;
  199.                 fprintf(IDWrite,"%d",ID);
  200.                 fclose(IDWrite);
  201.      }
  202.      
  203.      else {
  204.           system("cls");
  205.           header("F A I L U R E");
  206.           printf("\n\nFile read operation failed\n");
  207.           getch();
  208.           menu();
  209.      }
  210.      
  211.      switch(opt) {
  212.                  case 1: header("N E W  D A T A");
  213.                          printf("\n\n\nEnter your first name:\t");
  214.                          scanf("%s",&Patient.fname);  
  215.                          printf("\nEnter your last name:\t");
  216.                          scanf("%s",&Patient.lname);
  217.                          
  218.                          printf("Enter service performed:\n");
  219.                          printf("[1]\tCheckup\n[2]\tRefferal\n");
  220.                          printf("Select an option:\t");
  221.                          scanf("%d",&opt);
  222.                          
  223.                          switch(opt) {
  224.                                      case 1: Patient.service = "Checkup";
  225.                                      break;
  226.                                      case 2: Patient.service = "Refferal";
  227.                                      break;
  228.                                      default: printf("Invalid Option!\nPress enter to try again . . . ");
  229.                                               getch();
  230.                                               manageData(1);
  231.                                      break;
  232.                          }
  233.                                      
  234.                          
  235.                          printf("\nEnter price of services:\t$");
  236.                          scanf("%f",&Patient.price);
  237.                          
  238.                          printf("\nEnter amount paid:\t$");
  239.                          scanf("%f",&Patient.paid);
  240.                          
  241.                          Patient.owing = Patient.price - Patient.paid;
  242.                          
  243.                          Patient.patientID = ID--;
  244.                          getch();
  245.                          
  246.                          printData(Patient);
  247.                  break;
  248.                  case 2: header("E X I S T I N G");
  249.                          
  250.                          printf("Enter the last name of the patient who's record you wish to update:\t");
  251.                          scanf("%s",&patient);
  252.                          
  253.                          appendData(Patient, patient);
  254.                          
  255.                          getch();
  256.                  break;
  257.                  case 3: header("V I E W");
  258.                          printf("Enter the last name of the patient who's record you wish to view:\t");
  259.                          scanf("%s",&patient);
  260.                          readData(patient);
  261.                          getch();
  262.                  break;
  263.                  
  264.                  case 4: printf("Are you sure you wish to exit?\n[1]\tYes\n[2]\tNo\nEnter Option:\t");
  265.                          scanf("%d",&option);
  266.                          switch(option) {
  267.                                         case 1: exit(0);
  268.                                         break;
  269.                                         case 2: system("cls");
  270.                                                 menu();
  271.                                         break;
  272.                                         default: system("cls");
  273.                                                  header("E R R O R");
  274.                                                  printf("Invalid Input.\nPress Enter to continue . . .");
  275.                                                  getch();
  276.                                                  manageData(4);
  277.                                         break;
  278.                          }
  279.                  case 5: config();
  280.                  break;
  281.                  default: system("cls");
  282.                           header("E R R O R");
  283.                           printf("Invalid Input.\nPress Enter to try again . . . ");
  284.                           getch();
  285.                           menu(); //back to menu() function if input was invalid
  286.                  break;                
  287.      }
  288. }
  289.  
  290. //output data
  291. pData printData(pData Info)
  292. {
  293.      system("cls");
  294.      header("D A T A");
  295.      printf("Name:\t%s %s\n",Info.fname,Info.lname);
  296.      printf("ID Number:\t%d\n",Info.patientID);
  297.      printf("Service Performed:\t%s\n",Info.service);
  298.      printf("Cost of Services:\t%.2f\n",Info.price);
  299.      printf("Amount Paid:\t%.2f\n",Info.paid);
  300.      printf("Amount Owing:\t%.2f\n",Info.owing);
  301.      
  302.      getch();
  303.      
  304.      saveData(Info, Info.lname);
  305. }
  306.  
  307. //save data to a file    
  308. pData saveData(pData Info, char *filename) {
  309.       FILE *data;
  310.       if(data = fopen(filename,"w")) {
  311.               fprintf(data,"ID Number:\t%d\n\n\n",Info.patientID);
  312.               fprintf(data,"Name:\t%s %s\n",Info.fname,Info.lname);
  313.               fprintf(data,"Service Performed:\t%s\n",Info.service);
  314.               fprintf(data,"Cost of Services:\t%.2f\n",Info.price);
  315.               fprintf(data,"Amount Paid:\t%.2f\n",Info.paid);
  316.               fprintf(data,"Amount Owing:\t%.2f\n",Info.owing);
  317.               fclose(data);
  318.               menu();
  319.       }
  320.      
  321.       else {
  322.            printf("ERROR SAVING DATA TO FILE!  Try Again!");
  323.            getch();
  324.            menu();
  325.       }
  326. }
  327.  
  328. //read all data stored in a file
  329. void readData(char *filename) {
  330.      
  331.      FILE *data;
  332.      
  333.      long lSize; //size of file
  334.      char *buffer; //pointer to store file contents
  335.      size_t result; //buffer to check consistiency of file
  336.  
  337.      system("cls");
  338.      header("V I E W");
  339.      
  340.      if(data = fopen(filename,"rb")) { //open file for reading in binary mode
  341.              fseek (data , 0 , SEEK_END); //set pointer to end of file
  342.              lSize = ftell (data); //tell position, therefore passing file size
  343.              rewind (data);  //set pointer to beginning of file
  344.              buffer = (char*) malloc (sizeof(char)*lSize); //allocate the memory
  345.              
  346.              if (buffer == NULL) { // ensure validity of pointer
  347.                         printf("MEMORY ERROR\nPress Enter to return to menu . . .");
  348.                         getch();
  349.                         menu();
  350.              }
  351.              
  352.              result = fread (buffer,1,lSize,data); //check the amount of data read
  353.              
  354.              if (result != lSize) { //ensure validity
  355.                         printf("File Read Error!\nPress Enter to return to menu . . .");
  356.                         getch();
  357.                         menu();
  358.              }
  359.              
  360.              fflush(data);
  361.              
  362.              printf("%s",buffer); //output data
  363.              printf("\n\nPress Enter to return to menu . . .");
  364.              fclose (data);
  365.              free (buffer); //free memory allocated to avoid a leak
  366.              
  367.              getch();
  368.              menu();
  369.              
  370.      }
  371.      
  372.      else {
  373.           system("cls");
  374.           header("E R R O R");
  375.           printf("Patient does not exist!\nPress enter to return to menu . . .");
  376.           getch();
  377.           menu();
  378.      }
  379.          
  380. }
  381.  
  382. //append existing patient data to data file            
  383. pData appendData(pData Info, char *filename) {
  384.      
  385.       int opt;
  386.      
  387.       system("cls");
  388.      
  389.       header("U P D A T E");
  390.      
  391.       printf("Patient:\t%s",filename);
  392.      
  393.       switch(opt) {
  394.                   case 1: Info.service = "Checkup";
  395.                   break;
  396.                   case 2: Info.service = "Refferal";
  397.                   break;
  398.                   default: printf("Invalid Option!\nPress enter to try again . . . ");
  399.                            getch();
  400.                            appendData(Info, filename);
  401.                            break;
  402.                   }
  403.                          
  404.       printf("\nEnter price of services:\t$");
  405.       scanf("%f",&Info.price);
  406.                          
  407.       printf("\nEnter amount paid:\t$");
  408.       scanf("%f",&Info.paid);
  409.                          
  410.       Info.owing = Info.price - Info.paid;                
  411.      
  412.  
  413.       FILE *data;
  414.       if(data = fopen(filename,"a")) {
  415.              
  416.               fprintf(data,"\n\nService Performed:\t%s\n",Info.service);
  417.               fprintf(data,"Cost of Services:\t%.2f\n",Info.price);
  418.               fprintf(data,"Amount Paid:\t%.2f\n",Info.paid);
  419.               fprintf(data,"Amount Owing:\t%.2f\n",Info.owing);
  420.               fclose(data);
  421.               menu();
  422.       }
  423.      
  424.       else {
  425.            printf("ERROR SAVING DATA TO FILE!  Try Again!");
  426.            getch();
  427.            menu();
  428.       }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement