In: Computer Science
Program for C++
A company has six salespeople. Every month, they go on road trips to sell the company’s product. At the end of each month, the total sales for a salesperson in that month, together with that salesperson’s 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, he’s like to be able to view the report by ID or by total sales.
Create 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:
You’re not supposed to modify the given code otherwise
salesID.txt
32214
23422
57373
35864
54654
12345
salesData.txt
12345 1 893
32214 1 343
23422 3 903
57373 2 893
35864 5 329
54654 9 392
12345 2 999
32214 4 892
23422 4 895
23422 2 492
57373 6 892
35864 10 1223
54654 11 3420
12345 12 322
35864 5 892
54654 3 893
12345 8 494
32214 8 9023
23422 6 223
23422 4 783
57373 8 8834
35864 3 2882
HW8_SalesReport_shell.cpp
// HW8_SalesReport_shell.cpp (shell code. need ADD CODE #1 ~
#3)
// by Bin "Crystal" Peng, CS225
// generate sales report using monthly sales data
// fixed # of sales people; unknown # of sales record
#include <iostream>
#include <fstream> // std::ifstream
#include <string> // std::string
#include <iomanip> // 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 << "\nThank you for using our reporting
system!\n";
break;
default: // invalid input
std::cout << "invalid choice. Please try again.\n";
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. \n"; // 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. \n"; // 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
HW8_SalesReport_shell.cpp
// Add code is bold text
//===================================================
#include <iostream>
#include <fstream> // std::ifstream
#include <string> // std::string
#include <iomanip> // 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
// sort sales person records by ID of salesperson
void sortByID(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
// display id and yearly sales amout of all
salespersons
void showReport(salesPersonRec list[]/*IN*/, int
listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number
of records
// Post: first listSize records in list are printed on screen with
salesperson id and totalSales column
// 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
<< "\nThank you for using our reporting system!\n";
break;
default: // invalid input
std::cout
<< "invalid choice. Please try again.\n";
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. \n";
// 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. \n";
// 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
while (!inFile.eof()) // checking
for end of file
{
if (!(inFile >> id)) // get
salesperson's ID
return false; //
reading failed
if (!(inFile >>
month)) // get salesperson's month
return false; //
reading failed
if (!(inFile >>
amount)) // get salesperson's sales amount for that month
return false; //
reading failed
for (int index = 0;
index < listSize; index++) // for loop for searching id in
list
{
if
(list[index].ID == id)
{
list[index].totalSales += amount; // adding
amount for that salesperson
break;
}
}
}
// 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
void sortByID(salesPersonRec list[]/*INOUT*/, int
listSize/*IN*/)
{
// insertion sort
for (int i = 1; i < listSize; i++)
{
// find spot to insert [i]
salesPersonRec hold =
list[i];
std::string id = list[i].ID;
int indexToInsert = i - 1;
while (indexToInsert >= 0
&& (strcmp(id.c_str(),
(list[indexToInsert].ID).c_str())<0))
{ // strcmp(s1,s2) compare char*
return 0 if equal , negative if s1<s2 and positive if
s1>s2
//c_str() convert string to char*
// shift
list[indexToInsert + 1] = list[indexToInsert];
indexToInsert--;
}
// insert
list[indexToInsert + 1] =
hold;
}
} // end of sortByID
//----------------------------------------------
void showReport(salesPersonRec list[]/*IN*/, int
listSize/*IN*/)
{
std::cout << "Salesperson Id\t | \tTotal Sales"
<< std::endl;
for (int index = 0; index < listSize; index++) //
index for iterating over first listSize ids
{
std::cout <<
" " <<list[index].ID << "\t | \t "
<< list[index].totalSales << std::endl;
// extra
spaces for alignment in table
}
} //end of showReport
//----------------------------------------------
// END ADD CODE #3
//===================================================
Sample input files 1:
salesID.txt
32214
23422
57373
35864
54654
12345
salesData.txt
12345 1 893
32214 1 343
23422 3 903
57373 2 893
35864 5 329
54654 9 392
12345 2 999
32214 4 892
23422 4 895
23422 2 492
57373 6 892
35864 10 1223
54654 11 3420
12345 12 322
35864 5 892
54654 3 893
12345 8 494
32214 8 9023
23422 6 223
23422 4 783
57373 8 8834
35864 3 2882
Sample output 1:
Sample input files 2:
salesID.txt
A123
B245
A745
B874
7451
7964
salesData.txt
A123 1 400
B245 1 200
A745 1 100
B874 1 240
7451 1 300
7964 1 150
A123 2 700
B245 3 500
A745 4 100
B874 5 240
7451 6 100
7964 7 250
Sample output for input 2: