So I am making a vending machine and am having trouble knowing what exactly to use or how to go about giving the user a error message depending if they initially added enough money for their choice of drink. So if the user only input 1 dollar, and the drink they select is $1.25, they need to add the $.25, but how do I implement that in this code?
Here is my code along with the zip file just in case.
package vending.sample;
import java.util.Scanner;
public class VendingSample {
public static void main(String[] args) {
int coke = 1, sprite = 2, DrPepper = 3, Pepsi = 4, Fanta = 5, Water = 6, selection, i;
double change, total;
//boolean bool = true;
Scanner scan = new Scanner (System.in);
do {
total = calTotal();
if (total > 0 && total < 20)
System.out.println("You entered: " + total );
else
System.out.println("Please restart and enter a proper ammount");
System.out.println("\nPlease make a drink selection: \n"
+ "1 = Coke ($1.25)\n2 = Sprite($1.00)\n3 = DrPepper($1.35)\n4 = Pepsi($1.25)\n5 = Fanta($.75)"
+ "\n6 = Water($1.50)");
selection = scan.nextInt();
//insuf();
if (total < selection)
System.out.println("Please enter correct ammount \n\n");
if (selection == 1){
System.out.println("You selected: Coke\n\n");
if (total < 1.25)
{
System.out.println("Please enter more money");
System.out.println("How much would you like to add?");
}
change = total - 1.25;
System.out.println("Your change is $" + change + "\n\n");
}
else if (selection == 2)
{
System.out.println("You selected: Sprite\n\n");
change = total - 1;
System.out.println("Your change is $:" + change + "\n\n");
}
else if (selection == 3)
{
System.out.println("You selected: DrPepper\n\n");
change = total - 1.35;
System.out.println("Your change is $:" + change + "\n\n");
}
else if (selection == 4)
{
System.out.println("Your choice was Pepsi");
change = total - 1.25;
System.out.println("Your change is $:" + change + "\n\n");
}
else if (selection == 5)
{
System.out.println("Your choice was Fanta");
change = total - .75;
System.out.println("Your change is $:" + change + "\n\n");
}
else if (selection == 6)
{
System.out.println("Your choice was Water");
change = total - 1.50;
System.out.println("Your change is $:" + change + "\n\n");
}
else
System.out.println("Please enter valid selection.");
System.out.println("Would you like to order another drink? (1 = y / 2 = n)");
i = scan.nextInt();
System.out.println("\n \n \n");
} while (i == 1);
}
static double calTotal()
{
Scanner scan = new Scanner (System.in);
double dollar, quarters, dimes, nickles, pennies, total;
System.out.println("Please enter ammount of Dollars, Quarters, Dimes, Nickles,"
+ " and Pennies you will be entering: ");
dollar = scan.nextInt();
quarters = scan.nextInt();
dimes = scan.nextInt();
nickles = scan.nextInt();
pennies = scan.nextInt();
total = (1 * dollar) + (.25 * quarters) + (.10 * dimes) + (.05 * nickles) + (.01 * pennies);
return(total);
}
}