Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview For this assignment, modify the Seller class from program 9 and modify

ID: 3555765 • Letter: O

Question

Overview

For this assignment, modify the Seller class from program 9 and modify some of the code from program 8 so that it will work with an array of class objects rather than an array of structures.

Program 8

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct seller
{
string lastName;
string firstName;
string sellerID;
double salesTotal;
};
int buildSellerArray(seller sellerArray[]);
void sortSellerArray(seller sellerArray[], int arSize);
void printSellerArray(seller sellerArray[], int arSize);
void processSales(seller sellerArray[], int arSize);
int findSellerID(seller sellerAssay[], int arSize, string searchID);

int main()
{
seller sellerArray[30];

int arSize;
char searchID;

arSize=buildSellerArray(sellerArray);
sortSellerArray(sellerArray, arSize);
printSellerArray(sellerArray, arSize);
processSales(sellerArray, arSize);

printSellerArray(sellerArray, arSize);
  
}
/************************************************
Funtion:   Build Seller Array

Use:       Read sellers list and use data to fill a
           structured array and count and return how
           many seller's information infromation is
           added.

Arguments:   sellerArray[]

return:       i
************************************************/
int buildSellerArray(seller sellerArray[])
{
ifstream infile;
infile.open("sellers8.txt");
if (!infile)
   {
   cout<<"Seller file did not open"<<endl;
   exit(-1);
   }
int i =0;
infile>>sellerArray[i].lastName;
   while(infile)//loop created to read through text file.
   {
   infile>>sellerArray[i].firstName;
   infile>>sellerArray[i].sellerID;
   infile>>sellerArray[i].salesTotal;
   i++;
   infile>>sellerArray[i].lastName;
   }
return i;
}
/************************************************
Funtion:   Sort seller array

Use:       Takes seller array structure and sorts data by
           the sellers' ID.

Arguments:   sellerArray[], arSize  

return:      
************************************************/
void sortSellerArray(seller sellerArray[], int arSize)
{
int min;
seller temp;
   for(int i=0; i<arSize; i++)
   {
   min=i;
      
       for(int j=i+1; j<arSize; j++)
       {
           if(sellerArray[j].sellerID < sellerArray[min].sellerID)
           min=j;
       }
   temp=sellerArray[i];
   sellerArray[i]=sellerArray[min];
   sellerArray[min]=temp;
   }
}
/************************************************
Funtion:   Print seller array

Use:       Prnts data in seller array structure.

Arguments:   sellerArray[], arSize

return:      
************************************************/
void printSellerArray(seller sellerArray[], int arSize)
{
cout<<setw(25)<<"Seller List"<<endl<<endl;

   for(int i=0; i<arSize; i++)
   cout<<setw(30)<<fixed<<setprecision(2)<<left<<sellerArray[i].lastName+", "+sellerArray[i].firstName<<setw(10)
//      <<sellerArray[i].firstName<<setw(15)
   <<sellerArray[i].sellerID<<setw(10)
   <<right<<sellerArray[i].salesTotal<<endl;  

cout<<endl;
}

/************************************************
Funtion:   Process sales  

Use:       Reads seller transaction file. Accepts value from
           find sller ID finction. Makes decision based on value
           received from function to decided to print a seller's
           ID with error message or sales amount. If sales amount
           is rpint sales amount in sales transactions will be
           added to the correct sellers' array structure.
          
Arguments:   sellerArray[], arSize

return:      
************************************************/
void processSales(seller sellerArray[], int arSize)
{
cout<<setw(20)<<"Seller Transactions"<<endl<<endl;
int index;
string searchID;
double salesAmount;
ifstream infile;
infile.open("sales8.txt");

infile>>searchID;
   while(infile)
   {
   infile>>salesAmount;
  
   index=findSellerID(sellerArray, arSize, searchID);
      
       if(index==-1)
       cout<<setw(10)<<searchID<<setw(10)<<"Error - ID not found"<<endl;
       else
       {
       sellerArray[index].salesTotal+=salesAmount;
       cout<<setw(15)<<left<<searchID<<setw(10)<<salesAmount<<endl;
       }
   infile>>searchID;
   }
infile.close();
cout<<endl;
}
/************************************************
Funtion:   Find seller ID

Use:       calls seller ID in variable from process sales
           function and checks if it matches a seller ID
           in the seller array structure. If there is a match
           subcript number is returned. If there is not a
           match -1 is returned.

Arguments:   sellerArray[], arSize, searchID

return:       -1, i
************************************************/
int findSellerID(seller sellerArray[], int arSize, string searchID)
{
   for(int i=0; i<arSize; i++)
   {
       if(searchID == sellerArray[i].sellerID)
       return i;
   }
return -1;
}

Program 9

#include<iostream>
#include<iomanip>
#include<string.h>

using namespace std;

class Seller{
public:
Seller();
Seller(char [], char[], char [], double);

void print();
void setFirstName(char []);
void setLastName(char []);
void setID(char []);
void setSalesTotal(double);

double getSalesTotal();

private:
char firstName[20];
char lastName[30];
char ID[7];
double salesTotal;
};

Seller::Seller(){
setFirstName("None");
setLastName("None");
setID("ZZZ000");
setSalesTotal(0);
}
Seller::Seller( char fn[], char ln[], char id[], double st){
setFirstName(fn);
setLastName(ln);
setID(id);
setSalesTotal(st);
}
void Seller::print(){
cout << lastName << ", " << firstName << setw(10) << ID << setw(10) << getSalesTotal() << endl;
}
void Seller::setFirstName(char newFirstName[]){
if(strlen(newFirstName) > 0){
strcpy(firstName, newFirstName);
}
else{
strcpy(firstName, "None");
}
}
void Seller::setLastName(char newLastName[]){
if(strlen(newLastName) > 0){
strcpy(lastName, newLastName);
}
else{
strcpy(newLastName, "None");
}
}
void Seller::setID(char newID[]){
if(strlen(newID) > 0 && strlen(newID) < 7){
strcpy(ID, newID);
}
else{
strcpy(ID, "ZZZ000");
}
}
void Seller::setSalesTotal(double newSalesTotal){
if(newSalesTotal >= 0){
salesTotal = newSalesTotal;
}
else{
salesTotal = 0;
}
}
double Seller:: getSalesTotal(){
return salesTotal;
}

int main(){
cout << fixed << setprecision(2);
Seller seller1("Jane", "Doe", "CSI240", 1234.56);
Seller seller2;
Seller seller3("", "Urkel", "TOOBIG999", 876.34);
Seller seller4("Cooper", "Winnie", "TWY88", 13579.11);
Seller seller5("Chase", "Angela", "MSCL94", 24680.24);

cout << "The first seller ";
seller1.print();
cout << endl;

cout << "The second seller ";
seller2.print();
seller2.setFirstName("Jordan");
seller2.setLastName("Catalano");
seller2.setID("MSCL96");
seller2.setSalesTotal(246.80);
seller2.print();
cout << endl;

cout << "The third seller ";
seller3.print();
seller3.setFirstName("Steve");
seller3.setID("FM1989");
seller3.print();
cout << endl;

cout << "The fourth seller ";
cout << "Sales total is $ " << seller4.getSalesTotal() << endl;
cout << endl;

cout << "The fifth seller ";
seller5.print();
cout << endl; }

Modifications to the Seller class

The following methods should be added to the class:

void increaseSalesTotal( double increaseAmount )

This method will increase a Seller's sales total by a specific amount. It takes one argument: a double that represents the amount to increase the Seller's sales total. It returns nothing.

If the passed in increaseAmount is greater than or equal to 0, it should be added to the salesTotal data member. Otherwise, an error message should be displayed and the salesTotal data member should remain unchanged.

char * getFirstName()

This method returns a Seller's first name data member. It takes no arguments.

The * is something new for the 240 class. It is used to "tell" the compiler that the method will be returning an address. This is satisfied by the method returning the name of the character array. This topic will be discussed in further detail in the 241 class :-)

char * getLastName()

This method returns a Seller's last name data member. It takes no arguments. Use the code for getFirstName as a pattern for this method.

char * getID()

This method returns a Seller's ID number. It takes no arguments. Use the code for getFirstName as a pattern for this method.

Modifications to Program 8

For this assignment, along with the methods for the Seller class, there will also be functions. They will all be taken from program 8 and will need to be modified so that they work with an array of class objects rather than an array of structures.

Make sure that the prototypes for these functions are placed *AFTER* the Seller class definition but *BEFORE* int main().

int buildSellerArray( Seller sellerArray[] )

This function will be called to read the seller data into the seller array. As in program 8, the data will be coming from an input file. For this assignment, the file is named sellers10.txt and it has the same format and data as the sellers8.txt file from program 8.

In program 8, it's possible that a bunch of assignment statements were used to "fill" the various data members of the structure. So maybe something like:

The code will need to be modified for this program because the data members for a class object will be private and therefore they cannot be directly accessed using the dot notation. Instead, one of the set access methods will have to be used. So the earlier code becomes:

void printSellerArray( Seller sellerArray[], int size )

This function should print the contents of the seller array. Unlike program 8, there will not be a cout statement to display the information for a Seller. Instead, the print method should be called for each element in the array of Sellers.

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.

The logic for this function is the same as in program 8. However, the comparison on the decision statement will have to be changed so that it will use:

int findSellerID( Seller sellerArray[], int size, char searchID[] )

This function will be called to find the position of the seller array element with a seller ID that matches searchID. Note the change to the last argument for the function.

The logic for this function is the same as in program 8. However, the comparison on the decision statement will have to be changed so that it will use:

void processSales( Seller sellerArray[], int size )

This function 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.

Again, the logic for the function will be the same as in program 8. The changes that need to made to implement the logic are:

main()

If main was written correctly in program 8, there is no need to change anything in main for this program.

Programming Notes

The two input files for the program are sellers10.txt and sales10.txt

Each method AND function must have a documentation box.

Hand in a copy of your source code using Blackboard.

Output

The output for this program is the same as the output for program 8.

Explanation / Answer

DO RATE if satisfied

Please post the second question so that i can post the same answer there too.

#include<iostream>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
#include <fstream>
using namespace std;
class Seller {
public:
Seller();
Seller(char[], char[], char[], double);
void print();
void setFirstName(char[]);
void setLastName(char[]);
void setID(char[]);
void setSalesTotal(double);
double getSalesTotal();
char* getFirstName();
char* getLastName();
char* getID();
void increaseSalesTotal( double increaseAmount );
private:
char firstName[20];
char lastName[30];
char ID[7];
double salesTotal;
};
Seller::Seller() {
setFirstName("None");
setLastName("None");
setID("ZZZ000");
setSalesTotal(0);
}
Seller::Seller(char fn[], char ln[], char id[], double st) {
setFirstName(fn);
setLastName(ln);
setID(id);
setSalesTotal(st);
}
void Seller::print() {
cout << lastName << ", " << firstName << setw(10) << ID << setw(10) << getSalesTotal() << endl;
}
void Seller::setFirstName(char newFirstName[]) {
if (strlen(newFirstName) > 0) {
strcpy(firstName, newFirstName);
} else {
strcpy(firstName, "None");
}
}
void Seller::setLastName(char newLastName[]) {
if (strlen(newLastName) > 0) {
strcpy(lastName, newLastName);
} else {
strcpy(newLastName, "None");
}
}
void Seller::setID(char newID[]) {
if (strlen(newID) > 0 && strlen(newID) < 7) {
strcpy(ID, newID);
} else {
strcpy(ID, "ZZZ000");
}
}
void Seller::setSalesTotal(double newSalesTotal) {
if (newSalesTotal >= 0) {
salesTotal = newSalesTotal;
} else {
salesTotal = 0;
}
}
double Seller::getSalesTotal() {
return salesTotal;
}
char* Seller::getFirstName(){
return firstName;
}
char* Seller::getLastName(){
return lastName;
}
char* Seller::getID(){
return ID;
}
void Seller::increaseSalesTotal( double increaseAmount ){
if( increaseAmount >= 0)
this->salesTotal += increaseAmount;
else
cout <<"Error: Amount to increase is negative."<<endl;
}

int buildSellerArray(Seller sellerArray[]);
void sortSellerArray(Seller sellerArray[], int arSize);
void printSellerArray(Seller sellerArray[], int arSize);
void processSales(Seller sellerArray[], int arSize);
int findSellerID(Seller sellerAssay[], int arSize, char searchID[]);

int main() {
Seller sellerArray[30];
int arSize;
char searchID;
arSize = buildSellerArray(sellerArray);
sortSellerArray(sellerArray, arSize);
printSellerArray(sellerArray, arSize);
processSales(sellerArray, arSize);
printSellerArray(sellerArray, arSize);

}

/************************************************
Funtion: Build Seller Array
Use: Read sellers list and use data to fill a
structured array and count and return how
many Seller's information infromation is
added.
Arguments: sellerArray[]
return: i
************************************************/
int buildSellerArray(Seller sellerArray[]) {
ifstream infile;
infile.open("sellers10.txt");
if (!infile) {
cout << "Seller file did not open" << endl;
exit(-1);
}
int i = 0;
char strValue[80];
infile >> strValue;
sellerArray[i].setLastName(strValue);
while (infile) //loop created to read through text file.
{
infile >> strValue;
sellerArray[i].setFirstName(strValue);
infile >> strValue;
sellerArray[i].setID(strValue);
double salesTotal;
infile >> salesTotal;
sellerArray[i].setSalesTotal(salesTotal);
i++;
infile >> strValue;
sellerArray[i].setLastName(strValue);
}
return i;
}
/************************************************
Funtion: Sort Seller array
Use: Takes Seller array structure and sorts data by
the sellers' ID.
Arguments: sellerArray[], arSize
return:
************************************************/
void sortSellerArray(Seller sellerArray[], int arSize) {
int min;
Seller temp;
for (int i = 0; i < arSize; i++) {
min = i;

for (int j = i + 1; j < arSize; j++) {
if ( strcmp( sellerArray[j].getID(), sellerArray[min].getID() ) < 0 ) min = j;
}
temp = sellerArray[i];
sellerArray[i] = sellerArray[min];
sellerArray[min] = temp;
}
}
/************************************************
Funtion: Print Seller array
Use: Prnts data in Seller array structure.
Arguments: sellerArray[], arSize
return:
************************************************/
void printSellerArray(Seller sellerArray[], int arSize) {
cout << setw(25) << "Seller List" << endl << endl;
for (int i = 0; i < arSize; i++)
sellerArray[i].print();
cout << endl;
}
/************************************************
Funtion: Process sales
Use: Reads Seller transaction file. Accepts value from
find sller ID finction. Makes decision based on value
received from function to decided to print a Seller's
ID with error message or sales amount. If sales amount
is rpint sales amount in sales transactions will be
added to the correct sellers' array structure.

Arguments: sellerArray[], arSize
return:
************************************************/
void processSales(Seller sellerArray[], int arSize) {
cout << setw(20) << "Seller Transactions" << endl << endl;
int index;
char searchID[80];
double salesAmount;
ifstream infile;
infile.open("sales10.txt");
infile >> searchID;
while (infile) {
infile >> salesAmount;

index = findSellerID(sellerArray, arSize, searchID);

if (index == -1) cout << setw(10) << searchID << setw(10) << "Error - ID not found" << endl;
else {
sellerArray[index].increaseSalesTotal( salesAmount );
cout << setw(15) << left << searchID << setw(10) << salesAmount << endl;
}
infile >> searchID;
}
infile.close();
cout << endl;
}
/************************************************
Funtion: Find Seller ID
Use: calls Seller ID in variable from process sales
function and checks if it matches a Seller ID
in the Seller array structure. If there is a match
subcript number is returned. If there is not a
match -1 is returned.
Arguments: sellerArray[], arSize, searchID
return: -1, i
************************************************/
int findSellerID(Seller sellerArray[], int arSize, char searchID[]) {
for (int i = 0; i < arSize; i++) {
if (strcmp(searchID, sellerArray[i].getID()) ==0 ) return i;
}
return -1;
}