Friday, January 13, 2017

Programming Challenge 6.19 - Using Files - Hospital Report

Example File 1: InPatientBillingReport.txt
Example File 2: OutPatientBillingReport.txt

/* Using Files - Hospital Report - This program is a modification of
   Programming Challenge 6.15. In this version the final report is
   written to a file.

   ********************************************************************
   Overloaded Hospital - This program computes and displays the charges
   for a patient's hospital stay. First the program asks if the patient
   was admitted as an in-patient or an out-patient. If the patient was
   an in-patient, the following data should be entered:
  
   * The number of days spent in the hospital
   * The daily rate
   * Hospital medication charges
   * Charges for hospital services (lab tests, etc.)

   If the patient was an out-patient, the following data should be
   entered:

   * Charges for hospital services (lab tests, etc.)
   * Hospital medication charges

   This program uses two overloaded functions:

   * getPatientData()
   * getPatientData()

   Input Validation: This program does not accept negative numbers for
   any data.
   ******************************************************************** */

/* Function Prototypes: Get patient data, Get patient data */
double getPatientData(int, double, double, double);
double getPatientData(double, double);

#include "Utility.h"

int main()
{
   /* Constants: Menu items */
   const int INPATIENT_BILLING_SYSTEM = 1,
             OUTPATIENT_BILLING_SYSTEM = 2,
             QUIT = 3;

   /* Variables: Daily rate, Medication charges, Hospital service charges,
                 Inpatient and outpatient total charges */
   double dailyRate = 0.0,
          medCharges = 0.0,
          serviceCharges = 0.0,
          totalCharges = 0.0;

   /* Variable: Number of days, Menu choice */
   int numDays = 0,
       menuChoice = 0;

   /* Modification::Instantiate: file stream objects */
   ofstream inPatientBillingReport;
   ofstream outPatientBillingReport;

   do
   {
      /* Display: The menu, Ask if the patient was an in- or an
                  out-patient and get the according menu choice
                  from the user */
      cout << "\t\tXanth University Hospital Billing System\n"
           << "\t\t----------------------------------------\n\n"
           << "Please enter the necessary data for Com Passion\n"
           << "to compute the total charges of our (un)lucky\n"
           << "patients who survived Prof. Grossclout's harsh\n"
           << "curing methods.\n\n"
           << "Was the patient:\n\n"
           << "1.> An In-Patient?\n"
           << "2.> An Out-Patient?\n"
           << "3.> Quit\n\n"
           << "Your choice is: ";
      cin >> menuChoice;

      /* Validate: Menu choice */
      while (menuChoice < 1 || menuChoice > 3)
      {
         cout << "\nMENU CHOICE INVALID - CHANGING REALITY\n"
              << "GRAC'L SKELETON CHANGES INPUT TO 1 TO ENTER\n"
              << "DATA FOR IN-PATIENTS, 2 TO ENTER DATA FOR OUT-\n"
              << "PATIENTS, OR 3 TO QUIT!\n";
         cin >> menuChoice;

         catchInfiniteLoop();
      }

      /* Get: The patient data while menu choice is not quit */
      if (menuChoice != QUIT)
      {
         switch (menuChoice)
         {
            case INPATIENT_BILLING_SYSTEM:
              
               /* Display: Patient type, Patient name */
               cout << "\nBilling Information for "
                    << "In-Patient: Jenny Elf" << endl;

               /* Get: Billing Information */
               cout << "\nDuration of stay: ";
               cin >> numDays;

               /* While the input is un-Com Passionable, the following
               message will be displayed */
               while (numDays < 0)
               {
                  cout << "\nCAN NOT CO(M)PUTE - CHANGING REALITY!\n"
                       << "ENTRY FOR DURATION OF STAY IS REPEA(t)ED: ";
                  cin >> numDays;

                  catchInfiniteLoop();
               }

               cout << "Daily rate for this patient: X$ ";
               cin >> dailyRate;

               /* While the input is un-Com Passionable, the following
               message will be displayed */
               while (dailyRate < 0)
               {
                  cout << "\nCAN NOT CO(M)PUTE - CHANGING REALITY!\n"
                       << "ENTRY FOR DAILY RATE IS REPEA(t)ED: ";
                  cin >> dailyRate;

                  catchInfiniteLoop();
               }

               cout << "Medication charges for this patient X$: ";
               cin >> medCharges;

               /* While the input is un-Com Passionable, the following
               message will be displayed    */
               while (medCharges < 0)
               {
                  cout << "\nCAN NOT CO(M)PUTE - CHANGING REALITY!\n"
                     << "ENTRY FOR MEDICATION CHARGES IS REPEA(t)ED: ";
                  cin >> medCharges;

                  catchInfiniteLoop();
               }

               cout << "Hospital service charges X$: ";
               cin >> serviceCharges;

               /* While the input is un-Com Passionable, the following
               message will be displayed    */
               while (serviceCharges < 0)
               {
                  cout << "\nCAN NOT CO(M)PUTE - CHANGING REALITY!\n"
                       << "ENTRY FOR SERVICE CHARGES IS REPEA(t)ED: ";
                  cin >> serviceCharges;

                  catchInfiniteLoop();
               }

               /* Call: getPatientData */
               totalCharges = getPatientData(numDays, dailyRate,
                              medCharges, serviceCharges);

               /* Set up: Numeric output format */
               cout << fixed << showpoint << setprecision(2);

               /* Modification::Open: File "InPatientBillingReport.txt" */
               inPatientBillingReport.open("inPatientBillingReport.txt");

               /* Modification::Error handling: If the file was successfully
                                created, the report will be written to the file */
               if (inPatientBillingReport)
               {
                  /* Output: Write the formatted final billing data to file */
                  inPatientBillingReport
                     << "\n\t\tTotal Charges For Treatment Of Jenny Elf\n"
                     << "\t\t----------------------------------------\n\n"
                     << "Days Of Stay: "
                     << setw(42) << right << numDays
                     << "\nMedication Charges:"
                     << "\nDaily Rate:"
                     << setw(37) << right << "X$ "
                     << setw(8) << right << dailyRate
                     << "\nMedication Charges:"
                     << setw(29) << right << "X$ "
                     << setw(8) << right << medCharges
                     << "\nService Charges:"
                     << setw(32) << right << "X$ "
                     << setw(8) << right << serviceCharges
                     << "\n\t\t----------------------------------------\n"
                     << "Total Charges:"
                     << setw(34) << right << "X$ "
                     << setw(8) << right << totalCharges;

                  /* Close: File "InPatientReport.txt" if data was successfully written */
                  cout << "\nCOM PASSION HAS WRITTEN DATA TO FILE. CL(O)SING FIL(e).\n\n";
                  inPatientBillingReport.close();

                  cout << "\n\nN(e)Xt!\n\n";
               }
               else
               {
                  cout << "\nAn error occured. COM PASSION could not save data!\n"
                     << "Make sure neither the file 'InPatientBillingReport.txt'\n"
                     << "nor the folder the report is stored in is write protected,\n"
                     << "so that the data can be written!\n\n"
                     << "CHANGING REALITY!\n\n";
               }
               break;

            case OUTPATIENT_BILLING_SYSTEM:

               /* Display: Patient type, Patient name */
               cout << "\nBilling Information for "
                    << "Out-Patient: Che Centaur" << endl;

               /* Get: Billing Information */
               cout << "\nMedical charges for this patient X$: ";
               cin >> medCharges;

               /* While the input is un-Com Passionable, the following
               message will be displayed    */
               while (medCharges < 0)
               {
                  cout << "\nCAN NOT CO(M)PUTE - CHANGING REALITY!\n"
                       << "ENTRY FOR MEDICATION CHARGES IS REPEA(t)ED: ";
                  cin >> medCharges;

                  catchInfiniteLoop();
               }

               cout << "Hospital service charges X$: ";
               cin >> serviceCharges;

               /* While the input is un-Com Passionable, the following
                  message will be displayed    */
               while (serviceCharges < 0)
               {
                  cout << "\nCAN NOT CO(M)PUTE - CHANGING REALITY!\n"
                       << "ENTRY FOR SERVICE CHARGES IS REPEA(t)ED: ";
                  cin >> serviceCharges;

                  catchInfiniteLoop();
               }

               /* Call: getPatientData */
               totalCharges = getPatientData(medCharges, serviceCharges);

               /* Set up: Numeric output format */
               cout << fixed << showpoint << setprecision(2);

               /* Modification::Open: File "OutpatientReport.txt" */
               outPatientBillingReport.open("OutpatientBillingReport.txt");

               /* Error handling: If the file was successfully created, the
                                  report will be written to the file */
               if (outPatientBillingReport)
               {
                  /* Output: Write the formatted final billing data to file */
                  outPatientBillingReport
                     << "\n\t\tTotal Charges For Treatment Of Che Centaur\n"
                     << "\t\t------------------------------------------\n"
                     << "\nMedication Charges:"
                     << setw(31) << right << "X$ "
                     << setw(8) << right << medCharges
                     << "\nService Charges:"
                     << setw(34) << right << "X$ "
                     << setw(8) << right << serviceCharges
                     << "\n\t\t------------------------------------------\n"
                     << "Total Charges:"
                     << setw(36) << right << "X$ "
                     << setw(8) << right << totalCharges;

                  /* Close: File "OutpatientReport.txt" if data was successfully
                            written */
                  cout << "\nCOM PASSION HAS WRITTEN DATA TO FILE.\n"
                       << "CL(O)SING FIL(e).\n\n";
                  outPatientBillingReport.close();

                  cout << "\n\nN(e)Xt!\n\n";
               }
               else
               {
                  cout << "\nAn error occured. COM PASSION could not save data!\n"
                       << "Make sure neither the file 'OutPatientBillingReport.txt'\n"
                       << "nor the folder the report is stored in is write protected,\n"
                       << "so that the data can be written!\n\n"
                       << "CHANGING REALITY!\n\n";
               }
               break;
         }
      }

      if (menuChoice == QUIT)
      {
         cout << "\nOh Cruel World! Another Patient Survived, Ent Left ...\n";
         cin.ignore();
      }
   } while (menuChoice != QUIT);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getPatientData

   This overloaded function accepts the following in-patient
   information as arguments:
  
   * The number of days spent in the hospital
   * The daily rate
   * Hospital medication charges
   * Charges for the hospital services (lab tests, etc.)
  
   It calculates and returns the total charges for the in-
   patients stay.
   ********************************************************** */

double getPatientData(int numDays, double dailyRate, double medCharges,
                      double serviceCharges)
{
   /* Variable: Total patient charges */
   double inpatientChargesTotal = 0.0;

   /* Calculate: The total charges */
   inpatientChargesTotal = (dailyRate * numDays) +
                           (medCharges + serviceCharges);

   /* Return: Total charges */
   return inpatientChargesTotal;
}

/* **********************************************************
   Definition: getPatientData

   This overloaded function accepts the following out-patient
   information as arguments:
  
   * Hospital medication charges
   * Charges for the hospital services (lab tests, etc.)
  
   It calculates and returns the total charges for the in-
   patients stay.
   ********************************************************** */

double getPatientData(double medCharges, double serviceCharges)
{
   /* Variable: Total patient charges */
   double outpatientChargesTotal = 0.0;

   /* Calculate: Total charges */
   outpatientChargesTotal = (medCharges + serviceCharges);

   /* Return: Total charges */
   return outpatientChargesTotal;
}

Example Output



No comments:

Post a Comment