I missed my last comp. sci. class and have fallen a little behind, could someone
ID: 3789596 • Letter: I
Question
I missed my last comp. sci. class and have fallen a little behind, could someone please show me how to write a program for this?
The following table shows the price for coca, juice, and water:
Product
Price
coca
1.75
juice
2.50
Water
1.50
Write a program that asks the user to enter “coca”, “juice”, or “water”, and the amount of money that he/she puts into the vendor machine. The program then displays the message “ You ordered one bottle of …” and the corresponding change. For example, if the user enters “ coca” and 2 dollars, then your program displays:
“You ordered one bottle of Coca, and the change is 0.25 dollar.”
-Hint: if(input.equals("Coca"))...
Product
Price
coca
1.75
juice
2.50
Water
1.50
Explanation / Answer
//VendingMachine.java
import java.util.Scanner;
public class VendingMachine
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter drink(coca, juice, water): ");
String drink = sc.nextLine();
System.out.println("Enter the amount to put into vending machine: ");
double money = sc.nextDouble();
double change = 0.0;
double number_bottles = 0;
if(drink.equals("coca"))
{
number_bottles = money/1.75;
change = money - 1.75*(int)number_bottles;
}
else if(drink.equals("juice"))
{
number_bottles = money/2.50;
change = money - 2.50*(int)number_bottles;
}
else if(drink.equals("water"))
{
number_bottles = money/1.50;
change = money - 1.50*(int)number_bottles;
}
System.out.println("You ordered " + (int)number_bottles + " bottle of "+ drink +", and the change is " + change + " dollar ");
}
}
/*
Output:
Enter drink(coca, juice, water):
coca
Enter the amount to put into vending machine:
4.0
You ordered 2 bottle of coca, and the change is 0.5 dollar
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.