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 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 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 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...
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...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a program that determines the change to be dispensed from a vending machine. An item...
Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and 1 dollar, in 5-cents increments (25, 30, 35, . . . 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. (Explain with algorithms if you can please!)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT