/* Corporate Sales Data Input - This program reads the data in the file
created by the program in Programming Challenge 12.11. The program
calculates and displays the following figures:
* Total corporate sales for each quarter
* Total yearly sales for each division
* Total yearly corporate sales
* Average quarterly sales for the division
* The highest and lowest quarters for the corporation */
#include "Utility.h"
const int NAME_LENGTH = 15;
const int NUM_BRANCHES = 4;
const int NUM_QUARTERS = 4;
struct CorporateSales
{
char divisionName[NAME_LENGTH]; /* Holds the division names */
array<double, NUM_QUARTERS> qrtlySales; /* Holds the quarterly sales results */
};
struct SalesResults
{
array<double, NUM_QUARTERS> totalSalesQtr; /* Total corporate sales for each quarter */
array<double, NUM_QUARTERS> totalSalesDiv; /* Total yearly sales for each division */
array<double, NUM_QUARTERS> avgQtrlySales; /* Average quarterly sales for the division */
double totalYrlyCorp; /* Total yearly corporate sales */
double highestQtr; /* Highest quarterly sales for the corporation */
double lowestQtr; /* Lowest quarterly sales for the corporation */
int highest; /* Holds the quarter with highest sales result */
int lowest; /* Holds the quarter with lowest sales result */
};
struct DivisionSales
{
CorporateSales sales[NUM_BRANCHES]; /* Nested CorporateSales array structure member */
SalesResults summary; /* Nested SalesResult structure member */
};
void initStruct(DivisionSales &);
int readSalesData(DivisionSales &);
void totalSalesQtr(DivisionSales &);
void totalDivSales(DivisionSales &);
void avgDivSalesQtr(DivisionSales &);
void totalCorpSales(DivisionSales &);
void hiLoCorpSales(DivisionSales &);
void displaySalesReport(const DivisionSales &);
int main()
{
DivisionSales division;
int fOpen = 0;
initStruct(division);
cout << "\n\tISHIKAWA FRUIT COMPANY\n\n";
if (fOpen = readSalesData(division) != -1)
{
totalSalesQtr(division);
totalDivSales(division);
totalCorpSales(division);
avgDivSalesQtr(division);
hiLoCorpSales(division);
displaySalesReport(division);
}
cout << "\n\n\tNow exiting the program ...\n"
<< "\n\tISHIKAWA FRUIT COMPANY - Your number one fruit supplier!";
pauseSystem();
return 0;
}
/* **********************************************************
Definition: initStructure
This function uses a reference variable to a struct as
its argument. All structure members are initialized to a
default value.
********************************************************** */
void initStruct(DivisionSales &sales)
{
sales.summary.totalYrlyCorp = 0.0;
sales.summary.highestQtr = 0.0;
sales.summary.lowestQtr = 0.0;
sales.summary.highest = 0;
sales.summary.lowest = 0;
for (int index = 0; index < NUM_BRANCHES; index++)
{
sales.summary.totalSalesQtr[index] = 0.0;
sales.summary.totalSalesDiv[index] = 0.0;
sales.summary.avgQtrlySales[index] = 0.0;
}
}
/* **********************************************************
Definition: readSalesData
This function uses a reference variable to a struct as
parameter. The user is asked to enter the name of the file
from which the sales data should be read in. Upon succes,
the sales data is read in, and the structure members are
populated. In case of failure a message is displayed, and
the program exits.
********************************************************** */
int readSalesData(DivisionSales &division)
{
string fileName = "";
cout << "\tEnter filename containing sales data: ";
cin >> fileName;
fstream readData(fileName.c_str(), ios::in | ios::binary);
if (!readData.fail())
{
cout << "\tReading in sales data from\t\t" << fileName << "\n";
readData.read(reinterpret_cast<char *>(&division),
sizeof(CorporateSales) * NUM_BRANCHES);
cout << "\tData successfully read in from\t\t" << fileName << "\n"
<< "\tNow closing\t\t\t\t" << fileName << "\n\n\n\n";
}
else
{
cout << "\n\tFile Read Error. Could not open or process " << fileName << "\n"
<< "\tThis program will now exit ...";
return -1;
}
readData.close();
return 0;
}
/* **********************************************************
Definition: totalSalesQtr
This function uses a reference variable to a struct as
paramter. It calculates the sales total achieved by the
whole company in each of the four quarters. This data is
stored in the appropriate struct member variable.
********************************************************** */
void totalSalesQtr(DivisionSales &corp)
{
for (int index = 0; index < NUM_BRANCHES; index++)
{
for (int qtrs = 0; qtrs < NUM_QUARTERS; qtrs++)
{
corp.summary.totalSalesQtr[qtrs] += corp.sales[index].qrtlySales[qtrs];
}
}
}
/* **********************************************************
Definition: totalDivSales
This function uses a reference variable to a struct as
paramter. It calculates the sales total achieved by each
of the four companies' divisions, for each of the four
quarters. This result is stored in the appropriate struct
member variable.
********************************************************** */
void totalDivSales(DivisionSales &division)
{
for (int index = 0; index < NUM_BRANCHES; index++)
{
division.summary.totalSalesDiv[index] = 0;
for (int qtrs = 0; qtrs < NUM_QUARTERS; qtrs++)
{
division.summary.totalSalesDiv[index] += division.sales[index].qrtlySales[qtrs];
}
}
}
/* **********************************************************
Definition: avgDivSalesQtr
This function uses a reference variable to a struct as
paramter. It calculates the average sales results achieved
by each division in each of the four quarters. This data
is stored in the appropriate struct member variable.
********************************************************** */
void avgDivSalesQtr(DivisionSales &division)
{
for (int index = 0; index < NUM_BRANCHES; index++)
{
division.summary.avgQtrlySales[index] = 0;
for (int qtrs = 0; qtrs < NUM_QUARTERS; qtrs++)
{
division.summary.avgQtrlySales[index] +=
division.sales[index].qrtlySales[qtrs] / NUM_BRANCHES;
}
}
}
/* **********************************************************
Definition: totalCorpSales
This function uses a reference variable to a struct as
paramter. It calculates the sales total achieved by the
company. This data is stored in the appropriate struct
member variable.
********************************************************** */
void totalCorpSales(DivisionSales &corp)
{
for (int qtrs = 0; qtrs < NUM_QUARTERS; qtrs++)
{
corp.summary.totalYrlyCorp += corp.summary.totalSalesQtr[qtrs];
}
}
/* **********************************************************
Definition: totalSalesQtr
This function uses a reference variable to a struct as
paramter. It determines the quarters with the highest
and lowest sales results. Both the highest and lowest
results as well as the quarter in which it was achieved
is stored in the appropriate struct member variables.
********************************************************** */
void hiLoCorpSales(DivisionSales &corp)
{
corp.summary.highestQtr = corp.summary.totalSalesQtr[0];
corp.summary.lowestQtr = corp.summary.totalSalesQtr[0];
for (int index = 0; index < NUM_BRANCHES; index++)
{
corp.summary.highestQtr < corp.summary.totalSalesQtr[index] ?
corp.summary.highestQtr = corp.summary.totalSalesQtr[index],
corp.summary.highest = (index + 1):
corp.summary.highestQtr;
}
for (int index = 0; index < NUM_BRANCHES; index++)
{
corp.summary.lowestQtr > corp.summary.totalSalesQtr[index]?
corp.summary.lowestQtr = corp.summary.totalSalesQtr[index],
corp.summary.lowest = (index + 1) :
corp.summary.lowestQtr;
}
}
/* **********************************************************
Definition: totalSalesQtr
This function uses a reference variable to a struct as
paramter. It displays a detailed summary of the sales
results.
********************************************************** */
void displaySalesReport(const DivisionSales &corp)
{
cout << "\n\tISHIKAWA FRUIT COMPANY SALES DATA REPORT\n";
cout << setprecision(2) << fixed << showpoint << "\n";
cout << "\t" << setw(31) << right << "\tQuarterly Sales Corp."
<< setw(30) << right << "Yearly Sales Div."
<< setw(42) << right << "Quarterly Average Sales Div.\n";
cout << "\t" << setw(109) << setfill('-') << right;
cout << "\n" << setfill(' ');
for (int qtrs = 0; qtrs < NUM_QUARTERS; qtrs++)
{
cout << "\tQuarter " << (qtrs + 1)
<< setw(7) << right << "$ " << corp.summary.totalSalesQtr[qtrs]
<< setw(25) << right << "$ " << corp.summary.totalSalesDiv[qtrs]
<< setw(21) << right << "$ " << corp.summary.avgQtrlySales[qtrs] << "\n";
}
cout << "\t" << setw(109) << setfill('-') << right;
cout << "\n" << setfill(' ');
cout << "\tSales Total: " << setw(3) << right << "$ " << corp.summary.totalYrlyCorp;
cout << "\n\n\tHighest Sales Result achieved in Quarter " << (corp.summary.highest + 1)
<< setw(52) << right << "\tSales Result $ " << (corp.summary.highestQtr);
cout << "\n\tLowest Sales Result achieved in Quarter " << (corp.summary.lowest)
<< setw(52) << right << "\tSales Result $ " << (corp.summary.lowestQtr);
}
No comments:
Post a Comment