Array of objects, Bubble Sort, Binary Search

This is my first time posting here and I am doing so because I need a little help with an assignment I have for school..I am not asking for the answer but I would greatly appreciate a few hints to get me going in the right direction because after a while my brain just got overloaded and I got VERY confused....
I have done the first part of the question but I am having trouble with number 2..

This is the assignment:
1. 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. (4 points)
 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.

 TotalAnnualSales Commission percentage
 Under $10,000 No commission
 $10,000-$20,000 2% of TotalAnnualSales
 Greater than $20,000 4% of TotalAnnualSales
Demonstrate the class in a program that creates a sales person object, and then calls each of the functions in the class.
2. 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..




I am done with the first part of the question but I am having trouble making an array of objects....(How do i make my code look cool like it does in my compiler?)

[ #include <string>
#include <iostream>
#include <fstream>

using namespace std;

//Class declaration
class SalesPerson
{
//private data members of class
private:
string EID;
string FirstName;
string LastName;
string Date;
double TotalAnnualSales;

public:
//default constructor
SalesPerson();
//constructor
SalesPerson(string eid, string firstname, string lastname, string date, double totalannualsales);
//accessors
string getEID();
string getFirstName();
string getLastName();
string getDate();
double getTotalAnnualSales();
int nameSearch();

//mutators
void setEID(string eid);
void setFirstName(string firstname);
void setLastName(string lastname);
void setDate(string date);
void setTotalAnnualSales(double totalannualsales);

//function to calculate comission
double calculateCommission();
void bubbleSort(string FirstName[], const int SIZE);
int binarySearch(const string FirstName[], int size, string nameSearch);
};//end of class declaration


//class member definitions
SalesPerson::SalesPerson()
{
}
SalesPerson::SalesPerson(string eid, string firstname, string lastname, string date, double totalannualsales)
{
EID = eid;
FirstName = firstname;
LastName = lastname;
Date = date;
TotalAnnualSales = totalannualsales;
}
string SalesPerson::getEID()
{
return EID;
}
string SalesPerson::getFirstName()
{
return FirstName;
}
string SalesPerson::getLastName()
{
return LastName;
}

string SalesPerson::getDate()
{
return Date;
}
double SalesPerson::getTotalAnnualSales()
{
return TotalAnnualSales;
}
void SalesPerson::setEID(string eid)
{
EID = eid;
}
void SalesPerson::setFirstName(string firstname)
{
FirstName = firstname;
}
void SalesPerson::setLastName(string lastname)
{
LastName = lastname;
}
void SalesPerson::setDate(string date)
{
Date = date;
}
void SalesPerson::setTotalAnnualSales(double totalannualsales)
{
TotalAnnualSales = totalannualsales;
}

//calculate commission
double SalesPerson::calculateCommission()
{
double commission = 0;
if (TotalAnnualSales < 10000)
commission = 0;
else if (TotalAnnualSales <=20000)
commission = .02 * TotalAnnualSales;
else
commission = .04 * TotalAnnualSales;
return commission;
}

//main start
int main ()
{
//declare variables
string EID;
string FirstName;
string LastName;
string Date;
string nameSearch;
int foundLocation;
double TotalAnnualSales;
double commission;
const int size=5;

//create an array for the objects
SalesPerson employees[size];

ofstream outputFile;
outputFile.open("work.txt");


for ( int i=0; i < size; i++)
{
//prompt user for salesperson information
cout<< "Enter sales person ID: "<<endl;
outputFile<< "Enter sales person ID: "<<endl;
cin>>EID;
cout<< "Enter first name: "<<endl;
outputFile<< "Enter first name: "<<endl;
cin>>FirstName;
cout<< "Enter last name: "<<endl;
outputFile << "Enter last name: "<<endl;
cin>>LastName;
cout<< "Enter date: "<<endl;
outputFile<< "Enter date: "<<endl;
cin>>Date;
cout << "Enter total annual sales: "<<endl;
cin >> TotalAnnualSales;

//create object of sales person class
SalesPerson person(EID, FirstName, LastName, Date, TotalAnnualSales);

//call function to calculate commission
commission = person.calculateCommission();



cout<<" Commission earned for "<<employees[i].getLastName()<<" "<<employees[i].getFirstName()<< " is "<<commission<<endl;

}

//ask user for information
cout<< " Please enter the person's name that you wish to find in my name data base "<<endl;
outputFile<<" Please enter the person's name that you wish to find in my name data base "<<endl;
// cin>>nameSearch;
getline(cin,nameSearch);


//call functions
bubbleSort(FirstName, size);


foundLocation=binarySearch(FirstName , size, nameSearch);

if (foundLocation==-1)
{
cout<<nameSearch<<"is not in the array"<<endl;
}
else
cout<<nameSearch<<" is found in position "<<foundLocation<<endl;




system ("pause");
return 0;
}


void SalesPerson::bubbleSort(string FirstName[], const int size)
{
SalesPerson temp;
bool swap;

do
{ swap=false;
for (int count=0; count < (size-1); count++)
{
if(FirstName[count] > FirstName[count + 1])
{
temp=FirstName[count];
FirstName[count]=FirstName [count +1];
FirstName [count + 1];
swap=true;

}

}while (swap);
}


}

//this function preforms the binary search
int SalesPerson::binarySearch(const string employees[], int size, string nameSearch)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;

while (!found && first <= last)
{
middle = (first + last) / 2;
if (FirstName[middle] == nameSearch)
{
found = true;
position = middle;
}
else if (FirstName[middle] > nameSearch)
last = middle - 1;
else
first = middle + 1;
}
return position;
} ]
Last edited on
Topic archived. No new replies allowed.