In: Computer Science
3. Write a program that simulates a vending
machine. The user will be prompted to enter a number
then a letter. As part of the prompt you must display a message to
indicate the number and letter, along with the possible choices.
That is, what selections will give user which item. If
the user enters a number or a letter that is not valid, then
display the message “Bad Entry” and end the program. (100
pts)
Selections with messages.
1a - display the message “Here are your chips”
2a - display the message “Here is your soda”
3a – display the message “Here are your gummy worms”
1b – display the message “Here is your candy bar”
2b – display the message “Here is your popcorn”
3b – display the message “Here are your pretzels”
Program
import java.util.Scanner;
import java.lang.*;
class VendingMachine
{
public static void main(String[] args)
{
// declaring Scanner class object
Scanner scObj = new Scanner(System.in);
String choice;
// do while
do{
// printing prompt message to user
System.out.println(" \n1a - display the message “Here are your chips \n2a- display the message “Here is your soda \n3a- display the message “Here are your gummy worms \n1b- display the message “Here is your candy bar \n2b- display the message “Here is your popcorn \n3b- display the message “Here are your pretzels");
System.out.print("Enter your choice ");
// takes the input choice
choice = scObj.next();
switch(choice){
// if choice is any of the following case prints fhe respective message
case "1a":
System.out.print("Here are your chips");
break;
case "2a":
System.out.print("Here is your soda");
break;
case "3a":
System.out.print("Here are your gummy worms");
break;
case "1b":
System.out.print("Here is your candy bar");
break;
case "2b":
System.out.print("Here is your popcorn");
break;
case "3b":
System.out.print("Here are your pretzels");
break;
// none of the case will print the default case
default:
System.out.print("Bad Entry");
System.exit(0);
}
// continuee the loop until
} while(choice !="1a" && choice !="2a" && choice !="3a" && choice !="1b" && choice !="2b" && choice !="3b");
}
}
output screen