Advertisement
Guest User

Untitled

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