Given a snack vending machine assume the machine accept bills and coins after customer input money and select items the machine can calculate and dispose snack. Define five constraints and convert to Java code
![](/static/compass_v2/shared-icons/check-mark.png)
1. Define a VendingMachine class.
2. Initialize totalMoney, snackPrice, and snacksAvailable.
3. Create a method acceptMoney(double money) to accept money:
a. Check if the input money is valid currency:
i. If valid, add the money to totalMoney.
ii. Print "Inserted: $" + money.
iii. Otherwise, print "Invalid currency. Please insert valid bills or coins."
4. Create a method selectSnack(int snackNumber) to select a snack:
a. Check if the snackNumber is within the available range (1 to snacksAvailable):
i. If yes, check if totalMoney is greater than or equal to snackPrice:
i. If yes, deduct snackPrice from totalMoney, decrement snacksAvailable, and dispense the selected snack.
ii. Print "Dispensing snack #" + snackNumber.
iii. Print "Change: $" + totalMoney.
ii. If no, print "Insufficient funds. Please insert more money."
b. If the snackNumber is not within the valid range, print "Invalid snack selection."
5. Create a private method isValidCurrency(double money) to check if the input money is valid:
a. Check if money is equal to 1.0, 5.0, 10.0, 0.25, 0.1, or 0.05.
b. Return true if valid, false otherwise.
6. Create a VendingMachine object.
7. Enter a loop for user interaction:
a. Print "Available snacks: " + snacksAvailable.
b. Prompt the user to insert money: "Insert money (1.0, 5.0, 10.0, 0.25, 0.1, 0.05): $".
c. Read the user's input as,money.
iii. Otherwise, print "Invalid currency. Please insert valid bills or coins."
4. Create a method selectSnack(int snackNumber) to select a snack:
a. Check if the snackNumber is within the available range (1 to snacksAvailable):
i. If yes, check if totalMoney is greater than or equal to snackPrice:
i. If yes, deduct snackPrice from totalMoney, decrement snacksAvailable, and dispense the selected snack.
ii. Print "Dispensing snack #" + snackNumber.
iii. Print "Change: $" + totalMoney.
ii. If no, print "Insufficient funds. Please insert more money."
b. If the snackNumber is not within the valid range, print "Invalid snack selection."
5. Create a private method isValidCurrency(double money) to check if the input money is valid:
a. Check if money is equal to 1.0, 5.0, 10.0, 0.25, 0.1, or 0.05.
b. Return true if valid, false otherwise.
6. Create a VendingMachine object.
7. Enter a loop for user interaction:
a. Print "Available snacks: " + snacksAvailable.
b. Prompt the user to insert money: "Insert money (1.0, 5.0, 10.0, 0.25, 0.1, 0.05): $".
c. Read the user's input as money.
d. Call the acceptMoney(money) method to process the input.
e. Prompt the user to select a snack: "Select a snack (1-10): ".
f. Read the user's input as snackNumber.
g. Call the selectSnack(snackNumber) method to process the snack selection.
8. End the loop when necessary.
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)