In: Computer Science
Please write a program in c++ The method sellProduct of the Juice Machine programming example gives the user only two chances to enter enough money to buy the product. Rewrite the definition of the method sellProduct so that it keeps prompting the user to enter more money as long as the user has not entered enough money to buy the product. Also, write a program to test your method.
Your program should produce the following example output:
*** Welcome to Shelly's Juice Shop *** To select an item, enter 1 for orange juice (50 cents) 2 for apple juice (65 cents) 3 for mango juice (80 cents) 4 for strawberry banana juice (85 cents) 9 to exit 3 Please deposit 80 cents 40 Please deposit another 40 cents. 40 Collect your item at the bottom and enjoy. *** Welcome to Shelly's Juice Shop *** To select an item, enter 1 for orange juice (50 cents) 2 for apple juice (65 cents) 3 for mango juice (80 cents) 4 for strawberry banana juice (85 cents) 9 to exit 9
#include<iostream>
using namespace std;
//calling the method sell product
int sellProduct(int select)
{
int price,temp;
//condition to check selection
if(select == 1)
{
//for select 1 prompt to
enter 50 cents
cout << "Please
deposit 50 cents\n";
cin >>
price;
//prompt the user,untill
user deposited enough money
while(price != 50)
{
cout << "Please deposit another " << 50-price<<"
cents\n";
temp = price;
cin >> price;
price = temp + price;
}
}
else if(select == 2)
{
//for selection 2,prompt
to enter 65 cents
cout << "Please
deposit 65 cents\n";
cin >>
price;
//prompt the user,untill
user deposited enough money
while(price != 65)
{
cout << "Please deposit another " << 65-price<<"
cents\n";
temp = price;
cin >> price;
price = temp + price;
}
}
else if(select == 3)
{
//for selection 3,prompt
to enter 80 cents
cout << "Please
deposit 80 cents\n";
cin >>
price;
//prompt the user,untill
user deposited enough money
while(price != 80)
{
cout << "Please deposit another " << 80-price<<"
cents\n";
temp = price;
cin >> price;
price = temp + price;
}
}
else if(select == 4)
{
//for selection 4,prompt
to enter 85 cents
cout << "Please
deposit 85 cents\n";
cin >>
price;
//prompt the user,untill
user deposited enough money
while(price != 85)
{
cout << "Please deposit another " << 85-price<<"
cents\n";
temp = price;
cin >> price;
price = temp + price;
}
}
else if(select == 9)
{
// if user select
9,then exit the code
cout
<<"Exit";
return -1;
}
else
{
//if user enter wrong
selection
cout
<<"Wrong selection\n";
return
0;
}
return 0;
}
int main()
{
int select,price,i;
//do- while loop doesn't check the condition in
first iteration.From second iteration it checks the condition
//loop used to prompt the user,until user want
to exit
do
{
cout << "***
Welcome to Shelly's Juice Shop ***\n";
cout << "To select
an item,enter\n";
cout << "1 for
orange juice (50 cents)\n";
cout << "2 for
apple juice (65 cents)\n";
cout << "3 for
mango juice (80 cents)\n";
cout << "4 for
strawberry banana juice (85 cents)\n";
cout << "9 to
exit\n";
cin >>
select;
//calling the method
sellproduct()
i =
sellProduct(select);
//This condition to exit
the program
if(i== -1)
return 0;
cout << "Collect
your item at the bottom and enjoy\n\n";
}while(i != -1);
}
Continued from above picture