roup/afeosf5f fac3 4527-915a 366ff83ds465/Lab%20Assignment%20Files/Labos/ES1036b
ID: 3862325 • Letter: R
Question
roup/afeosf5f fac3 4527-915a 366ff83ds465/Lab%20Assignment%20Files/Labos/ES1036b s "Lab format, approach, design and pre-thought provided by Dr. Quazi Rahman E.2. Question 2: Improving the Text-based RPG vendor (60 Marks) Requirement: Update your store program to use modular programming Additionally, use a global variable to store your user's currency so that you may access and modify it anywhere in your program. Updated Specifications: 1. The program will print your info and a message that describes its mission (Must use the function you created in Lab04) 2. Customer is asked how much currency they have available which is stored to a global variable and displayed when they purchase. 3. Program explains how all shops are closed other than the one you create Program asks if the customer (the user) wants to enter the open shop. (If they don't, the program exits.) 5. Program introduces the shop and displays the available items and how much each costs (Minimum of 4 items). I 6. Customer is asked to pick an item from the list to purchase. If the customer picks an item not on the list, they are told that item doesn't exist or is no longer available and are asked to try again. Additionally, a final option under available items is available to exit the store. 7. If the customer has enough currency, the item's cost is deducted from their total currency and the purchased item is removed from the available items. If the customer doesn't have enough currency, they are told to try another item. 8. Program then outputs the item which was purchased and the remaining currency the customer has left. After a purchase is made, they are shown the new list of items available (with the one they bought no longer available) to buy more. If all items have been purchased, the store closes. Design tips: Find pieces/chunks of your code with "repeating patterns' which can be converted into void functions. See the pre-lab for a good example. Implementation Hints: A double colon before a variable name will access the global variable with that name.Explanation / Answer
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
//To Store amount in hand
double currency;
//Items name available in store
string ItemName[4] = {"Lux", "Cinthol", "Pepsi", "Sprite"};
//Item price for the items
double ItemPrice[4] = {50.00, 60.00, 90.00, 80.00};
//Current availability of stock in store
int count = 4;
//Validates the choice and returns true or false
bool validate(int ch)
{
//If choice is 5 exit
if(ch == 5)
exit(0);
//If choice is less than 1 or greater than 5 then invalid
if(ch < 1 || ch > 5)
{
cout<<" Invalid!";
return false;
}//End of if
//If the item name contains not available then return false
if(ItemName[ch-1].compare("Not Available.") == 0)
{
cout<<" Item no longer available!";
return false;
}//End of if
//If the currently available amount is less than the purchased item then return false
if(currency < ItemPrice[ch])
{
cout<<" You have Insufficient balance";
cout<<" Item Cost: "<<ItemPrice[ch-1]<<" You have: "<<currency;
return false;
}//End of if
//Otherwise returns true
return true;
}//End of function
//Operation on Store
void displayItems()
{
int ch = 0, x;
//Repeats till Store is out of stock
//Till count is not equals to 0
do
{
cout<<" We have "<<count<<" items for sale: ";
//Displays the item name and price information
for(x = 0; x < 4; x++)
{
if(ItemName[x].compare("Not Available.") == 0)
cout<<endl<<(x + 1)<<". "<<ItemName[x];
else
cout<<endl<<(x + 1)<<". ("<<ItemName[x]<<") Price: "<<ItemPrice[x];
}//End of for
cout<<endl<<(x + 1)<<". Exit Store";
cout<<" You have "<<currency<<" (currency)";
cout<<" Which item would you like to purchase ?";
cin>>ch;
//Calls the validate method.
if(validate(ch))
{
cout<<" Thank You!";
//If it returns true display the item name and price
cout<<" You purchased "<<ItemName[ch-1]<<" for "<<ItemPrice[ch-1];
//Update the item name by not available
ItemName[ch-1] = "Not Available.";
//Decrease the count by one
count--;
//Subtract the item price from the currently available currency
currency -= ItemPrice[ch-1];
}//End of if
}while(count != 0);
cout<<" You have "<<currency<<" (currency) left.";
cout<<" My shop is out of items.";
cout<<" Thank you for shopping, please come again!";
}//End of function
//Main function
int main()
{
char ch;
cout<<" Welcome to the Berhampur Marketplace!";
cout<<" How much (currency) have you brought today?";
cin>>currency;
cout<<" Unfortunately only the Pyari Shop is open today.";
cout<<" Would you like to enter Pyari Shop? (Y/N)";
cin>>ch;
if(ch == 'Y' || ch == 'y')
{
cout<<" Welcome to the Pyari Shop.";
displayItems();
}//End of if
}//End of main
Output:
Welcome to the Berhampur Marketplace!
How much (currency) have you brought today?500
Unfortunately only the Pyari Shop is open today.
Would you like to enter Pyari Shop? (Y/N)y
Welcome to the Pyari Shop.
We have 4 items for sale:
1. (Lux) Price: 50
2. (Cinthol) Price: 60
3. (Pepsi) Price: 90
4. (Sprite) Price: 80
5. Exit Store
You have 500 (currency)
Which item would you like to purchase ?1
Thank You!
You purchased Lux for 50
We have 3 items for sale:
Not Available.
2. (Cinthol) Price: 60
3. (Pepsi) Price: 90
4. (Sprite) Price: 80
5. Exit Store
You have 450 (currency)
Which item would you like to purchase ?2
Thank You!
You purchased Cinthol for 60
We have 2 items for sale:
Not Available.
Not Available.
3. (Pepsi) Price: 90
4. (Sprite) Price: 80
5. Exit Store
You have 390 (currency)
Which item would you like to purchase ?3
Thank You!
You purchased Pepsi for 90
We have 1 items for sale:
Not Available.
Not Available.
Not Available.
4. (Sprite) Price: 80
5. Exit Store
You have 300 (currency)
Which item would you like to purchase ?4
Thank You!
You purchased Sprite for 80
You have 220 (currency) left.
My shop is out of items.
Thank you for shopping, please come again!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.