C++ program that creates an array of six Division Sales objects and calculate the total corporate sales for the year
Corporate Sales
A corporation has six divisions, each responsible for sales to different geographic
locations. Design a DivSales class that keeps sales data for a division, with the following
members:
• An array with four elements for holding four quarters of sales figures for the division.
• A private static variable for holding the total corporate sales for all divisions for
the entire year.
• A member function that takes four arguments, each assumed to be the sales for a
Quarter. The value of the arguments should be copied into the array that holds the
sales data. The total of the four arguments should be added to the static variable
that holds the total yearly corporate sales.
• A function that takes an integer argument within the range of 0-3. The argument
is to be used as a subscript into the division Quarterly sales array. The function
should return the value of the array element with that subscript.
Write a C++ program that creates an array of six DivSales objects. The program should
ask the user to enter the sales for four quarters for each division. After the data are
entered, the program should display a table showing the division sales for each quarter.
The program should then display the total corporate sales for the year.
Input Validation: Only accept positive values for quarterly sales figures.
Answer:
// Corporate Sales #include <iostream> #include <iomanip> using namespace std; // Constants for the number of quarters // and number of divisions const int NUM_QTRS = 4; const int NUM_DIVS = 6; // Declaration of the DivSales class class DivSales { private: double sales[NUM_QTRS]; static double totalSales; public: void setSales(double, double, double, double); double getQSales(int q) { return sales[q]; } double getCorpSales() { return totalSales; } }; // Static member variable definition double DivSales::totalSales; //**************************************** // Constructor * //**************************************** void DivSales::setSales(double q1, double q2, double q3, double q4) { if (q1 < 0 || q2 < 0 || q3 < 0 || q4 < 0) { cout << "ERROR: Only provide positive values for sales.\n"; return; } sales[0] = q1; sales[1] = q2; sales[2] = q3; sales[3] = q4; for (int count = 0; count < NUM_QTRS; count++) totalSales += sales[count]; } // Function prototype void salesByDiv(DivSales []); int main() { DivSales divisions[NUM_DIVS]; double qtrs[NUM_QTRS]; for (int div = 0; div < NUM_DIVS; div++) { cout << "Enter sales data for Division "; cout << (div + 1) << endl; for (int q = 0; q < NUM_QTRS; q++) { cout << "\tQuarter " << (q + 1) << ": "; cin >> qtrs[q]; while (qtrs[q] < 0) { cout << "Please enter 0 or greater: "; cin >> qtrs[q]; } } divisions[div].setSales(qtrs[0], qtrs[1], qtrs[2], qtrs[3]); } cout << fixed << showpoint << setprecision(2); salesByDiv(divisions); cout << "\nTotal Corporate Sales: $" << divisions[0].getCorpSales() << endl; return 0; } //********************************************************** // The following function displays a list of sales figures * // by division. * //********************************************************** void salesByDiv(DivSales d[]) { for (int div = 0; div < NUM_DIVS; div++) { cout << "Sales for Division "; cout << (div + 1) << endl; for (int qtr = 0; qtr < NUM_QTRS; qtr++) { cout << "\tQuarter " << (qtr + 1) << ": $"; cout << d[div].getQSales(qtr) << endl; } } cout << "Press Enter to Continue."; cin.get(); }
Leave a reply