Question
Help Programming assignment, please test the code in netbeans to see if it works. Vending Machine ***: Write a program that mimics the operations of
Help Programming assignment, please test the code in netbeans to see if it works.
Vending Machine ***:
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 thats returned to the user.
Use this implementation:
Use two separate files one that holds main and one that holds a VendingMachine 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 the VendingMachine 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.
selectItem This method first checks to make sure that some money has been inserted into the machine. If thats the case, the method prompts the user to enter the selected items 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.
Within the VendingMachineDriver classs main method, use a loop that continues until the user enters q. Inside the loop, use a switch statement with case clauses for the three choices insert money, select an item, quit.
Use this main method:
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
char choice; // user's choice of action
boolean done = false; // flag that says user wants to quit
VendingMachine vm = new VendingMachine();
System.out.println("Welcome to John's vending machine! ");
do
{
System.out.println(
"Options: (i)nsert money, (s)elect an item," + " (q)uit");
System.out.print("Enter i, s, or q ==> ");
choice = stdIn.nextLine().charAt(0);
switch (choice)
{
case 'i': case 'I':
vm.insertMoney();
break;
case 's': case 'S':
vm.selectItem();
break;
case 'q': case 'Q':
done = true;
break;
default:
System.out.println("Invalid selection.");
} // end switch
} while (!done);
} // end main
This UML class diagram identifies the classes and their members.
Sample session:
Welcome to John's vending machine!
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> I
Amount of money inserted: 5
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> s
Selected item's price: 2.94
Your change is:
8 quarter(s)
0 dime(s)
1 nickle(s)
1 penny(ies)
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> x
Invalid selection.
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> s
Sorry, you can't select, since you haven't inserted money yet.
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> i
Amount of money inserted: 1
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> i
Amount of money inserted: 0
Invalid payment. Must enter a positive number.
Amount of money inserted: .25
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> s
Selected item's price: 1.5
Invalid selection. Price exceeds payment.
Selected item's price: 0
Invalid price. Must enter a positive number.
Selected item's price: 1.13
Your change is:
0 quarter(s)
1 dime(s)
0 nickle(s)
2 penny(ies)
Options: (i)nsert money, (s)elect an item, (q)uit
Enter i, s, or q ==> q
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started