In: Computer Science
Overview
In this assignment, you will write a program to track monthly sales data for a small company.
Input
Input for this program will come from two different disk files. Your program will read from these files by explicitly opening them.
The seller file (sellers.txt) consists of an unknown number of records, each representing one sale. Records consist of a last name, a first name, a seller ID, and a sales total. So, for example, the first few records in the file look like this:
Smith John SMI902 1205.78 Jones Ann JON374 3825.85 Martinez Roger MAR723 2783.32 ...
The sales file (sales.txt) consists of an unknown number of records, each representing one sale. Records consist of a seller ID followed by a sale amount. So, for example, the first few records in the file look like this:
GLE726 914.23 HEL834 307.34 SHU837 192.34 ...
Both files may be downloaded to your computer from Blackboard.
The Seller structure
The Seller structure represents information about a salesperson. You should declare this structure type near the top of your program, after using namespace std;, but before your function prototypes. The Seller should have the following members:
A first name (string)
A last name (string)
A seller ID (string)
A sales total (double)
Processing Requirements
Your main() function is (as usual) the boss. In this function, declare the following variables:
Seller sellerArray[30]; int size;
main() should
call buildSellerArray() to read the seller records; the effective size of the array that is returned should be saved and then passed to the remaining functions
call sortSellersByID() to sort the seller array in ascending order by the seller ID
call printSellerArray() to print the contents of the seller array
call processSales() to read and process the sales transaction records
call printSellerArray() to print the updated contents of the seller array
Other Functions
int buildSellerArray(Seller sellerArray[]) - This function will be called to read the seller data into the seller array.
Use the same technique you used for Assignment 5 to open the seller file (sellers.txt) and read the data for each record. Copy the data into an element of the seller array. Once you've read all the data, return the number of seller records read into the array.
void printSellerArray(Seller sellerArray[], int size) - This function should print the contents of the seller array formatted into columns with appropriate headers (see the sample output).
void sortSellersByID(Seller sellerArray[], int size) - This function should sort the seller array in ascending order by the seller ID using the selection sort algorithm covered in class.
int findSellerID(Seller sellerArray[], int size, string searchID) - This function will be called to find the position of the seller array element with a seller ID that matches searchID. The algorithm used by this function is linear or sequential search, and is outlined below.
int i; for (i = 0; i < size; i++) { if (searchID is equal to the id member of sellerArray[i]) return i; } return -1;
void processSales(Seller sellerArray[], int size) -
This method will read a series of sales records, each containing a last name, a first name, and a sales amount. For each sales record, the method should search for the seller name and, if found, should add the sales amount to the sales total for that seller. If a seller name is not found, an error message should be printed.
Pseudocode for the method logic is given below.
ifstream inFile; string id; double salesAmount; Open inFile for the file sales.txt Check for successful open Read id from inFile while (not end of file) { Read salesAmount from inFile int index = findSellerID(sellerArray, size, id); if (index == -1) Print an error message else { Add the salesAmount to the sales total for the Seller at element index of the seller array print id and salesAmount } Read id from inFile } Close input file
Output
The program should produce a report similar to the following:
Seller Listing Name Seller ID Sales Billick, Lance BIL912 783.33 Burruel, Erik BUR091 4839.12 Burdge, Clayton BUR146 101.35 Caraveo, Lorrie CAR026 1019.45 Christoff, Ted CHR728 1204.95 Dauber, Tabatha DAU083 8349.55 Deforge, Nannie DEF201 8934.43 Glessner, Mathew GLE726 3943.32 Helberg, John HEL834 3320.99 Hoops, Allan HOO737 874.97 Jones, Ann JON374 3825.85 Limas, Kelly LIM529 7493.15 Martucci, Cody MAR389 3847.90 Marciniak, Hugh MAR423 7374.23 Martinez, Roger MAR723 2783.32 Monroig, Harriett MON283 1938.02 Oestreich, Darryl OES873 1994.55 Piekarski, Neil PIE129 5562.44 Rollman, Ken ROL333 914.97 Shuttleworth, Penelope SHU837 3921.29 Smith, John SMI902 1205.78 Sugg, Louisa SUG762 8373.22 Tekulve, Hillary TEK711 2392.34 Sales Transactions Seller ID Sale Amount GLE726 914.23 HEL834 307.34 SHU837 192.34 BUR146 410.35 ROL333 1400.34 CAR026 519.65 JON374 342.78 BUR146 431.35 MAR723 286.42 LIM529 793.41 GLE727 Error - ID not found MON283 1382.50 DAU083 349.65 TEK711 392.34 BIL912 1736.38 DEF201 921.48 CAR026 1239.54 BUR146 1201.32 CHR728 204.55 MAR423 332.44 HOO737 537.79 MAR389 384.20 BUR091 639.62 MAR423 374.23 MON283 1228.52 LIM529 493.15 SHU837 321.29 SUG762 313.21 BUR146 131.35 Seller Listing Name Seller ID Sales Billick, Lance BIL912 2519.71 Burruel, Erik BUR091 5478.74 Burdge, Clayton BUR146 2275.72 Caraveo, Lorrie CAR026 2778.64 Christoff, Ted CHR728 1409.50 Dauber, Tabatha DAU083 8699.20 Deforge, Nannie DEF201 9855.91 Glessner, Mathew GLE726 4857.55 Helberg, John HEL834 3628.33 Hoops, Allan HOO737 1412.76 Jones, Ann JON374 4168.63 Limas, Kelly LIM529 8779.71 Martucci, Cody MAR389 4232.10 Marciniak, Hugh MAR423 8080.90 Martinez, Roger MAR723 3069.74 Monroig, Harriett MON283 4549.04 Oestreich, Darryl OES873 1994.55 Piekarski, Neil PIE129 5562.44 Rollman, Ken ROL333 2315.31 Shuttleworth, Penelope SHU837 4434.92 Smith, John SMI902 1205.78 Sugg, Louisa SUG762 8686.43 Tekulve, Hillary TEK711 2784.68
Other Notes
Smith John SMI902 1205.78 Jones Ann JON374 3825.85 Martinez Roger MAR723 2783.32 Monroig Harriett MON283 1938.02 Hoops Allan HOO737 874.97 Martucci Cody MAR389 3847.90 Burruel Erik BUR091 4839.12 Limas Kelly LIM529 7493.15 Billick Lance BIL912 783.33 Deforge Nannie DEF201 8934.43 Christoff Ted CHR728 1204.95 Helberg John HEL834 3320.99 Rollman Ken ROL333 914.97 Caraveo Lorrie CAR026 1019.45 Shuttleworth Penelope SHU837 3921.29 Marciniak Hugh MAR423 7374.23 Dauber Tabatha DAU083 8349.55 Tekulve Hillary TEK711 2392.34 Oestreich Darryl OES873 1994.55 Glessner Mathew GLE726 3943.32 Piekarski Neil PIE129 5562.44 Sugg Louisa SUG762 8373.22 Burdge Clayton BUR146 101.35
---------------------------
GLE726 914.23 HEL834 307.34 SHU837 192.34 BUR146 410.35 ROL333 1400.34 CAR026 519.65 JON374 342.78 BUR146 431.35 MAR723 286.42 LIM529 793.41 GLE727 340.35 MON283 1382.50 DAU083 349.65 TEK711 392.34 BIL912 1736.38 DEF201 921.48 CAR026 1239.54 BUR146 1201.32 CHR728 204.55 MAR423 332.44 HOO737 537.79 MAR389 384.20 BUR091 639.62 MAR423 374.23 MON283 1228.52 LIM529 493.15 SHU837 321.29 SUG762 313.21 BUR146 131.35
-----------------------------
Given below is the code for the question and the output . Please don't forget to rate the answer if it helped. Thank you.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
typedef struct
{
string firstname;
string lastname;
string sellerID;
double sales;
}Seller;
int buildSellerArray(Seller sellers[]);
void sortSellersByID(Seller sellers[], int size);
void printSellerArray(Seller sellers[], int size);
void processSales(Seller sellers[], int size);
int findSellerID(Seller sellerArray[], int size, string
searchID);
int main()
{
int size;
Seller sellers[30];
cout << fixed << setprecision(2);
size = buildSellerArray(sellers);
sortSellersByID(sellers, size);
printSellerArray(sellers, size);
processSales(sellers, size);
cout << "after processing sales ..." << endl;
printSellerArray(sellers, size);
return 0;
}
int buildSellerArray(Seller sellers[])
{
ifstream infile("sellers.txt");
if(!infile.is_open())
{
cout << "Could not open sellers.txt" << endl;
exit(1);
}
cout << "loading sellers.txt ..." << endl;
int size = 0;
while(infile >> sellers[size].lastname)
{
infile >> sellers[size].firstname ;
infile >> sellers[size].sellerID;
infile >> sellers[size].sales;
size++;
}
infile.close();
return size;
}
//sort by ids using selection sort
void sortSellersByID(Seller sellers[], int size)
{
cout << "sorting the sellers by Id ..." << endl;
int minIdx;
for(int i = 0; i < size; i++)
{
minIdx = i;
for(int j = i + 1; j < size; j++)
{
if(sellers[j].sellerID < sellers[minIdx].sellerID)
minIdx = j;
}
if(minIdx != i)
{
Seller temp = sellers[i];
sellers[i] = sellers[minIdx];
sellers[minIdx] = temp;
}
}
}
void printSellerArray(Seller sellers[], int size)
{
string name;
cout << setw(25) << left << "Name" <<
setw(15) << "Seller Id" << setw(10) << right
<< "Sales" << endl;
for(int i = 0; i < size; i++)
{
name = sellers[i].lastname + ", " + sellers[i].firstname;
cout << setw(25) << left << name <<
setw(15) << sellers[i].sellerID << setw(10) <<
right << sellers[i].sales << endl;
}
cout << endl;
}
void processSales(Seller sellers[], int size)
{
ifstream infile("sales.txt");
if(!infile.is_open())
{
cout << "Could not open sales.txt" << endl;
return;
}
cout << "processing sales.txt ..." << endl;
string sellerId;
double amount;
cout << left << setw(15) << "Seller Id" <<
right << setw(10) << "Sale Amount" << endl;
while(infile >> sellerId)
{
infile >> amount;
int idx = findSellerID(sellers, size, sellerId);
if(idx == -1)
cout << left << setw(15) << sellerId <<
right << setw(10) << "Error - ID not found" <<
endl;
else
{
sellers[idx].sales += amount;
cout << left << setw(15) << sellerId <<
right << setw(10) << amount << endl;
}
}
infile.close();
cout << endl;
}
int findSellerID(Seller sellers[], int size, string searchID)
{
for(int i = 0; i < size; i++)
{
if(sellers[i].sellerID == searchID)
return i;
}
return -1;
}
input file sellers.txt
Smith John SMI902 1205.78
Jones Ann JON374 3825.85
Martinez Roger MAR723 2783.32
Monroig Harriett MON283 1938.02
Hoops Allan HOO737 874.97
Martucci Cody MAR389 3847.90
Burruel Erik BUR091 4839.12
Limas Kelly LIM529 7493.15
Billick Lance BIL912 783.33
Deforge Nannie DEF201 8934.43
Christoff Ted CHR728 1204.95
Helberg John HEL834 3320.99
Rollman Ken ROL333 914.97
Caraveo Lorrie CAR026 1019.45
Shuttleworth Penelope SHU837 3921.29
Marciniak Hugh MAR423 7374.23
Dauber Tabatha DAU083 8349.55
Tekulve Hillary TEK711 2392.34
Oestreich Darryl OES873 1994.55
Glessner Mathew GLE726 3943.32
Piekarski Neil PIE129 5562.44
Sugg Louisa SUG762 8373.22
Burdge Clayton BUR146 101.35
input file : sales.txt
GLE726 914.23
HEL834 307.34
SHU837 192.34
BUR146 410.35
ROL333 1400.34
CAR026 519.65
JON374 342.78
BUR146 431.35
MAR723 286.42
LIM529 793.41
GLE727 340.35
MON283 1382.50
DAU083 349.65
TEK711 392.34
BIL912 1736.38
DEF201 921.48
CAR026 1239.54
BUR146 1201.32
CHR728 204.55
MAR423 332.44
HOO737 537.79
MAR389 384.20
BUR091 639.62
MAR423 374.23
MON283 1228.52
LIM529 493.15
SHU837 321.29
SUG762 313.21
BUR146 131.35
output
loading sellers.txt ...
sorting the sellers by Id ...
Name Seller Id Sales
Billick, Lance BIL912 783.33
Burruel, Erik BUR091 4839.12
Burdge, Clayton BUR146 101.35
Caraveo, Lorrie CAR026 1019.45
Christoff, Ted CHR728 1204.95
Dauber, Tabatha DAU083 8349.55
Deforge, Nannie DEF201 8934.43
Glessner, Mathew GLE726 3943.32
Helberg, John HEL834 3320.99
Hoops, Allan HOO737 874.97
Jones, Ann JON374 3825.85
Limas, Kelly LIM529 7493.15
Martucci, Cody MAR389 3847.90
Marciniak, Hugh MAR423 7374.23
Martinez, Roger MAR723 2783.32
Monroig, Harriett MON283 1938.02
Oestreich, Darryl OES873 1994.55
Piekarski, Neil PIE129 5562.44
Rollman, Ken ROL333 914.97
Shuttleworth, Penelope SHU837 3921.29
Smith, John SMI902 1205.78
Sugg, Louisa SUG762 8373.22
Tekulve, Hillary TEK711 2392.34
processing sales.txt ...
Seller Id Sale Amount
GLE726 914.23
HEL834 307.34
SHU837 192.34
BUR146 410.35
ROL333 1400.34
CAR026 519.65
JON374 342.78
BUR146 431.35
MAR723 286.42
LIM529 793.41
GLE727 Error - ID not found
MON283 1382.50
DAU083 349.65
TEK711 392.34
BIL912 1736.38
DEF201 921.48
CAR026 1239.54
BUR146 1201.32
CHR728 204.55
MAR423 332.44
HOO737 537.79
MAR389 384.20
BUR091 639.62
MAR423 374.23
MON283 1228.52
LIM529 493.15
SHU837 321.29
SUG762 313.21
BUR146 131.35
after processing sales ...
Name Seller Id Sales
Billick, Lance BIL912 2519.71
Burruel, Erik BUR091 5478.74
Burdge, Clayton BUR146 2275.72
Caraveo, Lorrie CAR026 2778.64
Christoff, Ted CHR728 1409.50
Dauber, Tabatha DAU083 8699.20
Deforge, Nannie DEF201 9855.91
Glessner, Mathew GLE726 4857.55
Helberg, John HEL834 3628.33
Hoops, Allan HOO737 1412.76
Jones, Ann JON374 4168.63
Limas, Kelly LIM529 8779.71
Martucci, Cody MAR389 4232.10
Marciniak, Hugh MAR423 8080.90
Martinez, Roger MAR723 3069.74
Monroig, Harriett MON283 4549.04
Oestreich, Darryl OES873 1994.55
Piekarski, Neil PIE129 5562.44
Rollman, Ken ROL333 2315.31
Shuttleworth, Penelope SHU837 4434.92
Smith, John SMI902 1205.78
Sugg, Louisa SUG762 8686.43
Tekulve, Hillary TEK711 2784.68