(C++ program) Design a class for a sales person that has the following members v
ID: 3725427 • Letter: #
Question
(C++ program)
Design a class for a sales person that has the following members variable:
--EID: To holds the sales person’s employment ID
--Firstname: To holds the sales person’s first name.
--Lastname: To holds the sales person’s last name.
--Date: To holds the sales person’s birth date
--TotalAnnualSales: To holds the sales person’s total sales in a year.
In addition, the class should have the following member functions.
--Constructor: The constructor should accept the sales person’s data as arguments and assign these values to the object’s member variables.
--Mutators: Appropriate mutator functions should be created to allow values to be set for each object’s data members.
--Accessors: Appropriate accessor functions should be created to allow values to be retrieved from an object’s data members.
--calculateCommission: This function should calculate and display the commission amount for a sales person based on his totalAnnualSales. See below
Demonstrate the class in a program that creates a sales person object, and then calls each of the functions in the class.
--With the sales person class design in question #1 above in hand, write a program
--To have an array of sales person objects. The program should read and store information for each sales person in each of the object. It should then call a class function, once for each object, to display the sales person’s ToalAnnualSales amount and commission amount.
--This program should use bubble sort to sort the array of sales person objects. It places sales person objects in ascending order by their first name.
--Demonstrate the binary search method in this program to search by first name.
TotalAnnualSales Commission Percentage Under $10,000 No commission $10,000-$20,000 2% of TotalAnnualSales Greater than $20,000 4% of TotalAnnualSales
Explanation / Answer
#include <iostream>
using namespace std;
class SalesPerson
{
private:
int eid;
string firstName;
string lastName;
string date;
int totalAnnualSales;
public:
SalesPerson(){
}
SalesPerson(int eid,string firstName,string lastName,string date,int totalAnnualSales){
this->eid = eid;
this->firstName = firstName;
this->lastName = lastName;
this->date = date;
this->totalAnnualSales = totalAnnualSales;
}
int getEid(){
return eid;
}
void setEid(int eid){
this->eid = eid;
}
string getFirstName(){
return firstName;
}
void setFirstName(string firstName){
this->firstName = firstName;
}
string getLastName(){
return lastName;
}
void setLastName(string lastName){
this->lastName = lastName;
}
string getDate(){
return date;
}
void setDate(string date){
this->date = date;
}
int getTotalAnnualSales(){
return totalAnnualSales;
}
void setTotalAnnualSales(int totalAnnualSales){
this->totalAnnualSales = totalAnnualSales;
}
void calculateCommission(){
float commission;
if(totalAnnualSales<10000){
commission = 0;
}
if(totalAnnualSales>=10000 && totalAnnualSales<20000){
commission = 2*totalAnnualSales/100;
}
if(totalAnnualSales>=20000){
commission = 4*totalAnnualSales/100;
}
cout<<"Commission of employee with id "<<eid<<" is : $"<<commission<<endl;
}
};
void swap(SalesPerson &sp1, SalesPerson &sp2){
int temp;
string temp1;
temp = sp1.getEid();
sp1.setEid(sp2.getEid());
sp2.setEid(temp);
temp1 = sp1.getFirstName();
sp1.setFirstName(sp2.getFirstName());
sp2.setFirstName(temp1);
temp1 = sp1.getLastName();
sp1.setLastName(sp2.getLastName());
sp2.setLastName(temp1);
temp1 = sp1.getDate();
sp1.setDate(sp2.getDate());
sp2.setDate(temp1);
temp = sp1.getTotalAnnualSales();
sp1.setTotalAnnualSales(sp2.getTotalAnnualSales());
sp2.setTotalAnnualSales(temp);
}
int binarySearch(SalesPerson arr[], int l, int r, string x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
// If the element is present at the middle
// itself
if (arr[mid].getFirstName() == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid].getFirstName() > x)
return binarySearch(arr, l, mid-1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid+1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
int main(){
int n;
cout<<"Enter number of sales person objects: ";
cin>>n;
SalesPerson objArr[n];
for(int i=0;i<n;i++){
int eid,totalAnnualSales;
string firstName,lastName,date;
cout<<"Enter eid: ";
cin>>eid;
cout<<"Enter first name: ";
cin>>firstName;
cout <<"Enter second name: ";
cin>>lastName;
cout<<"Enter date :";
cin>>date;
cout<<"Enter total annual sales: ";
objArr[i].setEid(eid);
objArr[i].setFirstName(firstName);
objArr[i].setLastName(lastName);
objArr[i].setDate(date);
objArr[i].setTotalAnnualSales(totalAnnualSales);
}
//printing values
for(int i=0;i<n;i++){
cout<<"Emp id:"<<objArr[i].getEid()<<endl;
cout<<"Total Annual Sales: "<<objArr[i].getTotalAnnualSales()<<endl;
objArr[i].calculateCommission();
}
//sorting based on first name
for(int i=0;i<n;i++){
for(int j = i+1;j<n;j++){
if(objArr[i].getFirstName()>objArr[j].getFirstName()){
swap(objArr[i],objArr[j]);
}
}
}
//binary search
string firstName;
cout<<"Enter firstName to be searched: ";
cin>>firstName;
int res = binarySearch(objArr, 0, n-1, firstName);
if(res==-1){
cout<<"Not Found";
}
else{
cout<<"Found at poition: "<<res;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.