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

Taif coffee shop wants to open three branches at Taif University at the followin

ID: 3828868 • Letter: T

Question

Taif coffee shop wants to open three branches at Taif University at the following locations: Al-Hawiya, Al Faisaliah and Qarua. The coffee shop manager needs to computerize orders to generate reports at the end of each month. He requests you to write a C++ program to input ordered data and generate monthly report. The manger asks you to display the monthly report in a tabular form with all orders names in alphabetical order. The report should display each order name with its ordered quantity by each branch and total ordered quantity by all branches. At the end of the report, he wants you to print the popular order and the popular branch. To simplify the testing, your program can manage six order types but you can easily increase the number of order types. Therefore, you will need an array of string called orderName (NO OF ORDER], where No oF ORDER is a constant variable. SAMPLE Your program should display the report in the following form: Monthly Report of Taif Coffee shop Order Branches Total Al-Hawiya Al Faisaliah Qarua Quantity Name Coffee 400 300 200 900 600 Crepe 400 200 1200 Croissant. 500 350 150 1000 350 Doughnut 300 120 770 Sandwich 200 190 190 580 Tea 300 250 220 770 Popular order: crepe, Total ordered Quantity 1200 orders Popular Branch: Al-Hawiya, Total orderd Quantity: 2350 orders

Explanation / Answer

Here is the code for the question. Sample input and output are also given. If working on linux, make sure the filenames are given exactly same, otherwise files will not be read (case sensitive).

// C++ code
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//global variables
const int NO_OF_ORDER = 6;
const int NO_OF_BRANCH = 3;


int getOrderName(string orderName[]); //reads orderName.txt and fill the array
void sortOrders(string orderName[], int size); //sorts the orderName array
void setToZero(int Hawiyah[], int Faisaliah[], int Qarua[], int totalQuantity[], int size); //initialize all array elements to 0
int searchOrders(string name, string orderName[], int size); //return the index of a name in orderName , -1 if not found
bool insertOrders(string orderName[], int Hawiyah[], int Faisaliah[], int Qarua[], int size); //loads data from orderData.txt
void sumTotalQuantity(int Hawiyah[], int Faisaliah[], int Qarua[], int totalQuantity[], int size); //calculates the total for each product across branches
int popularOrder(int totalQuantity[], int size); //determines the index of the name which has maximum totalQuantity
int popularBranch(int Hawiyah[], int Faisaliah[], int Qarua[], int size, int &brachQuantity); //returns the index of the branch which has most orders
void reportHeading(); //prints report heading
void reportBody(string orderName[],int Hawiyah[], int Faisaliah[], int Qarua[], int totalQuantity[], int size); //prints report body

int main()
{
string orderName[NO_OF_ORDER];
int Hawiyah[NO_OF_ORDER], Faisaliah[NO_OF_ORDER],Qarua[NO_OF_ORDER];
int totalQuantity[NO_OF_ORDER];

getOrderName(orderName);
sortOrders(orderName,NO_OF_ORDER);
setToZero(Hawiyah,Faisaliah, Qarua, totalQuantity, NO_OF_ORDER);
insertOrders(orderName, Hawiyah, Faisaliah, Qarua, NO_OF_ORDER);
sumTotalQuantity(Hawiyah, Faisaliah, Qarua, totalQuantity, NO_OF_ORDER);
reportHeading();
reportBody(orderName,Hawiyah, Faisaliah, Qarua, totalQuantity, NO_OF_ORDER);
}

int getOrderName(string orderName[])
{
ifstream infile("orderName.txt");
if(!infile)
{
cout<<"Could not read order names from orderName.txt"<<endl;
return false;

}

int i=0;
while(infile>>orderName[i])
i++;

return i;
}

void sortOrders(string orderName[], int size)
{
int minIdx;
string temp;
for(int i=0; i<size; i++)
{
minIdx = i;
for(int j=i+1; j<size; j++)
{
if(orderName[j] < orderName[minIdx])
minIdx = j;
}

if(minIdx != i)
{
temp = orderName[i];
orderName[i] = orderName[minIdx];
orderName[minIdx] = temp;
}
}
}

void setToZero(int Hawiyah[], int Faisaliah[], int Qarua[], int totalQuantity[], int size)
{
for(int i=0; i<size; i++)
{
Hawiyah[i] = 0;
Faisaliah[i] = 0;
Qarua[i] = 0;
totalQuantity[i] = 0;
}
}

int searchOrders(string name, string orderName[], int size)
{
for(int i=0; i<size; i++)
{
if(orderName[i] == name)
return i;
}
return -1;
}


bool insertOrders(string orderName[], int Hawiyah[], int Faisaliah[], int Qarua[], int size)
{
ifstream infile("orderData.txt");
if(!infile)
{
cout<<"Could not read orderData.txt"<<endl;
return false;
}

string name;
int idx, branch, quantity;
while(infile>>name)
{
idx = searchOrders(name, orderName, size);
infile >> branch;
infile >> quantity;
if(branch == 1)
Hawiyah[idx] = quantity;
else if(branch == 2)
Faisaliah[idx] = quantity;
else if(branch == 3)
Qarua[idx] = quantity;
}
return true;
}

void sumTotalQuantity(int Hawiyah[], int Faisaliah[], int Qarua[], int totalQuantity[], int size)
{
for(int i = 0; i < size; i++)
{
totalQuantity[i] = Hawiyah[i] + Faisaliah[i] + Qarua[i];
}

}
int popularOrder(int totalQuantity[], int size)
{
int maxIdx = 0;
for(int i=1; i < size; i++)
{
if(totalQuantity[i] > totalQuantity[maxIdx])
maxIdx = i;
}
return maxIdx;
}


//returns the number of the brach which has the maximum orders. The branchQuantity is passed by reference . So its also output variable.
//It is updated with popularBranch's total orders . The popular branch number is returned (1, 2, 3) and its sum quantity is updated in branchQuantity
int popularBranch(int Hawiyah[], int Faisaliah[], int Qarua[], int size, int &brachQuantity)
{
int total1 = 0, total2 = 0, total3 = 0;
int popularBranch;
for(int i=0; i<size; i++)
total1 += Hawiyah[i];
for(int i=0; i<size; i++)
total2 += Faisaliah[i];
for(int i=0; i<size; i++)
total3 += Qarua[i];
if(total1 > total2)
{
if(total1 > total3)
{
popularBranch = 1;
brachQuantity = total1;
}
else
{
popularBranch = 3;
brachQuantity = total3;
}
}
else
{
if(total2 > total3)
{
popularBranch = 2;
brachQuantity = total2;
}
else
{
popularBranch = 3;
brachQuantity = total3;
}
}

return popularBranch;
}

void reportHeading()
{
cout<<" --------------------Monthly Report of Taif Coffee Shop------------------------"<<endl;
cout<<"Order --------------------Branches------------------ Total"<<endl;
cout<<"Name Al-Hawiyah Al-Faisaliah Qarua Quantity"<<endl;
cout<<"_________________________________________________________________________________"<<endl;

}

void reportBody(string orderName[],int Hawiyah[], int Faisaliah[], int Qarua[], int totalQuantity[], int size)
{
for(int i=0; i<size; i++)
{
cout<<left<<setw(15)<<orderName[i]<<" "<<setw(5)<<Hawiyah[i] <<" "<<Faisaliah[i]<<" "<<Qarua[i]<<" "<<totalQuantity[i]<<endl;
}

int popOrdIdx = popularOrder(totalQuantity, size);
cout<<" Popular order: "<<orderName[popOrdIdx]<<", Total Ordered Quantity: "<<totalQuantity[popOrdIdx]<<" orders"<<endl<<endl;

int popBranchQty, branch;
string branchNames[]={"Al-Hawiyah", "Al-Faisaliah","Qarua"};

branch = popularBranch(Hawiyah, Faisaliah, Qarua, size, popBranchQty);
cout<<"Popular branch: "<<branchNames[branch-1]<<", Total ordered quantity: "<<popBranchQty<<" orders"<<endl;
}

contents of input file orderName.txt

Coffee Doughnut Croissant Sandwich Tea Crepe

contents of input file orderData.txt

Coffee 1 400
Coffee 2 300
Coffee 3 200
Doughnut 1 350
Doughnut 2 300
Doughnut 3 120
Sandwich 1 200
Sandwich 2 190
Sandwich 3 190
Crepe 1 600
Crepe 2 400
Crepe 3 200
Croissant 1 500
Croissant 2 350
Croissant 3 150
Tea 1 300
Tea 2 250
Tea 3 220

output

--------------------Monthly Report of Taif Coffee Shop------------------------
Order        --------------------Branches------------------        Total
Name        Al-Hawiyah    Al-Faisaliah        Qarua        Quantity
_________________________________________________________________________________
Coffee        400         300       200       900
Crepe         600         400       200       1200
Croissant         500         350       150       1000
Doughnut        350         300       120       770
Sandwich        200         190       190       580
Tea         300         250       220       770

Popular order: Crepe, Total Ordered Quantity: 1200 orders

Popular branch: Al-Hawiyah, Total ordered quantity: 2350 orders