/****************************************************/
/*                                                  */
/*   CSCI 121: Principles of Computer Programming   */
/*   Programming Project #4                         */
/*   Due Wednesday, February 9                      */
/*   Programmed by Ian Giese                        */
/*                                                  */
/****************************************************/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
    int accNum;
    char custName[21], accType;
    double prevMeter, currMeter, amountOwed, energyUsed, tempValue;
    double totUsage = 0, totOwed = 0;
    ifstream input("Assign4.txt");
    ofstream output("BillingReport.log");
    output << right << setw(62) << "Lake Side Power - Billing Report" << endl
           << endl << "Account   " << left << setw(23) << "Customer" << setw(13)
           << "Type" << "Current   Previous   Usage    Bill" << endl << "-------"
           << "   --------------------   ----------   -------   --------  "
           << " ------   -------" << endl;
    while (!input.eof())
    {
          input >> accNum >> ws;
          input.getline(custName, 20);
          input >> prevMeter >> currMeter >> accType >> ws;
          if (currMeter < prevMeter)
             energyUsed = (100000.0 - prevMeter) + currMeter;
          else
             energyUsed = currMeter - prevMeter;
          if (accType == 'R')
          {
             if (energyUsed > 300)
             {
                if (energyUsed > 600)
                {
                   if (energyUsed > 1000)
                   {
                      tempValue = energyUsed - 1000;
                      amountOwed = 75 + (.05 * tempValue);
                   }
                   else
                   {
                      tempValue = energyUsed - 600;
                      amountOwed = 51 + (.06 * tempValue);
                   }
                }
                else
                {
                   tempValue = energyUsed - 300;
                   amountOwed = 27 + (.08 * tempValue);
                }
             }
             else
             {
                amountOwed = .09 * energyUsed;
             }
          }
          else if (accType == 'S')
          {
             if (energyUsed > 500)
             {
                tempValue = energyUsed - 500;
                amountOwed = 42.5 + (.07 * tempValue);
             }
             else
             {
                amountOwed = .085 * energyUsed;
             }
          }
          else if (accType == 'C')
          {
             if (energyUsed > 1000)
             {
                if (energyUsed > 1500)
                {
                   tempValue = energyUsed - 1500;
                   amountOwed = 107.5 + (.045 * tempValue);
                }
                else
                {
                   tempValue = energyUsed - 1000;
                   amountOwed = 75 + (.065 * tempValue);
                }
             }
             else
             {
                amountOwed = .075 * energyUsed;
             }
          }
          output << " " << left << setw(9) << accNum << setw(23) << custName
                 << setw(13);
                 
          if (accType == 'R')
             output << "Regular";
          else if (accType == 'S')
             output << "Senior";
          else if (accType == 'C')
             output << "Commercial";
          
          output << right << fixed << showpoint
                 << setprecision(1) << setw(7) << currMeter << setw(11)
                 << prevMeter << setw(9) << energyUsed << setprecision(2)
                 << setw(10) << amountOwed << endl;
          totUsage += energyUsed;
          totOwed += amountOwed;
    }
    output << endl << "Total Usage:   " << totUsage << endl
           << "Total Billing: " << totOwed;
    return 0;
}

