Advertisement
Guest User

Untitled

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