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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 20PE: When you borrow money to buy a house, a car, or for some other purpose, you repay the loan by making...
Topic Video
Question
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
Expert Solution
Step 1: Providing Algorithm of the code

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.


steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage