1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
//Program: Cable Company Billing
//This program reads an input file comprised of local customers for a cable
//company. The program then calculates the customer's bill and outputs the
//bill to a file. The program processes three types of customers: business,
//residential, and employee.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
//Named constants - residential customers
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
//Named constants - business customers
const double BUS_BILL_PROC_FEES = 15.00;
const double BUS_BASIC_SERV_COST = 75.00;
const double BUS_BASIC_CONN_COST = 5.00;
const double BUS_COST_PREM_CHANNEL = 50.00;
//Named constants - employee customers
const double EMP_BILL_PROC_FEES = 4.50;
const double EMP_BASIC_SERV_COST = RES_BASIC_SERV_COST * 0.8;
//Named constant - maximum customers
const int MAX_CUSTOMERS = 100;
struct Customer
{
Customer() : customerType(' '), numOfBasicServConn(0),
numOfPremChannels(0), amountDue(0.0) {
}
char customerType;
int numOfBasicServConn, numOfPremChannels;
double amountDue;
string accountNumber;
};
int read_File(istream& fin, Customer cust[]);
void compute_amountDue(Customer cust[], int numOfCustomers);
void output(Customer cust[], int numOfCustomers, ostream& os);
int main()
{
Customer cust[MAX_CUSTOMERS];
ifstream fin("inFile.txt");
if (!fin)
{
cerr << "File failed to open!\n";
return (1);
}
int numOfCustomers = read_File(fin, cust);
compute_amountDue(cust, numOfCustomers);
ofstream os("outFile.txt");
if (!os)
{
cerr << "Could not open output file!\n";
return (1);
}
output(cust, numOfCustomers, os);
cin.ignore(10000, '\n');
cin.get();
return 0;
}
int read_File(istream& fin, Customer cust[])
{
int x = 0;
while (x < MAX_CUSTOMERS && fin >> cust[x].customerType) {
if (cust[x].customerType == 'B')
fin >> cust[x].accountNumber >> cust[x].numOfBasicServConn >> cust[x].numOfPremChannels;
else if (cust[x].customerType == 'R')
fin >> cust[x].accountNumber >> cust[x].numOfBasicServConn;
else if (cust[x].customerType == 'E')
fin >> cust[x].accountNumber;
x++;
}
return x;
}
void compute_amountDue(Customer cust[], int numOfCustomers)
{
for (int i = 0; i < numOfCustomers; i++)
{
if (cust[i].customerType == 'B') {
cust[i].amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST
+ cust[i].numOfPremChannels * BUS_COST_PREM_CHANNEL;
if (cust[i].numOfBasicServConn >= 10)
cust[i].amountDue += (cust[i].numOfBasicServConn - 10) *
BUS_BASIC_CONN_COST;
}
else if (cust[i].customerType == 'R')
cust[i].amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST;
else if (cust[i].customerType == 'E')
cust[i].amountDue = EMP_BILL_PROC_FEES + (RES_BASIC_SERV_COST * 0.8);
}
}
void output(Customer cust[], int numOfCustomers, ostream& os)
{
for (int i = 0; i < numOfCustomers; ++i) {
os << cust[i].customerType << " " << cust[i].accountNumber << " "
<< cust[i].numOfBasicServConn << " " << cust[i].numOfPremChannels << " "
<< fixed << setprecision(2) << '$' << cust[i].amountDue << '\n';
}
}
|