The Question is :
A store has 6 items. Each item has a price and a bar-code that uniquely
identifies it. To simulate such a store, the program offers a menu showing the
codes, their corresponding items, and their prices. The user chooses one or
several items to buy, and pays for them. The program then returns the right
change (big bills first). For example, if the user pays 150 QR and the items cost
101 QR, then the amount returned is 49 QR. So, the change will be four 10
bills, one 5 bill, and four 1 bills. We assume that the user will buy one or more
items and checkout by choosing the code 0. If the amount is not sufficient, the
user is asked to repay for the items.
The codes, the items, and the prices to use are shown in the following table
(The code 0 is used to signal that the user completed the shopping):
Code Item Price
0 - -
1 Bread 5
2 Oil 8
3 Pizza 43
4 Laptop 1199
5 Macaroni 20
6 Tuna 7
Due to the problem of debugging and understanding programs with global
variables, your implementation must not contain any global variables.
Run and test the program with following scenarios:
The user buys Pizza (item 3) and pays 45.
The user buys Laptop (item 4) and pays 1500.
The user did not buy anything by choosing the code zero.
The user buys two Pizzas (3), Macaroni (5), and Oil (2). The user pays 100.
Hi every one,
#include <iostream>
#include <iomanip>
using namespace std;
void DispalyStoreItems ();
int ChoosingItems ();
double calculateChange ( int , int );
int main ()
{
int cost;
DispalyStoreItems ();
cost = ChoosingItems ();
calculateChange ( int pay , int cost );
system ("PAUSE");
return 0;
}
void DispalyStoreItems ()
{
cout <<"Code" << setw (10) << "Item" << setw (10) << "Price" << endl;
cout << "------------------------" << endl;
cout << setw (2) << "0" << setw (11) << " - " << setw (10) << " - " << endl;
cout << endl;
cout << setw (2) << "1" << setw (13) << "Bread" << setw (7) << "5" << endl;
cout << endl;
cout << setw (2) << "2" << setw (11) << "Oil" << setw (9) << "8" << endl;
cout << endl;
cout << setw (2) << "3" << setw (13) << "Pizza" << setw (8) << "43" <<endl;
cout << endl;
cout << setw (2) << "4" << setw (14) << "Laptop" << setw (9) << "1199" << endl;
cout << endl;
cout << setw (2) << "5" << setw (16) << "Macaroni" << setw (5) << "20" << endl;
cout << endl;
cout << setw (2) << "6" << setw (12) << "Tuna" << setw (8) << "7" << endl;
cout << endl;
}
int ChoosingItems ()
{
int price;
int item = 0;
int cost = 0;
int code = 0;
int count = 0;
int Bread = 0;
int Oil = 0;
int Pizza = 0;
int Laptop = 0;
int Macaroni = 0;
int Tuna = 0;
while ( true )
{
cout << "Enter the code of the Item you want to buy : ";
cin >> code;
cout << endl;
if ( code == 0 )
break;
switch ( item )
{
case 1:
Bread++;
break;
case 2:
Oil++;
break;
case 3:
Pizza++;
break;
case 4:
Laptop++;
break;
case 5:
Macaroni++;
break;
case 6:
Tuna++;
break;
}
if ( code == 1 )
{
price = 5;
}
else if ( code == 2 )
{
price = 8;
}
else if ( code == 3 )
{
price = 43;
}
else if ( code == 4 )
{
price = 1199;
}
else if ( code == 5 )
{
price = 20;
}
else if ( code == 6 )
{
price = 7;
}
else
{
price = 0;
}
cost += price;
}
if ( Bread != 0 )
cout << "The User Buys " << item << "\n";
if ( Oil != 0 )
cout << "The User Buys " << item << "\n";
if ( Pizza != 0 )
cout << "The User Buys " << item << "\n";
if ( Laptop != 0 )
cout << "The User Buys " << item << "\n";
if ( Macaroni != 0 )
cout << "The User Buys " << item << "\n";
if ( Tuna != 0 )
cout << "The User Buys " << item << "\n";
cout << "and Pays " << cost << "\n";
return cost;
}
double calculateChange ( int pay, int cost )
{
double units;
double change;
cout << "Enter the amount of the money you should pay : ";
cin >> pay;
cout << endl;
change = pay - cost;
if ( change >= 100)
{
change = ( change / 100 ) - ( units * 100 );
cout << change << "Hundreds" << "\n";
}
if ( change >= 50 )
{
change = ( change / 50 ) - ( units * 50 );
cout << change << "Fifties" << "\n ";
}
if ( change >= 10 )
{
change = ( change / 10 ) - ( units * 10 );
cout << change << "Tens" << "\n";
}
if ( change >= 1 )
{
change = change - units;
cout << change << "Ones" << "\n";
}
return change;
}