Do not use arrays for this project. Please show the code and output (see example
ID: 3596734 • Letter: D
Question
Do not use arrays for this project. Please show the code and output (see example output ).
You must utilize all functions listed:
bool isBookFiction(char fiction)
double getFee(int quantity, char genre, bool isFiction)
double calcBookSalesAndTax(double price, int quantity, double &taxTotal)
string setGenre(char genre)
void printoutput(ofstream &outPutFile, string customerID, string bookTitle, string bookGenre,
bool isFiction, int quantity, double price, double subTotal, double taxTotal, double extraFee, double total)
void displayTotals(ofstream &outPutFile, double totalSales, int totalOrders)
Each line will contain the following information about the book:
Customer ID
Title
Author
ISBN
Price
Quantity
Fiction/Non-Fiction
Genre
Data:
1234 Once_Upon_A_Time New_Author 1234323456787 25.00 4 N M
2345 Midnight_Moon Margaret_Brown 3456789765432 50.00 10 F D
3456 A_Wrinkle_In_Time Madeline_Engle 2535483215987 60.00 4 N M
4567 Harry_Potter J_K_Rowling 0002569854712 100.00 100 F D
5678 Charlottes_Web E_B_White 036250125478 25.00 12 F M
6789 The_Snowy_Day Ezra_Keats 00025523148 50.00 5 N R
Additional Requirements:
Assume the tax on the total price of the books is 7%.
Shipping fee: If the quantity is over 20 Fee $50, 15-19: Fee $40, 10-14: Fee $30, 5-9: Fee $20, less than 5 Fee $10.
Use and if/else or switch to determine the fee.
Tax should not be calculated on the fees.
A Boolean variable should be used to hold whether the book is Fiction or Non-Fiction.
Use an if/else if statement to display the genre in the output.
Do not apply fee to non-fiction romance.
Accumulate total sales (without tax and fees) of all invoices for this batch (file).
Accumulate total number of book orders in this batch.
Display the average book order sale.
Take this project one step at a time:
Loop through and read the data into variables.
Within your loop, next process a single line file.
Call isBookFiction sending it the char received from the file and set your isFiction variable
Call getFree to set your fee variable, all logic to determine fee is this single function
Call calcBookSales and Tax, this MUST be a function that uses both a byValue and byRef parameter, it does not matter is sales(just quantity*price) is byRef or taxTotal (quantity*price*.07) is byRef
f)Call setGenre to set the char from the file to a variable that holds the word (e.g. send it r or R for romance-string bookGenre = setGenre(‘r’); - would result in bookGenre = “Romance”
Call a void function printOutput to print the output to the file, send your output file handler byRef. Send all the information you need to print each individual invoice.
Still within your loop accumulate your total variables (keep total book sales and the number of book sales (lines))
Once your loop is complete call void displayTotals to write the total book sales and average book order.
Pseudo code:
Includes
Function prototypes
Main:
Declare variables
Open file handlers
While not end of file
Read a single line and store information to variables
Call functions to set all variables
Accumulate the total sales and number of book orders (a book order is one line in the file)
Write invoice information for that one line to the file
Move to next line in file
End while
Write totals to file (total sales and average book order)
Close files
Function bodies
Example Output:
Invoice
Customer ID: 1234
Once_Upon_A_Time Fiction Mystery 4@25.00 Subtotal: 100.00
Total Book Sales: 100.00
Tax: 7.00
Subtotal: 107.00
Extra Fee: 10.00
Total: 117.00
_____________________________________________________________________________________
_____________________________________________________________________________________________
Invoice
Customer ID: 2345
Midnight_Moon Fiction Drama 10@50.00 Subtotal: 500.00
Total Book Sales: 500.00
Tax: 35.00
Subtotal: 535.00
Extra Fee: 30.00
Total: 565.00
_____________________________________________________________________________________
Invoice
Customer ID: 2345
Midnight_Moon Fiction Drama 10@50.00 Subtotal: 500.00
Total Book Sales: 500.00
Tax: 35.00
Subtotal: 535.00
Extra Fee: 30.00
Total: 565.00
_____________________________________________________________________________________
REPORT TOTALS:
Total Book Sales: 11390.00
Average Book Order: 1898.33
____________________________________________________________________________________
Note: Show the next customer…Complete for all separate Customer IDs.
Explanation / Answer
Given below is the code for the question with output. Please don't forget to rate the answer if it helpd. Thank you.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
bool isBookFiction(char fiction);
double getFee(int quantity, char genre, bool isFiction);
double calcBookSalesAndTax(double price, int quantity, double &taxTotal);
string setGenre(char genre);
void printoutput(ofstream &outPutFile, string customerID, string bookTitle, string bookGenre,
bool isFiction, int quantity, double price, double subTotal, double taxTotal, double extraFee, double total);
void displayTotals(ofstream &outPutFile, double totalSales, int totalOrders);
int main()
{
int qty, totalOrders = 0;
string custID, title, author, isbn;
double price, fee, totalSales = 0 ;
string fiction, genre;
string filename, genreStr;
cout << "Enter input filename: ";
cin >> filename;
ifstream ifs(filename.c_str());
if(!ifs.is_open())
{
cout << "Could not open input file " << filename << endl;
return 1;
}
string outFilename;
cout << "Enter output filename: ";
cin >> outFilename;
ofstream outFile(outFilename.c_str());
//read 1st record
ifs >> custID >> title >> author >> isbn >> price >> qty >> fiction >> genre ;
while(true)
{
//cout << custID << endl;
if(ifs.eof())
break;
bool isFiction = isBookFiction(fiction[0]);
fee = getFee(qty, genre[0], isFiction);
genre = setGenre(genre[0]);
double tax, sales;
sales = calcBookSalesAndTax(price, qty, tax);
printoutput(outFile, custID, title, genreStr, isFiction, qty, price, sales, tax, fee, sales + tax + fee);
totalSales += sales ;
totalOrders++;
ifs >> custID >> title >> author >> isbn >> price >> qty >> fiction >> genre ;
}
ifs.close();
displayTotals(outFile, totalSales, totalOrders);
outFile.close();
cout << "Output written to file " << outFilename << endl;
}
bool isBookFiction(char fiction)
{
if(fiction =='f' || fiction == 'F')
return true;
else
return false;
}
double getFee(int quantity, char genre, bool isFiction)
{
if(!isFiction && (genre == 'R' || genre == 'r'))
return 0;
else
{
if(quantity >= 20) //20 and above
return 50;
else if(quantity >= 15) //15 - 19
return 40;
else if(quantity >= 10) //10 - 14
return 30;
else if(quantity >= 5) // 5 - 9
return 20;
else
return 10;
}
}
double calcBookSalesAndTax(double price, int quantity, double &taxTotal)
{
double sales = price * quantity;
taxTotal = 0.07 * sales;
return sales;
}
string setGenre(char genre)
{
if(genre == 'r' || genre == 'R')
return "Romance";
else if(genre == 'm' || genre == 'M')
return "Mystery";
else if(genre == 'd' || genre == 'D')
return "Drama";
else
return to_string(genre);
}
void printoutput(ofstream &outPutFile, string customerID, string bookTitle, string bookGenre,
bool isFiction, int quantity, double price, double subTotal, double taxTotal, double extraFee, double total)
{
outPutFile << setprecision(2) << fixed;
outPutFile << "Invoice" << endl;
outPutFile << "Customer ID: " << customerID << endl;
outPutFile << left << setw(50) << bookTitle << " " << setw(10) << bookGenre << " " <<
quantity << "@" << price << " " << "Subtotal: " << subTotal << endl;
outPutFile << "Total Book Sales: " << subTotal << endl;
outPutFile << "Tax: " << taxTotal << endl;
subTotal += taxTotal;
outPutFile << "Subtotal: " << subTotal << endl;
outPutFile << "Extra Fee: " << extraFee << endl;
outPutFile << "Total: " << total << endl;
outPutFile << "------------------------------------------------------" << endl << endl;
}
void displayTotals(ofstream &outPutFile, double totalSales, int totalOrders)
{
outPutFile << "REPORT TOTALS:" << endl;
outPutFile << "Total Book Sales: " << totalSales << endl;
outPutFile << "Average Book Order: " << totalSales / totalOrders << endl;
}
input file :books.txt
1234 Once_Upon_A_Time New_Author 1234323456787 25.00 4 N M
2345 Midnight_Moon Margaret_Brown 3456789765432 50.00 10 F D
3456 A_Wrinkle_In_Time Madeline_Engle 2535483215987 60.00 4 N M
4567 Harry_Potter J_K_Rowling 0002569854712 100.00 100 F D
5678 Charlottes_Web E_B_White 036250125478 25.00 12 F M
6789 The_Snowy_Day Ezra_Keats 00025523148 50.00 5 N R
ouput file: report.txt
Invoice
Customer ID: 1234
Once_Upon_A_Time 4@25.00 Subtotal: 100.00
Total Book Sales: 100.00
Tax: 7.00
Subtotal: 107.00
Extra Fee: 10.00
Total: 117.00
------------------------------------------------------
Invoice
Customer ID: 2345
Midnight_Moon 10@50.00 Subtotal: 500.00
Total Book Sales: 500.00
Tax: 35.00
Subtotal: 535.00
Extra Fee: 30.00
Total: 565.00
------------------------------------------------------
Invoice
Customer ID: 3456
A_Wrinkle_In_Time 4@60.00 Subtotal: 240.00
Total Book Sales: 240.00
Tax: 16.80
Subtotal: 256.80
Extra Fee: 10.00
Total: 266.80
------------------------------------------------------
Invoice
Customer ID: 4567
Harry_Potter 100@100.00 Subtotal: 10000.00
Total Book Sales: 10000.00
Tax: 700.00
Subtotal: 10700.00
Extra Fee: 50.00
Total: 10750.00
------------------------------------------------------
Invoice
Customer ID: 5678
Charlottes_Web 12@25.00 Subtotal: 300.00
Total Book Sales: 300.00
Tax: 21.00
Subtotal: 321.00
Extra Fee: 30.00
Total: 351.00
------------------------------------------------------
Invoice
Customer ID: 6789
The_Snowy_Day 5@50.00 Subtotal: 250.00
Total Book Sales: 250.00
Tax: 17.50
Subtotal: 267.50
Extra Fee: 0.00
Total: 267.50
------------------------------------------------------
REPORT TOTALS:
Total Book Sales: 11390.00
Average Book Order: 1898.33
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.