-2

hey I'm writing a console app in java that is a vending machine. I have to have 2 vend() methods and a insertMoney() method. Right now I only have a insertMoney() method and I'm having a problem calling it. The error I get is that when it runs through I can't get the variable 'credit' to update with the amount entered from the user.

        package javaapplication3;

       import java.util.Scanner;

        public class CandyMachine {
        Scanner getinput = new Scanner(System.in);

        //variables for money
         static int quarters;
        static int dollars;
        static int dimes;
        static int nickels;
       public static int credit;
       //variables for stock of candy
      static int twix = 10;
       static int snickers = 5;
        static int skittles = 8;

      public static void main(String[] args) {
         Scanner input = new Scanner(System.in);
       while (true) {
        System.out.println("*******************************************");
        System.out.println("Vending options are as follows...");
        System.out.println("0: Enter Money: ");
        System.out.println("1: twix 2.00 dollars");
        System.out.println("2 snickers 1.00 dollars");
        System.out.println("3 skittles 2.50 dollars");
        System.out.println("4 Return Change");
        System.out.println("*********************************************");
        int userSelection = input.nextInt();

        switch (userSelection) {
            case 0:
                insertMoney();
                break;
            case 1:
                System.out.println("Enter 2.00 dollars please");
                if (credit >= 200) {
                    twix -= 1;
                } else {
                    System.out.println("Enter more money");
                }
                break;
            case 2:
                System.out.println("Eneter 1.00 dollar please");
                if (credit >= 200) {
                    snickers -= 1;
                } else {
                    System.out.println("Enter more money");
                }
                break;
            case 3:
                System.out.println("Enter 2.50 dollars please");
                if (credit >= 250) {
                    skittles -= 1;
                } else {
                    System.out.println("Enter more money");
                }
                break;
            case 4:
                if (credit > 0) {
                    System.out.println("Hack me for your money back :) ");
                }
                break;
            default:
                System.out.println("Please enter a correct number you anarchist.");
                break;
        }




    }
}

public static void insertMoney() {
    int moneySelection;
    Scanner getinput = new Scanner(System.in);
    do {
        System.out.println("1: Enter quarters");
        System.out.println("2: Enter dimes");
        System.out.println("3: Enter nickels");
        System.out.println("4: Enter Dollars");
        System.out.println("5: When all money is entered");
        moneySelection = getinput.nextInt();

        switch (moneySelection) {
            case 1:
                System.out.println("How many quarters? ");
                int userQuarters = getinput.nextInt();
                credit = quarters * 25;
                //break;
            case 2:
                System.out.println("How many dimes?");
                int userdimes = getinput.nextInt();
                credit = dimes * 10;
                //break;
            case 3:
                System.out.println("How many Nickels?");
                int userNickels = getinput.nextInt();
                credit = nickels * 5;
                //break;
            case 4:
                System.out.println("How many Dollars?");
                int userDollar = getinput.nextInt();
                credit = credit * 100;
                //break;
            case 5:
                System.out.println("Thank you");
                break;
        }            

    //return credit;
} while (moneySelection != 5);

}

}

odus
  • 48
  • 8

1 Answers1

0

You get an error because insertMoney() is not static and you call it without specifying an instance. You can either make it static or call by referring to the instance of the class e.g.

input.insertMoney()

Everything that is static inside your class exists only once in the application. Everything that is non-static exists once per instance of your class. You have to choose if you want your class to be used in a static way or not. The main method of your application is the entry point of the application and there must only be one of them so it must always be static.

I note that the first line of your main method is:

Scanner input = new Scanner(System.in);

This creates an instance of your class. If you want everything to be static you would not need this line as you do not need an instance to call static functions or access static variables. If you do create an instance you then access each function by writing instancename.Function e.g.

int userSelection = input.nextInt();

This way of doing it uses a specific instance of the Scanner. Every variable inside the each instance is separate so if you had the following:

Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);

Then you can call:

int userSelection = input1.nextInt();
int userSelection = input2.nextInt();
int userSelection = input3.nextInt();

and all the values of the internal variables (quarters, dollars, etc.) can be different in each instance. This would be useful if you wanted to model multiple vending machines. (They can all hold different coins and have different stock levels)

The normal way of doing it would be to have every variable and method in your class non-static. (Apart from the main function). This makes your code more expandable in the future.

Robert3452
  • 1,354
  • 2
  • 17
  • 39