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.
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.
Use this implementation:
- Use two separate files –
VendingMachineDriver
that holdsmain
and one that holds aVendingMachine
class definition. - Within the
VendingMachine
class, include only one variable,paymentSum
. It holds the sum of money inserted for the current selection. In the interest of encapsulation, use local variables, as opposed to instance variables, whenever possible. Declare a variable locally within a method if the variable needs to persist only for the duration of a particular method call. Declare a variable as an instance variable if the variable needs to persist longer than the duration of a particular method call. For theVendingMachine
class, the sum of the inserted money is the only thing that needs to persist longer than the duration of a particular method call. - Within the
VendingMachine
class, include these methods:insertMoney
– 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.selectItem
– 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.
Try to have your program output match as closely as possible the sample output provided.
Use this main
method
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char choice;
boolean done = false;
VendingMachine vm = new VendingMachine();
System.out.println("Welcome to John's vending machine!\n");
do {
System.out.println("Options: (i)nsert money, (s)elect an item, (q)uit");
System.out.print("Enter i, s, or q ==> ");
choice = in.nextLine().charAt(0);
switch (choice) {
case 'i', 'I' -> vm.insertMoney();
case 's', 'S' -> vm.selectItem();
case 'q', 'Q' -> done = true;
default -> System.out.println("Invalid selection.");
}
} while(!done);
}
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:
Commit and Push your code to GitHub Classroom.