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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education