/*********************************************************
*Patient Data Management System *
*Developed for Dr. Grace-Ann Cooper *
*Jonathan Cooper *
*13-1 *
*Ms Jones *
*Computer Science *
/*********************************************************/
//header files
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
#include <time.h>
//inline functions
#define TIME() time_t rawtime; time(&rawtime); printf("The current local time is: %s\n\n", ctime(&rawtime));
//Data structure to hold Patient Data
typedef struct {
int patientID; //Unique Patient ID
char fname[40]; //First Name
char lname[40]; //Last Name
char *service; //Service
float price; //Price of Services
float paid; //Amount Paid
float owing; //Amount Owing
}pData;
//Function prototype declarations
void login(void);
void header(char *text);
void menu(void);
void config(void);
void manageData(int opt);
pData saveData(pData Info, char *filename);
pData printData(pData Info);
void readData(char *filename);
pData appendData(pData Info, char *filename);
//Global Variables
int option;
pData Patient;
int main (void) {
login();
}
void login(void) {
char valid[20], vpass[20];
FILE *info;
if((info = fopen("login.txt","r"))) { // Check if file exists / is valid
fscanf(info,"%s",&valid); //Get the Username and Password
fscanf(info,"%s",&vpass);
fclose(info);
}
else {
header("E R R O R!");
printf("Program not configured!\nPress Enter to begin configuration . . .");
getch();
system("cls");
config();
}
int i = 0;
char user[7], pass[6];
header("L O G I N");
printf("Welcome to the Cooper Patient Data Payment Management System\n\nPlease enter your credentials:\n\n");
printf("User Name:\t");
scanf("%s",&user);
printf("\n\nPassword:\t");
scanf("%s",&pass);
if(strcmp(user,valid)==0 && strcmp(pass,vpass)==0 ) { //ensure validity of username AND password
system("cls");
header("S U C C E S S");
printf("\n\n\n\n\n\n\t\tPress Enter To Continue to Menu. . . ");
getch();
system("cls");
menu();
}
/* Create a proper error message here */
else { //If User info is invalid
system("cls");
header("E R R O R ");
printf("Invalid username or password. Press Enter to try again . . . ");
getch();
system("cls");
login();
}
}
/* Menu function */
void menu(void) {
system("cls");
header("M E N U");
printf("Options:\n\
[1]\tNew Patient\n\
[2]\tExisting Patient\n\
[3]\tCurrent Patient Information\n\
[4]\tExit Program\n\
[5]\tSettings\n");
scanf("%d",&option);
fflush(stdin); //flush input stream to avoid fatal error if user enters a character
system("cls");
manageData(option); //passes the user selection to this function
}
//header for menus, etc
void header(char *text) {
printf("\t\t====================================================\n");
printf("\t\t\t\t\t%s\n\n\t\t", text);
TIME()
printf("\t\t====================================================\n\n\n\n");
}
//allows for setup of user data
void config(void) {
char username[20], password[20], confirm[20];
FILE *config, *pID;
header("C O N F I G");
printf("Enter Desired Username:\t");
scanf("%s",&username);
printf("\nEnter Desired Password:\t");
scanf("%s",&password);
printf("\nEnter Password Again:\t");
scanf("%s",&confirm);
if(strcmp(confirm, password)==0) {
if(config = fopen("login.txt","w")) {
if(pID = fopen("ID.txt","w")) {
fprintf(config,"%s\t%s",username,password);
fprintf(pID,"1");
fclose(pID);
fclose(config);
system("cls");
header("S U C C E S S");
printf("\n\n\nFile Write Successful!\n\nPress enter to access the menu . . .");
getch();
system("cls");
menu();
}
}
else {
system("cls");
header("F A I L U R E");
printf("\n\nID File Write Failed\n");
getch();
system("cls");
login();
}
}
else {
system("cls");
header("F A I L U R E");
printf("Passwords do not match!\nPress enter to try again . . .");
getch();
system("cls");
login();
}
}
void manageData(int opt) {
char patient[50];
int ID;
FILE *pID, *IDWrite;
if(pID = fopen("ID.txt","r")) {
fscanf(pID,"%d",&ID);
fclose(pID);
}
if(IDWrite = fopen("ID.txt","w")) {
ID++;
fprintf(IDWrite,"%d",ID);
fclose(IDWrite);
}
else {
system("cls");
header("F A I L U R E");
printf("\n\nFile read operation failed\n");
getch();
menu();
}
switch(opt) {
case 1: header("N E W D A T A");
printf("\n\n\nEnter your first name:\t");
scanf("%s",&Patient.fname);
printf("\nEnter your last name:\t");
scanf("%s",&Patient.lname);
printf("Enter service performed:\n");
printf("[1]\tCheckup\n[2]\tRefferal\n");
printf("Select an option:\t");
scanf("%d",&opt);
switch(opt) {
case 1: Patient.service = "Checkup";
break;
case 2: Patient.service = "Refferal";
break;
default: printf("Invalid Option!\nPress enter to try again . . . ");
getch();
manageData(1);
break;
}
printf("\nEnter price of services:\t$");
scanf("%f",&Patient.price);
printf("\nEnter amount paid:\t$");
scanf("%f",&Patient.paid);
Patient.owing = Patient.price - Patient.paid;
Patient.patientID = ID--;
getch();
printData(Patient);
break;
case 2: header("E X I S T I N G");
printf("Enter the last name of the patient who's record you wish to update:\t");
scanf("%s",&patient);
appendData(Patient, patient);
getch();
break;
case 3: header("V I E W");
printf("Enter the last name of the patient who's record you wish to view:\t");
scanf("%s",&patient);
readData(patient);
getch();
break;
case 4: printf("Are you sure you wish to exit?\n[1]\tYes\n[2]\tNo\nEnter Option:\t");
scanf("%d",&option);
switch(option) {
case 1: exit(0);
break;
case 2: system("cls");
menu();
break;
default: system("cls");
header("E R R O R");
printf("Invalid Input.\nPress Enter to continue . . .");
getch();
manageData(4);
break;
}
case 5: config();
break;
default: system("cls");
header("E R R O R");
printf("Invalid Input.\nPress Enter to try again . . . ");
getch();
menu(); //back to menu() function if input was invalid
break;
}
}
//output data
pData printData(pData Info)
{
system("cls");
header("D A T A");
printf("Name:\t%s %s\n",Info.fname,Info.lname);
printf("ID Number:\t%d\n",Info.patientID);
printf("Service Performed:\t%s\n",Info.service);
printf("Cost of Services:\t%.2f\n",Info.price);
printf("Amount Paid:\t%.2f\n",Info.paid);
printf("Amount Owing:\t%.2f\n",Info.owing);
getch();
saveData(Info, Info.lname);
}
//save data to a file
pData saveData(pData Info, char *filename) {
FILE *data;
if(data = fopen(filename,"w")) {
fprintf(data,"ID Number:\t%d\n\n\n",Info.patientID);
fprintf(data,"Name:\t%s %s\n",Info.fname,Info.lname);
fprintf(data,"Service Performed:\t%s\n",Info.service);
fprintf(data,"Cost of Services:\t%.2f\n",Info.price);
fprintf(data,"Amount Paid:\t%.2f\n",Info.paid);
fprintf(data,"Amount Owing:\t%.2f\n",Info.owing);
fclose(data);
menu();
}
else {
printf("ERROR SAVING DATA TO FILE! Try Again!");
getch();
menu();
}
}
//read all data stored in a file
void readData(char *filename) {
FILE *data;
long lSize; //size of file
char *buffer; //pointer to store file contents
size_t result; //buffer to check consistiency of file
system("cls");
header("V I E W");
if(data = fopen(filename,"rb")) { //open file for reading in binary mode
fseek (data , 0 , SEEK_END); //set pointer to end of file
lSize = ftell (data); //tell position, therefore passing file size
rewind (data); //set pointer to beginning of file
buffer = (char*) malloc (sizeof(char)*lSize); //allocate the memory
if (buffer == NULL) { // ensure validity of pointer
printf("MEMORY ERROR\nPress Enter to return to menu . . .");
getch();
menu();
}
result = fread (buffer,1,lSize,data); //check the amount of data read
if (result != lSize) { //ensure validity
printf("File Read Error!\nPress Enter to return to menu . . .");
getch();
menu();
}
fflush(data);
printf("%s",buffer); //output data
printf("\n\nPress Enter to return to menu . . .");
fclose (data);
free (buffer); //free memory allocated to avoid a leak
getch();
menu();
}
else {
system("cls");
header("E R R O R");
printf("Patient does not exist!\nPress enter to return to menu . . .");
getch();
menu();
}
}
//append existing patient data to data file
pData appendData(pData Info, char *filename) {
int opt;
system("cls");
header("U P D A T E");
printf("Patient:\t%s",filename);
switch(opt) {
case 1: Info.service = "Checkup";
break;
case 2: Info.service = "Refferal";
break;
default: printf("Invalid Option!\nPress enter to try again . . . ");
getch();
appendData(Info, filename);
break;
}
printf("\nEnter price of services:\t$");
scanf("%f",&Info.price);
printf("\nEnter amount paid:\t$");
scanf("%f",&Info.paid);
Info.owing = Info.price - Info.paid;
FILE *data;
if(data = fopen(filename,"a")) {
fprintf(data,"\n\nService Performed:\t%s\n",Info.service);
fprintf(data,"Cost of Services:\t%.2f\n",Info.price);
fprintf(data,"Amount Paid:\t%.2f\n",Info.paid);
fprintf(data,"Amount Owing:\t%.2f\n",Info.owing);
fclose(data);
menu();
}
else {
printf("ERROR SAVING DATA TO FILE! Try Again!");
getch();
menu();
}
}