Question

In: Computer Science

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:

  1. Soda the price is $1.50/can.

Computer should ask “How many cans do you want?”

  1. Chips, the price is $1.20/bag.

Computer should ask “How many bags do you want?”

  1. Juice, the price is $1.50/bar.

Computer should ask “How many bars do you want?”

When user type in different quantities, the computer should print out the total due amount.

The 2nd one is the quantity of the items.

Solutions

Expert Solution

Solution code is given below with comments:

import java.util.*;

public class Main
{
   public static void main(String[] args) {
       // Scanner object for input from console.
       Scanner sc = new Scanner(System.in);
      
       // Setting price for the items.
       double sodaPricePerCan = 1.50;
       double chipsPricePerBag = 1.20;
       double juicePricePerBar = 1.50;
      
       // Takes input for the item user wants.
       System.out.println("What item you want?");
       String input = sc.nextLine();
       int quantity;
       double total;
      
       // Using equals method of String and then continuing with the
       // next question of How many items do you want
       if(input.equals("Soda")) {
       System.out.println("How many cans do you want?");
       quantity = sc.nextInt();
       total = quantity*sodaPricePerCan;
       System.out.println("Please pay $"+total+".");
       }
       else if(input.equals("Chips")) {
       System.out.println("How many bags do you want?");
       quantity = sc.nextInt();
       total = quantity*chipsPricePerBag;
       System.out.println("Please pay $"+total+".");
       }
       else if(input.equals("Juice")) {
       System.out.println("How many bars do you want?");
       quantity = sc.nextInt();
       total = quantity*juicePricePerBar;
       System.out.println("Please pay $"+total+".");
       }
   }
}

Screenshots:


Related Solutions

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...
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....
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
Coding Java Vending machine Lab
Given two integers as user inputs that represent the number of drinks to buy and the number of bottles to restock, create a VendingMachine object that performs the following operations:Purchases input number of drinksRestocks input number of bottlesReports inventoryThe VendingMachine is found in VendingMachine.java. A VendingMachine's initial inventory is 20 drinks.Ex: If the input is:5 2the output is:Inventory: 17 bottles import java.util.Scanner; public class LabProgram {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);              /* Type your code here. */            } }
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Java - Create a program that simulates a slot machine. When the program runs, it should...
Java - Create a program that simulates a slot machine. When the program runs, it should do the following: - Ask the user to enter the amount of money he or she wants to enter into the slot machine. - Instead of displaying images, have the program randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars (To select a word, the program can generate a random number in the range of 0 through 5. If...
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
In this assessment, you will debug and fix a given Java console application that uses 2...
In this assessment, you will debug and fix a given Java console application that uses 2 dimensional arrays but the application does not compile nor execute. You can use either the Toolwire environment or your local Java development environment to complete this assignment.The application has four bugs. Your assignment is to find these bugs and fix them so that the application meets its stated requirements.The requirements of this application are as follows: The application is register students for courses in...
In this assessment, you will debug and fix a given Java console application that uses 2...
In this assessment, you will debug and fix a given Java console application that uses 2 dimensional arrays but the application does not compile nor execute. You can use either the Toolwire environment or your local Java development environment to complete this assignment.The application has four bugs. Your assignment is to find these bugs and fix them so that the application meets its stated requirements.The requirements of this application are as follows: The application is register students for courses in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT