In: Computer Science
C++ Program
A company pays its salespeople on a commission basis. The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period. For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21. Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points. There are 12 salesmen for the company. The input data file is "SalesPerson SalesV3.txt". It contains the salesperson number (3 digit id) followed by his sales. Each salesperson has numerous sales listed for the period. You will need to keep up with the total number of sales and of course the total sales per salesman.
Output :
1.
Print out the Sales report showing the salesmen ID, his total
number of sales, his total
sales
and his average per sale. Show the report in sorted
ordered by Sales ID.
2. Print out the same report but this time print out in sorted ordered by total sales.
3. Print out the same report again sorted on average sales.
The company is looking at a reduction in sales force. So I need to know which salesman had the smallest total sales. Print out the worsted salesman, his total sales and percentage of his sales compared to the second worsted salesman. (Just to see how off his sales really are compared to one other. ie did he have a bad sales period or he is really a bad salesman.)
SalesPerson SalesV3.txt
322 10.80
848 920.00
828 1267.00
848 8320.00
229 66330.00
254 6670.00
689 520.00
691 4880.00
828 3860.00
848 2820.00
229 7848.00
828 60.00
848 820.00
229 8115.00
546 1280.00
828 660.00
848 320.00
190 325.00
828 263.00
848 9315.00
828 3860.00
848 2093.00
322 4225.00
254 960.00
689 220.00
691 436.00
322 4210.00
689 520.00
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
bool mysort(pair<int,float> p1, pair<int,float>
p2){
return p1.second > p2.second;
}
struct Salesman{
int id = 0;
int total_sales = 0;
float sales_amount = 0;
float average = 0;
int getId(){return id;}
int getTotalSales(){return total_sales;}
float getSalesAmount(){return sales_amount;}
float getAverage(){return average;}
void setId(int id){id = id;}
void setTotalSales(int sales){
total_sales = sales;
}
void setSalesAmount(float amount){
sales_amount = amount;
}
void setAverage(float avg){
average = avg;
}
};
struct find_salesman :
unary_function<Salesman,bool>{
int id;
find_salesman(int id) : id(id) {}
bool operator () ( const Salesman& s ) const{
return s.id == id;
}
};
bool sort1(Salesman s1, Salesman s2){
return s1.getId() < s2.getId();
}
bool sort2(Salesman s1, Salesman s2){
return s1.getTotalSales() > s2.getTotalSales();
}
bool sort3(Salesman s1, Salesman s2){
return s1.getAverage() > s2.getAverage();
}
int main(){
// Reading the sales report
string line;
ifstream myfile("Sales Person SalesV3.txt");
vector<Salesman> salesmen(1000);
map<int,float> sales_stats;
// This
map will store Salesman Id vs His Total Sales amount
map<int,int> sales_count;
// This
map will store Salesman Id vs No. of Sales
vector<int> s1, s2;
if(myfile.is_open()){
while(getline(myfile,line)){
istringstream
iss(line); //
Reading each record line
int val =
0;
// Storing
Employee ID
float stat =
0;
// Storing
Employee Sale Amount
string s;
int count =
0;
while(getline(iss,s,' ')){
if(count==0)
// Count 0 signifies 1st
value of map, which is employee Id
val = stoi(s);
if(count==1)
// Count 1 signifies 2nd
value of map, which is Sales Amount
stat = stof(s);
count++;
}
auto it = find_if(salesmen.begin(), salesmen.end(),
find_salesman(val));
if(it==salesmen.end()){
Salesman salesman = {val,1,stat,stat};
// The 3rd parameter can be changed to 250 + 0.11*stat (For
required case)
salesmen.push_back(salesman);
}
else{
it->setId(val);
it->setTotalSales(it->getTotalSales()+1);
it->setSalesAmount(it->getSalesAmount() + stat);
it->setAverage(it->getSalesAmount()/it->getTotalSales());
}
}
myfile.close();
}
else{
cout<<"\nUnable to open file
or file doesn't exist";
}
// Printing 1st Report - Ordered by Salesman ID
sort(salesmen.begin(),salesmen.end(),sort1);
cout<<"\nReport 1 Ordered By Salesman
ID\n";
cout<<"\n ID | Sales | Total Sales | Average per Sale
|";
for(auto it=salesmen.begin();it!=salesmen.end();it++){
if(it->getId()!=0){
cout<<"\n
"<<it->getId()<<" |
"<<(it->total_sales)<<"\t|\t"
<<(it->sales_amount)<<"\t|\t"<<(it->average)<<"\t
|";
}
}
sort(salesmen.begin(),salesmen.end(),sort2);
// Printing 2nd Report - Ordered by Total
Sales
cout<<"\n\nReport 2 Ordered by Total Number of
Sales\n";
cout<<"\n ID | Sales | Total Sales | Average per Sale
|";
for(auto it=salesmen.begin();it!=salesmen.end();it++){
if(it->getId()!=0){
cout<<"\n
"<<it->getId()<<" |
"<<(it->total_sales)<<"\t|\t"
<<(it->sales_amount)<<"\t|\t"<<(it->average)<<"\t
|";
}
}
// Printing 3rd Report - Ordered by Average
Sales
sort(salesmen.begin(),salesmen.end(),sort3);
cout<<"\n\nReport 3 - Ordered by
Average\n";
cout<<"\n ID | Sales | Total Sales | Average per Sale
|";
for(auto it=salesmen.begin();it!=salesmen.end();it++){
if(it->getId()!=0){
cout<<"\n
"<<it->getId()<<" |
"<<(it->total_sales)<<"\t|\t"
<<(it->sales_amount)<<"\t|\t"<<(it->average)<<"\t
|";
}
}
}