Question: 7.15 Lab: Sales Class - Array of Sales objects (overload operators) Reuse the Sales class with the following modifications: Overload the stream insertion operator (
7.15 Lab: Sales Class - Array of Sales objects (overload operators)
Reuse the Sales class with the following modifications:
Overload the stream insertion operator (<<) and use it to display reports and write data to file using the same format (see below).
Overload the relational operator (<) and use it to sort the array in ascending order by name (insertion sort)
This program will create an array of 30 Sales objects and it will read data from an input file (Sales.txt) into this array. It will sort the array in ascending order by name. Then it will calculate the average of all sales people and display on the screen the average sale followed by the names of the salespeople with above average sales, the amount sold and the amount earned.
Average Sales: $4411.86 Salespeople with above average sales: Doe, Mary Jane 5001 262.55 East, Linda 15000 900.00 Newman, Dan 5500 288.75 Peterson, William 14200 852.00 Smith, Victor 7995 419.74 West, Paul 5000 225.00
If the list is empty, write "N/A" as shown below:
Average Sales: $9125 Salespeople with above average sales: N/A
Finally, it will write to another file (salesReport.txt) a table as shown below:
Sales Report ==================== ============= ============= Name Amount Sold Amount Earned ==================== ============= ============= Baker, Tom 1300 58.50 Doe, Mary Jane 5001 262.55 . . . ==================== ============= ============= Number of salespeople: 5
Display the amount earned with 2 decimals (out << setprecision(2) << fixed;). Assume that a name has at most 20 characters (for formatting).
out << setw(20) << left << name << " "; out << setw(9) << right << amountSold << " "; out << setw(13) << right << amountEarned << endl;
Write the following stand-alone functions (each function solves a specific part of the problem):
readSalesData() insertSort() calcSalesAvg() displayOverAvg() writeReport()
On each line in the input file there are four items: id, year, name, and amount sold as shown below:
20192785 2017 North, Jane; 10000 19278520 2012 South, Tim; 12000
Prompt the user to enter the name of the input file (with extension). Generate the name of the output file by adding the word "Report" to the input file's name.
If the input file name is sales.txt, the output file name will be salesReport.txt If the input file name is newSales.txt, the output file name will be newSalesReport.txt Display the output file's name as shown below:
Report saved in: salesReport.txt
If the user enters the incorrect name for the input file, display the following message and terminate the program:
Input file: sales.txt not found!
*********************************
in main.cpp
void showReport(string fileName);
int main() { Sales salesArr[MAX_SIZE]; int size = 0; string fileName; cout << "Please enter the input file's name: "; getline(cin, fileName); readData(fileName, salesArr, size); insertSort(salesArr, size); double avg = calcSalesAvg(salesArr, size); displayOverAvg(salesArr, size, avg); writeReport(salesArr, size, fileName); string option; cout << "Show report?" << endl; getline(cin, option); if (option == "Y" || option == "y") showReport(fileName);
return 0; }
/* Write your code here: function definitions */ /* This function receives the name of a file and displays its contents to the screen.
*/ void showReport(string fileName) { fileName.insert(fileName.find("."), "Report"); ifstream in(fileName); if (in.fail()) { cout << "Input file: " << fileName << " not found!" << endl; exit(EXIT_FAILURE); } string line; while (getline(in, line)) { cout << line << endl; } in.close(); }
*******************
in Sales.h
/* Specification file for the Sales class - Overloaded stream insertion operator (<<) - Overloaded relational operator (<) */
#ifndef SALES_H #define SALES_H #include
using std::ostream; using std::string; // ^^^ avoid adding using namespace std;
class Sales { private: /* Write your code here*/ public: // constructors /* Write your code here*/ // setters /* Write your code here*/ // getters /* Write your code here*/ // other functions /* Write your code here*/ // overloaded operators /* Write your code here*/ // Overloaded < /* Write your code here*/ // Overloaded << };
#endif
*****************
in Sales.cpp
/* Implementation file for the Sales class. */
#include "Sales.h" #include
/* Write your code here */
********************
According to the zybooks, I need help to test these files to check it if they produce the right answers especially in the format.Thanks!
sale.txt Y
sales.txt N
sales.txt Y
newSales.txt n
newSales.txt y
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
