CSC1110 Homework 6
Before you start
Read the instructions and watch the video posted in Lab Assignment 6 for using GitHub Classroom to access and submit your program.
Use the link provided by your professor to access the GitHub repo with the starter code.
Complete the following program
Write a program that mimics the operations of a vending machine. More specifically, the program reads amounts of money that are inserted into the vending machine, asks the user to select an item, and then prints the change that’s returned to the user.
The GitHub repo contains two files: VendingMachineDriver.java and VendingMachine.java. VendingMachineDriver.java contains the main() method and should be modified.
VendingMachine.java requires the following specification:
- Contains one instance variable, paymentSum, that holds the sum of money inserted for the current selection.
- Since paymentSum will be used across multiple methods and needs to persist longer than a single method call, it makes sense to make it an instance variable. If was only used in a single method or only needed to persist in a method, we would make it a local variable to that method. In the interest of encapsulation, we want to use local variables, as opposed to instance variables, whenever possible.
- Contains a method called insertMoney() that takes in a Scanner object and returns nothing. This method prompts the user to enter an amount of money that is inserted into the machine. Input validation ensures that the user enters a positive number. The entered money amount adds to the accumulated sum of inserted money. You do not need to verify that the input is numeric.
- Contains a method called selectItem() that takes in a Scanner object and returns nothing. This method first checks to make sure that some money has been inserted into the machine. If that’s the case, the method prompts the user to enter the selected item’s price. Input validation ensures that the entered price is a positive number and that it is no greater than the accumulated inserted money. Finally, the method calculates and prints a list of the coins that are to be returned to the user as change. You do not need to verify that the input is numeric.
The following sample shows the operation of the program:
Sample Output
Welcome to John's vending machine! Options: (i)nsert money, (s)elect an item, (q)uit Enter i, s, or q ==> d Invalid selection. Options: (i)nsert money, (s)elect an item, (q)uit Enter i, s, or q ==> i Amount of money inserted: -1 Invalid payment. Must enter a positive number. Amount of money inserted: 0 Invalid payment. Must enter a positive number. Amount of money inserted: 2 Options: (i)nsert money, (s)elect an item, (q)uit Enter i, s, or q ==> s Select item's price: -1 Invalid price. Must enter a positive number. Select item's price: 0 Invalid price. Must enter a positive number. Select item's price: .32 Your change =========== 6 quarter(s) 1 dime(s) 1 nickel(s) 3 penny(ies) Options: (i)nsert money, (s)elect an item, (q)uit Enter i, s, or q ==> i Amount of money inserted: 12.43 Options: (i)nsert money, (s)elect an item, (q)uit Enter i, s, or q ==> s Select item's price: 8.74 Your change =========== 14 quarter(s) 1 dime(s) 1 nickel(s) 4 penny(ies) Options: (i)nsert money, (s)elect an item, (q)uit Enter i, s, or q ==> q
Submission Instructions:
Add DONE to the README and then Commit and Push your code to GitHub Classroom.