Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A company has six salespeople. Every month, they go on road trips to sell the companys product. At the end of each month, the total

A company has six salespeople. Every month, they go on road trips to sell the companys product. At the end of each month, the total sales for a salesperson in that month, together with that salespersons ID and the month, is recorded in a file. At the end of each year, the manager of the company wants to see an annual sales report. Due to the amount of data involved, hes like to be able to view the report by ID or by total sales.

Write a program to load id data from salesID.txt, sales data from salesData.txt (unknown # of records. The records are not in any order of sales id or month), calculate the total sales of each person in a year, and display a report. Shell code is provided in HW8_SalesReport_shell.cpp. You need understand the shell code and then add:

ADD CODE #1: declaration of two functions sortByID and showReport. Also include:

o Function prologue comment

o /*IN*/, /*OUT*/, /*INOUT*/ comments to each function parameter.

o Pre- and Post- condition comments for each function.

ADD CODE #2: complete function loadData

ADD CODE #3: implementation of two functions sortByID and showReport

#include #include // std::ifstream #include // std::string #include // std::setw(), std::fixed, std::setprecision()

//---------------------------------------------- // global declarations //----------------------------------------------

const int MAX = 10; // max # of sales person

struct salesPersonRec { std::string ID; // salesperson's ID double totalSales; // salesperson's yearly sales amount };

//---------------------------------------------- // function declarations //----------------------------------------------

// read id data into array and set totalSales of each record to 0 bool loadID(salesPersonRec list[]/*OUT*/, int listSize/*IN*/); // Pre: listSize specified // Post: listSize # of ids read in from file and saved in the ID column in the first listSize records of list. totalSales of those records set to 0

// read in sales data and calculate total sales per person bool loadData(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/); // Pre: listSize specified, list contains valid ids and totalSales set to 0 for the first listSize records // Post: sales data read from file // totalSales of the first listSize records are set to accumulated sales of the salesperson with the given id

// display menu on screen void showMenu(); // Pre: none // Post: menu choices printed on screen

// display id and yearly sales amout of all salespersons by id order void showReportByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/); // Pre: listSize specified, list contains at least listSize number of records // Post: the first listSize records in list are reordered so they are now in ascending order of the id column // id and totalSales column of the first listSize records in list are printed on screen

// display id and yearly sales amout of all salespersons by total sales order void showReportBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/); // Pre: listSize specified, list contains at least listSize number of records // Post: the first listSize records in list are reordered so they are now in ascending order of the total sales column // id and totalSales column of the first listSize records in list are printed on screen

// sort sales person records by total sales amount void sortBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/); // Pre: listSize specified, list contains at least listSize number of records // Post: the first listSize records in list are reordered so they are now in ascending order of the totalSales column

// ADD CODE #1: two function declarations

// END ADD CODE #1

//----------------------------------------------

int main() { salesPersonRec salesPersonList[MAX]; // array to hold the salesperson's data int numOfSalesPerson = 6;

if (!loadID(salesPersonList, numOfSalesPerson)) // load id data return 1;

if (!loadData(salesPersonList, numOfSalesPerson)) // load sales data return 1;

// user interaction via menu int option = 0; // user option do { // display menu showMenu();

// user option std::cin >> option;

switch (option) { case 1: // sort and display report by id showReportByID(salesPersonList, numOfSalesPerson); break; case 2: // sort and display report by sales showReportBySales(salesPersonList, numOfSalesPerson); break; case 0: // exit std::cout << " Thank you for using our reporting system! "; break; default: // invalid input std::cout << "invalid choice. Please try again. "; break; } } while (option != 0);

} // end main

//---------------------------------------------- // Function Implementation //----------------------------------------------

// read id data into array and set totalSales of each record to 0 bool loadID(salesPersonRec list[]/*OUT*/, int listSize/*IN*/) { std::ifstream inFile; // input file std::string idfilename = "salesID.txt";

// open id file inFile.open(idfilename); if (inFile.fail()) { std::cerr << "Error: can't open input file \"" << idfilename << "\". Abort. "; // report to cerr return false; // and end now }

// read in id data for (int index = 0; index < listSize; index++) { if (!(inFile >> list[index].ID)) // get salesperson's ID return false; // reading failed

list[index].totalSales = 0.0; } inFile.close(); // close file

return true; } //end loadID

//----------------------------------------------

// read in sales data and calculate total sales of each person bool loadData(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/) { std::ifstream inFile; // input file std::string datafilename = "salesData.txt";

// open sales data inFile.open(datafilename); if (inFile.fail()) { std::cerr << "Error: can't open input file \"" << datafilename << "\". Abort. "; // report to cerr return false; // and end now }

// read in data and process std::string id; int month; double amount;

// ADD CODE #2: // read sales data from file, // calculate and update totalSales of each record

// END ADD CODE #2

inFile.close(); // close data file

return true; } //end loadData

//----------------------------------------------

// display menu on screen void showMenu() { const std::string menuOptions[] = { "Options:", "1. Display sales report by sales id.", "2. Display sales report by annual sales amount.", "0. Exit", "Choose (0 ~ 2): " }; int menuLen = sizeof(menuOptions) / sizeof(std::string);

std::cout << std::endl; for (int i = 0; i < menuLen - 1; i++) std::cout << menuOptions[i] << std::endl; std::cout << menuOptions[menuLen - 1]; // last item } // end getUserOption

//----------------------------------------------

void showReportByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/) { sortByID(list, listSize); showReport(list, listSize); } // end showReportByID

//----------------------------------------------

void showReportBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/) { sortBySales(list, listSize); showReport(list, listSize); } // end showReportBySales

//----------------------------------------------

void sortBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/) { // insertion sort for (int i = 1; i < listSize; i++) { // find spot to insert [i] salesPersonRec hold = list[i]; double amount = list[i].totalSales; int indexToInsert = i - 1; while (indexToInsert >= 0 && amount < list[indexToInsert].totalSales) { // shift list[indexToInsert + 1] = list[indexToInsert]; indexToInsert--; } // insert list[indexToInsert + 1] = hold; } } // end sortBySales

//----------------------------------------------

// ADD CODE #3: implementation of two functions

// END ADD CODE #3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

8P6 Perform the indicated calculation.

Answered: 1 week ago

Question

3. Identify the four characteristics of popular culture.

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago