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

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 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
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...
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
What factors should you consider when choosing between a console application and a GUI application?
What factors should you consider when choosing between a console application and a GUI application?
Make a Console application Language should be Visual Basic You will be simulating an ATM machine...
Make a Console application Language should be Visual Basic You will be simulating an ATM machine as much as possible Pretend you have an initial deposit of 1000.00. You will Prompt the user with a Main menu: Enter 1 to deposit Enter 2 to Withdraw Enter 3 to Print Balance Enter 4 to quit When the user enters 1 in the main menu, your program will prompt the user to enter the deposit amount. If the user enter more than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT