Question

In: Computer Science

3. Write a program that simulates a vending machine.   The user will be prompted to enter...

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”

Solutions

Expert Solution

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


Related Solutions

Write a program that simulates a vending machine. The machine holds six snack items labeled them...
Write a program that simulates a vending machine. The machine holds six snack items labeled them 1 through 6. The program should initially display a menu of items along with their prices: Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 The program then should ask the user to enter the item to purchase along with a sum of money....
Write a java console application,. It simulates the vending machine and ask two questions. When you...
Write a java console application,. It simulates the vending machine and ask two questions. When you run your code, it will do following: Computer output: What item you want? User input: Soda If user input is Soda Computer output: How many cans do you want? User input:            3 Computer output: Please pay $3.00. END The vending machine has 3 items for sale: Soda the price is $1.50/can. Computer should ask “How many cans do you want?” Chips, the price is $1.20/bag....
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a java program that calculates a speeding fine. The user is prompted for the speed...
Write a java program that calculates a speeding fine. The user is prompted for the speed of the vehicle, the posted speed limit and if the offence took place in the construction zone. The fine is calculated at $75 + 3$/ km over the speed limit for the first 20km over + $6 / km for the next 20 and $9/km after that. If the posted limit is 40km, the fine is doubled. If the offence took place in a...
Write a program that asks the user to enter 3 grades and computes the minimum and...
Write a program that asks the user to enter 3 grades and computes the minimum and the maximum of those 3 grades and prints it. Hint: Use the Math.min() and Math.max() methods. This program will compute the smallest and highest of 3 grades entered by the user. Enter 3 grades separated by a space: 100 85.3 90.5 Smallest: 85.3 Highest: 100.0 Bye
Exercise 3 – Strings Using a function Write a program that prompts the user to enter...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”. Example1: Input1: my dog and myself are going to my friend Input2: my Output: 0 11 31 Example 2: Input1: Programming is fun Input 2: my Output: not found
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT