0

Need help with if else statement in Java. Need the program to say "Sorry, out of stock" when items are at 0. I tried but it wont print out "Sorry, out of stock" Can anyone explain to me how to properly set that up so when the items are at 0 the program will let the user know that the item is out of stock. Thank you.

import java.util.Scanner;

public class VendingMachine {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int Chips = 5;
        int Cookies = 4;
        int Candies = 3;
        double ChipsPrice = 1.25;
        double CookiesPrice = 0.85;
        double CandiesPrice = 0.95;

        Scanner choice = new Scanner(System.in);
        Scanner moneyIn = new Scanner(System.in);

        while (true) {
            double Change = 0;
            double Amount = 0;
            double Money = 0;

            System.out.println("Welcome to the Vending Machine");
            System.out.println("Please insert Money");
            Amount = moneyIn.nextDouble();

            //Make an if statements, such as if moneyIn equals 5 quarters then Amount = 5*0.25
            //Ask how many quarters how many nickels how many dimes

            System.out.println("What snack would you like?");
            System.out.println("Potato Chips: $" + ChipsPrice + " " + Chips + " left");
            System.out.println("Cookies: $" + CookiesPrice + " " + Cookies + " left");
            System.out.println("Candies: $" + CandiesPrice + " " + Candies + " left");
            String which = choice.nextLine();

            if (which.equals("Potato Chips")) {
                System.out.println("You selected Potato Chips: $" + ChipsPrice + " " + Chips + " left");
                if (Amount < ChipsPrice) {
                    System.out.println("Not enough money inserted");

                    if (Chips == 0) ;
                    System.out.println("Sorry, out of stock");
                } else {
                    Chips = Chips - 1;
                    Change = ChipsPrice - Amount;
                    System.out.println("Please take your chips ");
                    System.out.println("Your change is " + Change);
                }
            } else if (which.equals("Cookies")) {
                System.out.println("You selected Cookies: $" + CookiesPrice + " " + Cookies + " left");
                Cookies = Cookies - 1;

                if (Amount < CookiesPrice) {
                    System.out.println("Not enough money inserted");
                    if (Cookies == 0)
                        System.out.println("Sorry, out of stock");
                } else {
                    Cookies = Cookies - 1;
                    Change = CookiesPrice - Amount;
                    System.out.println("Please take your cookies");
                    System.out.println("Your change is " + Change);
                }
            } else if (which.equals("Candies")) {
                System.out.println("You selected Candies: $" + CandiesPrice + " " + Candies + " left");

                if (Amount < CandiesPrice) {
                    System.out.println("Not enough money inserted");
                    if (Cookies == 0)
                        System.out.println("Sorry, out of stock");
                } else {
                    Candies = Candies - 1;
                    Change = CookiesPrice - Amount;
                    System.out.println("Please take your candies");
                    System.out.println("Your change is " + Change);
                }
            } else {
                System.out.println("Please select one of the snacks below");
            }
        }
    }
}
MikaelF
  • 3,518
  • 4
  • 20
  • 33
Zain N.
  • 1
  • 1
  • 4
  • 1
    Remove the semicolon at the end of `if (Chips == 0);`. And pay attention to indentation. – shmosel Feb 09 '17 at 03:27
  • Other than that, you need different conditions for different products, it can't be always `if(Cookies == 0)`. – Siddharth Tyagi Feb 09 '17 at 03:31
  • Also, sometimes the if stmts are in the wrong place – Gab Feb 09 '17 at 03:33
  • An IDE will show you right away if you're adding a semicolon at the end of an if statement, because it won't indent the following statement. – MikaelF Feb 09 '17 at 03:36
  • @ZainN. For fun, see a more advanced version of your example code using classes and objects [here in PasteBin.com](http://pastebin.com/Yd8vyBPw). Objects make this kind of work easier, and use less code. – Basil Bourque Feb 09 '17 at 05:31

1 Answers1

1

Just to go through this, a few observations:

// It might be simpler to use a "switch" statement here
if (which.equals("Potato Chips")) {
         System.out.println("You selected Potato Chips: $"+ChipsPrice+" "+Chips+" left");
         if (Amount < ChipsPrice){
             System.out.println("Not enough money inserted");
             // Remove the semicolon - as written this won't do anything
             // Also, this condition shouldn't be here since you're not vending anyway
             // Incidentally, many people argue that you should always use curly
             // brackets, even around one-line "if" statements like this, precisely
             // to prevent errors like this
             if (Chips == 0);
             System.out.println("Sorry, out of stock");

         }
         else {
             // This can be written as Chips--;
             Chips = Chips - 1;
             // Should actually be Amount - ChipsPrice;
             // If they paid 75 cents for a 25-cent item, the change is 75 - 25 = 50 cents,
             // NOT 25 - 75 = -50 cents
             Change = ChipsPrice - Amount;
             System.out.println("Please take your chips " );
             System.out.println("Your change is "+ Change );


         }
     }
     else if (which.equals("Cookies")) {
         System.out.println("You selected Cookies: $"+CookiesPrice+" "+Cookies+" left");

         // Cookies--
         Cookies = Cookies - 1;

         if (Amount < CookiesPrice){
             System.out.println("Not enough money inserted");

             // Should be checked in the "else" statement
             if (Cookies == 0)
                 System.out.println("Sorry, out of stock");
         }
         else {
             // Cookies--
             Cookies = Cookies - 1;
             // Amount - CookiesPrice
             Change = CookiesPrice - Amount;
             System.out.println("Please take your cookies");
             System.out.println("Your change is "+ Change );

         }

     }
     else if (which.equals("Candies")) {
         System.out.println("You selected Candies: $"+CandiesPrice+" "+Candies+" left");

         if (Amount < CandiesPrice){
             System.out.println("Not enough money inserted");
             // Again, you shouldn't check this here given that you won't vend either way
             // Also, should be if (Candies == 0), NOT if (Cookies == 0)
             if (Cookies == 0)
                 System.out.println("Sorry, out of stock");
         }
         else {
             // Candies--;
             Candies = Candies - 1;
             // Should actually be Amount - CandyPrice. You use CookiesPrice instead.
             Change = CookiesPrice - Amount;
             System.out.println("Please take your candies");
             System.out.println("Your change is "+ Change );

         }
     }
     else {
         System.out.println("Please select one of the snacks below");
     }

One more thing: you're basically doing the same exact thing 3 consecutive times; in situations like that, it's usually better to try to refactor the behavior in question as a method (rather than typing it 3 separate times).