Question

Please answer the following in C++.

Add a function to both the Sale and Salesperson classes that returns the private salesperson ID number. Write a main()function that contains an array of five Salesperson objects and store appropriate data in it. Then, continue to prompt the user for Sale data until the user enters an appropriate sentinel value. For each Sale transaction entered, determine whether the salesperson's ID number is valid. Either display an error message, or use the friend display()function to display all the data.

Use the following for the ID and name:

103: Woods

104: Martin

105: Martin

106: Hansen

107: Davis

Add a function to both the Sale and Salesperson classes that returns the private salesperson ID number Write a main()function that contains an array of five Salesperson objects and store appropriate data in it. Then, continue to prompt the user for Sale data until the user enters an appropriate sentinel value. For each Sale transaction entered, determine whether the salespersons ID number is valid. Either display an error message, or use the friend display()function to display all the data Use the following for the ID and name 103: Woods . 104: Martin . 105: Martin . 106: Hansen . 107: Davis Sample Run Enter salesperson ID or 0 to quit 103 Enter month of sale 2 Enter day 12 Enter year 2016 Enter amount of sale 1000 Sale #103 on 2/12/2016 for $1000 sold by #103 Woods Enter salesperson ID or 0 to quit 106 Enter month of sale 11 Enter day 25 Enter year 2016 Enter amount of sale 500 Sale #106 on 11/25/2016 for $500 sold by #106 Hansen Enter salesperson ID or 0 to quit 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We first create the two classes, Salesperson and Sale. Salesperson has just two data members, id and name.

Sale has data members: id, which will be link the object to a Salesperson object with the same id. mm,dd,yyyy and amt save the month, day, year and amount of sale.

Data can be saved to objects of these classes either through their constructors or their respective setData() methods. Both classes have a method getID() which returns the id of the object of the respective classes.

The Sale class has a friend function, display() which is used to display private data of that class. It takes two arguments, an object of Sale class and an object of Salesperson class. It makes use of the Salesperson class' getID() and getName() methods to display data of that class.

The validateID() function takes two arguments, an int ID and an arrayP[] of Salesperson. It compares the id of each element of P[] with ID. If a match is found, then ID is valid and the function returns the index of P[] where the match was found. If match is not found, then ID is invalid and the function returns a -1.

In the main() function, we first create an array of five salespersons with the data given in the question.

Then we get a salesperson's ID from the user and validate it. The program goes into a loop to accept ID and dat until the user enters 0 for ID to stop.

Then we get the date, validating each part (day and month) as it is entered. Note that this code does not account for leap years. After this, we get the sale amount.

Once the data has been entered, a Sale object, t, is created with it. Since the question does not mention that the Sale data has to be stored anywhere, we simply overwrite t in each iteration of the outer while loop.

All this dat from the two objects is then displayed using the display() function.

Code:

#include <iostream> #includeくstring> using nanespace std; class salesperson int id string nare: 10 pubiic: Salesperson/defaul

15 16 17 18 19 assign dumny id=-1 ; values dd-0: Sale (int L,int m, int d,int y,double a) //paraneterized constructor 54 id-i

casino pyesData.cpp int nain) //tirst create the array of 5 Salespersons Salesperson SPT5] SP [0].setData (i03, Woods) SP [

casino pyesData.cpp 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1

#include <iostream>
#include <string>

using namespace std;

class Salesperson
{
int id;
string name;
public:
Salesperson() //default constructor
{ //assign dummy values
id=-1;
name="";
}
Salesperson(int i,string n) //parameterized constructor
{
id=i;
name=n;
}
void setData(int i,string n) //set up the object
{
id=i;
name=n;
}
int getID() //return salesperson's ID
{
return id;
}
string getName() //return salesperson's name
{
return name;
}
};

class Sale
{
int id; //this is the same as Salesperson id
int mm; //month of sale
int dd; //day of sale
int yyyy; //year of sale
double amt; //amount of sale
public:
Sale() //default constructor
{ //assign dummy values
id=-1;
mm=0;
dd=0;
yyyy=0;
amt=0;
}
Sale(int i,int m,int d,int y,double a) //parameterized constructor
{
id=i;
mm=m;
dd=d;
yyyy=y;
amt=a;
}
void setData(int i,int m,int d,int y,double a) //set the object's data
{
id=i;
mm=m;
dd=d;
yyyy=y;
amt=a;
}
int getID() //return salesperson id
{
return id;
}
friend void display(Sale S,Salesperson P);
};

void display(Sale S,Salesperson P) //the friend function
{
cout<<"Sale #"<<S.id<<" on "<<S.mm<<"/"<<S.dd<<"/"<<S.yyyy<<" for $"<<S.amt<<" by #"<<P.getID()<<" "<<P.getName()<<endl;
}

int validateID(int ID,Salesperson P[]) //check if ID is found in P[], return index if the array at which ID is found, -1 if not found
{
for(int i=0;i<5;i++) //go thru the array of salespaersons
if(ID==P[i].getID()) //if ID is found in the array, ie ID is valid
return i; //return index at which match is found
//control will come here only if ID is invalid, ie was not found in the array
return -1;
}

int main()
{
//first create the array of 5 Salespersons
Salesperson SP[5];
SP[0].setData(103,"Woods");
SP[1].setData(104,"Martin");
SP[2].setData(105,"Martin");
SP[3].setData(106,"Hansen");
SP[4].setData(107,"Davis");

int ID,i; //to input ID to search, index of SP[]
int m,d,y; //to input the date
double a; //to input sales amount

//read ID
cout<<"Enter salesperson ID or 0 to quit ";
cin>>ID;
i=validateID(ID,SP); //validate the ID
while(ID!=0 && i==-1) //keep looping till the user enters a valid id
{
cout<<"Invalid Salesperson ID."<<endl;
cout<<"Enter salesperson ID or 0 to quit ";
cin>>ID;
i=validateID(ID,SP); //validate the ID
}

//keep looping until user enters 0 for ID to quit
while(ID!=0)
{
cout<<"Enter month of sale ";
cin>>m;
while(m<1 || m>12) //keep looping till valid month number is entered
{
cout<<"Invalid month."<<endl;
cout<<"Enter month of sale ";
cin>>m;
} // end of inner while
cout<<"Enter day of sale ";
cin>>d;
if(m==2) //if month is February (Note: this code does not take into account leap years)
{
while(d<1 || d>28) //keep looping till valid day number is entered
{
cout<<"Invalid date."<<endl;
cout<<"Enter day of sale ";
cin>>d;
} //end of while(d...
}
else //(if m!=2)
{
if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) //if the month can have 31 days
while(d<1 || d>31) //keep looping till valid day number is entered
{
cout<<"Invalid date."<<endl;
cout<<"Enter day of sale ";
cin>>d;
} //end of while(d...
else //if the month can have only 30 days
while(d<1 || d>30) //keep looping till valid day number is entered
{
cout<<"Invalid date."<<endl;
cout<<"Enter day of sale ";
cin>>d;
} //end of while(d...
} //end of else (m!=2)

cout<<"Enter year of sale ";
cin>>y;
cout<<"Enter amount of sale ";
cin>>a;
Sale t(ID,m,d,y,a); //create a new Sale object
display(t,SP[i]); //display data of the Sale object t and the Salesperson object SP[i]

//read the next ID
cout<<"Enter salesperson ID or 0 to quit ";
cin>>ID;
i=validateID(ID,SP); //validate the ID
while(ID!=0 && i==-1) //keep looping till the user enters a valid id or 0
{
cout<<"Invalid Salesperson ID."<<endl;
cout<<"Enter salesperson ID or 0 to quit ";
cin>>ID;
i=validateID(ID,SP); //validate the ID
} //end of while(ID...
} //end of outer while

return 0;
}

Output

Enter salesperson ID or to quit 6 Invalid Salesperson ID Enter salesperson ID or θ to quit 103 Enter month of sale 15 Invalid

Hope this helps!

Add a comment
Know the answer?
Add Answer to:
Please answer the following in C++. Add a function to both the Sale and Salesperson classes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • C Program Assignment: 2-Add comments to your program to full document it by describing the most...

    C Program Assignment: 2-Add comments to your program to full document it by describing the most important processes. 3-Your variables names should be very friendly. This means that the names should have meaning related to what they are storing. Example: Instead of naming the variable that stores the hours worked for an employee in a variable called ‘x’ you should name it HoursWorked. 5-Submit your .c program (note that I only need your source code and not all files that...

  • In C++, Please help me compute the following flowchart exactly like this requirement: Use while loops...

    In C++, Please help me compute the following flowchart exactly like this requirement: Use while loops for the input validations required: number of employees(cannot be less than 1) and number of days any employee missed(cannot be a negative number, 0 is valid.) Each function needs a separate flowchart. The function charts are not connected to main with flowlines. main will have the usual start and end symbols. The other three functions should indicate the parameters, if any, in start; and...

  • Develop a set of classes for a college to use in various student service and personnel applications. Classes you need to design include the following: • Person — A Person contains the following fields...

    Develop a set of classes for a college to use in various student service and personnel applications. Classes you need to design include the following: • Person — A Person contains the following fields, all of type String: firstName, lastName, address, zip, phone. The class also includes a method named setData() that sets each data field by prompting the user for each value and a display method that displays all of a Person’s information on a single line at the...

  • PLEASE ANSWER IN C# 5. a. Create a Patient class for the Wrightstown Hospital Billing Department....

    PLEASE ANSWER IN C# 5. a. Create a Patient class for the Wrightstown Hospital Billing Department. Include auto-implemented prop- erties for a patient ID number, name, age, and amount due to the hospital, and include any other methods you need. Override the ToString method to return all the details for a patient. Write an application that prompts the user for data for five Patients. Sort them in patient ID number order and display them all, including a total amount owed....

  • In C Program This program will store the roster and rating information for a soccer team. There w...

    In C Program This program will store the roster and rating information for a soccer team. There will be 3 pieces of information about each player: Name: string, 1-100 characters (nickname or first name only, NO SPACES) Jersey Number: integer, 1-99 (these must be unique) Rating: double, 0.0-100.0 You must create a struct called "playData" to hold all the information defined above for a single player. You must use an array of your structs to to store this information. You...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work....

    Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work. You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. Playlist.h - Class declaration Playlist.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT