60 412
Assignments Done
98,2%
Successfully Done
In April 2018

Answer to Question #50416 in C++ for zexy

Question #50416
B. 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. Save the file as Sales2.cpp.
Expert's answer
#include <iostream>
#include <string>
using namespace std;
class Sale {
private :
    int _dayOfMonth;
    double _amount;
    int _salesPersonID;
public :
    Sale(int dayOfMonth, double amount, int salesPersonID) {
        _dayOfMonth = dayOfMonth;
        _amount = amount;
        _salesPersonID = salesPersonID;
    }
    int getSalesPersonID() const {
        return _salesPersonID;
    }
    friend void display(Sale sale, SalesPerson salesPerson);
};
class SalesPerson {
private :
    int _salesPersonID;
    string _lastName;
public :
    SalesPerson(int salesPersonID, string lastName) {
        _salesPersonID = salesPersonID;
        _lastName = lastName;
    }
    int getSalesPersonID() const {
        return _salesPersonID;
    }
    friend void display(Sale sale, SalesPerson salesPerson);
};
void display(Sale sale, SalesPerson salesPerson) {
    cout << "Sale: \n";
    cout << "Day: " << sale._dayOfMonth << endl;
    cout << "Amount: " << sale._amount << endl;
    cout << "Sales person ID: " << sale._salesPersonID << endl;
    cout << endl;
    cout << "Sales person: \n";
    cout << "ID: " << salesPerson._salesPersonID << endl;
    cout << "Last name: " << salesPerson._lastName << endl;
    cout << endl;
}
SalesPerson* validateSale(Sale sale, SalesPerson persons[], int personNumber) {
    for (int i = 0; i < personNumber; ++i) {
        if (persons[i].getSalesPersonID() == sale.getSalesPersonID()) {
            persons[i];
        }
    }
    return NULL;
}
int main() {
    const int personNumber = 5;
    SalesPerson persons[personNumber] = {
            SalesPerson(1, "Smith"),
            SalesPerson(2, "Jones"),
            SalesPerson(3, "Lawrence"),
            SalesPerson(4, "Englishman"),
            SalesPerson(5, "Phillips")
    };
    do {
        int sentinel = 0;
        cout << "Do you want to enter sale? (1/0): ";
        cin >> sentinel;
        if (sentinel == 0) {
            break;
        }
        // Read data
        int dayOfMonth;
        double amount;
        int salesPersonID;
        cout << "Day of month: ";
        cin >> dayOfMonth;
        cout << "Amount: ";
        cin >> amount;
        cout << "Sales person ID: ";
        cin >> salesPersonID;
        Sale sale(dayOfMonth, amount, salesPersonID);
        SalesPerson* salesPerson = validateSale(sale, persons, personNumber);
        if (!salesPerson) {
            cout << "Error: invalid sales person ID entered. Try again.";
        } else {
            display(<#(Sale)sale#>, <#(SalesPerson)salesPerson#>);
        }
    } while(true);
    return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions