The method sellProduct of the Juice Machine programming example gives the user o
ID: 3865319 • Letter: T
Question
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. Also, move the showSelection and sellProduct functions from the main() program to be members of the cashRegister class and reside in cashRegister implementation file, and add another juice type named "grape". The instructions indicate the requirement for a test program, however, the main() program should be sufficient and an additional test program unnecessary
This is what I have so far:
#include
#include "cashRegister.h"
#include "dispenserType.h"
using namespace std;
void showSelection();
void sellProduct(dispenserType& product,
cashRegister& pCounter);
int main()
{
cashRegister counter;
dispenserType orange(100, 50);
dispenserType apple(100, 65);
dispenserType mango(75, 80);
dispenserType strawberryBanana(100, 85);
int choice; //variable to hold the selection
showSelection();
cin >> choice;
while (choice != 9)
{
switch (choice)
{
case 1:
sellProduct(orange, counter);
break;
case 2:
sellProduct(apple, counter);
break;
case 3:
sellProduct(mango, counter);
break;
case 4:
sellProduct(strawberryBanana, counter);
break;
default:
cout << "Invalid selection." << endl;
}//end switch
showSelection();
cin >> choice;
}//end while
return 0;
}//end main
void showSelection()
{
cout << "*** Welcome to Shelly's Juice Shop ***" << endl;
cout << "To select an item, enter " << endl;
cout << "1 for orange juice (50 cents)" << endl;
cout << "2 for apple juice (65 cents)" << endl;
cout << "3 for mango juice (80 cents)" << endl;
cout << "4 for strawberry banana juice (85 cents)" << endl;
cout << "9 to exit" << endl;
}//end showSelection
void sellProduct(dispenserType& product,
cashRegister& pCounter)
{
int amount; //variable to hold the amount entered
int amount2; //variable to hold the extra amount needed
if (product.getNoOfItems() > 0) //if the dispenser is not
//empty
{
cout << "Please deposit " << product.getCost()
<< " cents" << endl;
cin >> amount;
if (amount < product.getCost())
{
cout << "Please deposit another "
<< product.getCost()- amount
<< " cents" << endl;
cin >> amount2;
amount = amount + amount2;
}
if (amount >= product.getCost())
{
pCounter.acceptAmount(amount);
product.makeSale();
cout << "Collect your item at the bottom and "
<< "enjoy." << endl;
}
else
cout << "The amount is not enough. "
<< "Collect what you deposited." << endl;
cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
<< endl << endl;
}
else
cout << "Sorry, this item is sold out." << endl;
cashRegister.h file:
#include <iostream>
#include "cashRegister.h"
using namespace std;
int cashRegister::getCurrentBalance() const
{
return cashOnHand;
}
void cashRegister::acceptAmount(int amountIn)
{
cashOnHand = cashOnHand + amountIn;
}
cashRegister::cashRegister(int cashIn)
{
if (cashIn >= 0)
cashOnHand = cashIn;
else
cashOnHand = 500;
}
dispenserType file:
#include <iostream>
#include "dispenserType.h"
using namespace std;
int dispenserType::getNoOfItems() const
{
return numberOfItems;
}
int dispenserType::getCost() const
{
return cost;
}
void dispenserType::makeSale()
{
numberOfItems--;
}
dispenserType::dispenserType(int setNoOfItems, int setCost)
{
if (setNoOfItems >= 0)
numberOfItems = setNoOfItems;
else
numberOfItems = 50;
if (setCost >= 0)
cost = setCost;
else
cost = 50;
}
Explanation / Answer
Answer for the given Question:
See the below updated code for add new fruit juice type is grape in main method and selection menu.
Moved showSelection and sellProduct methods to cashRegister class see the definations.
see the below updated methods and main function as per given problem statement
// Moved showSelection and sellProduct methods inside cashRegister class
void cashRegister::showSelection()
{
cout << "*** Welcome to Shelly's Juice Shop ***" << endl;
cout << "To select an item, enter " << endl;
cout << "1 for orange juice (50 cents)" << endl;
cout << "2 for apple juice (65 cents)" << endl;
cout << "3 for mango juice (80 cents)" << endl;
cout << "4 for strawberry banana juice (85 cents)" << endl;
cout << "5 for Grape juice (90 cents)" << endl; // Added new Juice selection in menu
cout << "9 to exit" << endl;
}//end showSelection
// Moved showSelection and sellProduct methods inside cashRegister class
// This method is already restricted to enough money to purchase the product
void cashRegister::sellProduct(dispenserType& product, cashRegister& pCounter)
{
int amount; //variable to hold the amount entered
int amount2; //variable to hold the extra amount needed
if (product.getNoOfItems() > 0) //if the dispenser is not empty
{
cout << "Please deposit " << product.getCost()
<< " cents" << endl;
cin >> amount;
if (amount < product.getCost())
{
cout << "Please deposit another "
<< product.getCost()- amount
<< " cents" << endl;
cin >> amount2;
amount = amount + amount2;
}
if (amount >= product.getCost())
{
pCounter.acceptAmount(amount);
product.makeSale();
cout << "Collect your item at the bottom and "
<< "enjoy." << endl;
}
else
cout << "The amount is not enough. "
<< "Collect what you deposited." << endl;
cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
<< endl << endl;
}
else
cout << "Sorry, this item is sold out." << endl;
}
#include<iostream>
using namespace std;
int main()
{
cashRegister counter;
dispenserType orange(100, 50);
dispenserType apple(100, 65);
dispenserType mango(75, 80);
dispenserType strawberryBanana(100, 85);
dispenserType grape(90, 80); // Added new Fruit
int choice; //variable to hold the selection
counter.showSelection();
cin >> choice;
while (choice != 9)
{
switch (choice)
{
case 1:
counter.sellProduct(orange, counter);
break;
case 2:
counter.sellProduct(apple, counter);
break;
case 3:
counter.sellProduct(mango, counter);
break;
case 4:
counter.sellProduct(strawberryBanana, counter);
break;
case 5:
counter.sellProduct(grape, counter); // Added new selection menu for grape juice with 90 cents
break;
default:
cout << "Invalid selection." << endl;
}//end switch
counter.showSelection();
cin >> choice;
}//end while
return 0;
}//end main
and declare the below two methods declartions inside cashRegister class
void showSelection();
void sellProduct(dispenserType& product, cashRegister& pCounter)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.