help Rewriting the Programming ( will rate ) 1.using arrays, and user defined si
ID: 3771514 • Letter: H
Question
help Rewriting the Programming ( will rate )
1.using arrays, and user defined simple data types to hold customer data.
2.Your input will come from a text file of at least 15 customers
3. Input file format - customerType accountNumber premiumChannels ( i.e residential example: R12345 5 , business example B12345 16 8 )
4. Precision should be two decimal places
5. Calculate the running average for residential and business customer spending
6. Print all customer's bill to a single file and the end of the file you should have the average summary for each customer type.
code
#include <iostream>
#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;
const double RES_COST_PREM_CHANNEL = 7.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;
int main()
{
//Variable declaration
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double amountDue;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "This program computes a cable "
<< "bill." << endl;
cout << "Enter account number (an integer): ";
cin >> accountNumber;
cout << endl;
cout << "Enter customer type: "
<< "R or r (Residential), "
<< "B or b (Business): ";
cin >> customerType;
cout << endl;
switch (customerType)
{
case 'r':
case 'R':
cout << "Enter the number"
<< " of premium channels: ";
cin >> numOfPremChannels;
cout << endl;
amountDue = RES_BILL_PROC_FEES
+ RES_BASIC_SERV_COST
+ numOfPremChannels *
RES_COST_PREM_CHANNEL;
cout << "Account number: "
<< accountNumber
<< endl;
cout << "Amount due: $"
<< amountDue
<< endl;
break;
case 'b':
case 'B':
cout << "Enter the number of basic "
<< "service connections: ";
cin >> numOfBasicServConn;
cout << endl;
cout << "Enter the number"
<< " of premium channels: ";
cin >> numOfPremChannels;
cout << endl;
if (numOfBasicServConn<= 10)
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
else
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ (numOfBasicServConn - 10) *
BUS_BASIC_CONN_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
cout << "Account number: "
<< accountNumber << endl;
cout << "Amount due: $" << amountDue
<< endl;
break;
default:
cout << "Invalid customer type." << endl;
}//end switch
return 0;
}
Explanation / Answer
Main.cpp
#include <iostream>
#include <iomanip>
#include<fstream>
#include <string>
#include <sstream>
using namespace std;
//Named constants – residential customers
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
const double RES_COST_PREM_CHANNEL = 7.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;
int main()
{
//Variable declaration
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double amountDue;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "This program computes a cable " << "bill." << endl;
double businessTotalBill = 0;
double residentTotalBill = 0;
int b = 0, r = 0;
ifstream infile("C:/Users/Z001LF5/Desktop/bank2.txt");//Use complete address of your input file here
if (!infile.is_open())
{
cout << "Error opening file ";
}
else
{
string command;
ofstream myfile("C:/Users/Z001LF5/Desktop/bill.txt");//Use complete address of your output file here
while (getline(infile, command))
{
stringstream ssin(command);
string first;
ssin >> first;
if (first == "R" || first == "r")
{
r++;
string premiumChannels;
ssin >> accountNumber >> premiumChannels;
numOfPremChannels = stoi(premiumChannels);
amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numOfPremChannels *RES_COST_PREM_CHANNEL;
residentTotalBill += amountDue;
if (myfile.is_open())
{
myfile << accountNumber << " " << amountDue;
myfile << ' ';
}
}
else if (first == "B" || first == "b")
{
b++;
string basicServConn;
string premiumChannels;
ssin >> accountNumber >> basicServConn >> premiumChannels;
numOfBasicServConn = stoi(basicServConn);
numOfPremChannels = stoi(premiumChannels);
if (numOfBasicServConn <= 10)
{
amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + numOfPremChannels *BUS_COST_PREM_CHANNEL;
}
else
{
amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numOfBasicServConn - 10) *BUS_BASIC_CONN_COST + numOfPremChannels *BUS_COST_PREM_CHANNEL;
}
businessTotalBill += amountDue;
if (myfile.is_open())
{
myfile << accountNumber << " " << amountDue;
myfile << ' ';
}
}
else
{
if (myfile.is_open())
{
myfile << "Invalid Customer Type";
myfile << ' ';
}
myfile.close();
}
}
if (r != 0)
{
double residentAvg = residentTotalBill / r;
if (myfile.is_open())
{
myfile << "Residential Avg Bill" << " " << residentTotalBill;
myfile << ' ';
}
}
if (b!=0)
{
double businessAvg = businessTotalBill / b;
if (myfile.is_open())
{
myfile << "Business Type Avg Bill" << " " << businessTotalBill;
myfile << ' ';
}
}
myfile.close();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.