In: Computer Science
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:
Computer should ask “How many cans do you want?”
Computer should ask “How many bags do you want?”
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.
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: