3 Replies - 841 Views - Last Post: 06 November 2016 - 03:24 AM Rate Topic: -----

#1 CrypticZero   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 16

Getting many cannot find variable errors in ATM Class

Posted 05 November 2016 - 08:40 PM

Hi all, so I am relatively new to java, just started a class this year and overall I like it. However, every so often, my teacher gives us a project which gives me some issues. This week, she wants us to create an ATM machine, which has a log in, a pin to enter, and after those two steps, the options to check account balance starting at 0 dollars, the option to deposit any amount of money, and the option to withdraw money only by multiples of 20 dollars. The account is not allowed to go negative. Then an option to logout must also be present. However, I am having some issues. I have accomplished the login steps that were requested, however, the options in the account for balance, withdraw, and deposit do not work. If anyone could help me fix these issues, it would be very much appreciated. I have been working on separating my code into methods too, so I am not putting lot and lots of code into my launcher class, as I know that is an issue I have, so feel free to criticize me there as well. My main problem right now when I try to compile is I get an error telling me that it cannot find my options variable. Although, I believe there may be more problems after that issue is fixed, which would probably be finding the other variables used since I wrote them all similarly. Here is my code:

import java.io.*;
import java.util.*;
public class aTMLauncher
{
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        aTMTester atm = new aTMTester();
        
        System.out.println("Login: ");
        System.out.print("Username > ");
         
        String usernameInput=input.nextLine();
        int count=4;
        while (!usernameInput.equals(aTMTester.getUsername()))
        {
            System.out.println("\nIncorrect input. Please try again. You have " + (count-1) + " remaining attempts.");
            count--;
            if(count==0)
            {
                System.out.println("No remain attempts, system will now exit");
                System.exit(0);
            }
            System.out.print("Username > ");
            usernameInput = input.nextLine();
        }
         
        System.out.print("Pin > ");
        int PINInput=input.nextInt();
        int count1=4;
        while (PINInput<aTMTester.getPIN() || PINInput>aTMTester.getPIN())
        {
            System.out.println("\nIncorrect input. Please try again. You have " + (count1-1) + " remaining");
            count1--;
            if(count1==0)
            {
                System.out.println("No remain attempts, system will now exit");
                System.exit(0);
            }
            System.out.print("Pin > ");
            PINInput = input.nextInt();
        }
         
        options = input.nextInt();
         
        while(PINInput==2468)
        {
            atm.main();
            options = input.nextInt();
            if (options==4)
            {
                atm.logout();
            }
            else if(options==3)
            {
                System.out.println("How much money would you like to deposit?");
                System.out.print("$ ");
                deposit = input.nextInt();
                atm.balance();
            }
            else if(option==2)
            {
                System.out.println("How much money will you be withdrawing? \n(Only amounts divisible by 20 are accepted)");
                deposit= input.nextInt();
                atm.withdraw1();
                while(withdraw1%20<0 || withdraw%20>0)
                {
                    System.out.println("Inalid withdraw amount. Please retry.");
                    System.out.println("How much money will you be withdrawing? \n(Only amounts divisible by 20 are accepted)");
                    deposit= input.nextInt();
                    atm.withdraw1();
                }
            }
            else if(option==1)
            {
                System.out.println("Your account balance is "+balance);
            }
        }
    }
}

public class aTMTester
{
    private static final String username = "David";
    private static final int PIN = 2468;
    private int balance=0;
    private int options, deposit, withdraw;
    private String menu;
     
    /*
     * Default constructor
     */
    
    public aTMTester()
    {
    }
     
    public static String getUsername()
    {
        return username;
    }
 
    public static int getPIN()
    {
        return PIN;
    }
     
    public int getOptions()
    {
        return options;
    }
     
    public static void main()
    {
        System.out.println("\nWelcome," + username +"!");
         
        System.out.println("+++++++++++Account: "+ username +"+++++++++++");
        System.out.println("1. Check Account Balance");
        System.out.println("2. Deposit Checks");
        System.out.println("3. Withdraw Money");
        System.out.println("4. Logout");
         
        System.out.print("\nWhat would you like to do next?\n");
    }
     
    public static void logout()
    {
        System.out.println("Thanks for usinging the David Vachlon Inc. ATM. We hope you expierence was fast and simple! Have a great day.");
        System.exit(0);
    }
     
    public static void balance()
    {
        return balance;
    }
     
    public static int deposit()
    {
        balance+=deposit;
    }
     
    public static int  withdraw()
    {
        balance-=withdraw;
    }
     
    public static int withdraw1()
    {
        balance%20=0;
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Getting many cannot find variable errors in ATM Class

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Getting many cannot find variable errors in ATM Class

Posted 06 November 2016 - 12:14 AM

you don't appear to have declared the variable options, e.g. declare it as in int
int options = input.nextInt();

Was This Post Helpful? 0
  • +
  • -

#3 CrypticZero   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 16

Re: Getting many cannot find variable errors in ATM Class

Posted 06 November 2016 - 03:12 AM

View Posthorace, on 06 November 2016 - 12:14 AM, said:

you don't appear to have declared the variable options, e.g. declare it as in int
int options = input.nextInt();


I have been able to get a working, compiling code. Thanks for the help. However, I do have one more question if you have the time to answer it. I was wondering where in my code could I put a inputMismatchException. I only just heard about that and that would solve the problem of my code always giving me an error when I accidentally don't input an integer. I have been trying to use it for myself for the past hour but I cant seem to find the right spot to put it. If you could help me figure out where to put that, so that I dont get that error, that would really help. Here is my working, fully functional code:

import java.io.*;
import java.util.*;
public class aTMLauncher
{
    public static int options=0;
    public static int withdraw, deposit;
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        aTMTester atm = new aTMTester();
        
        System.out.println("Login: \n(case sensitive)");
        System.out.print("Username > ");
         
        String usernameInput=input.nextLine();
        int count=4;
        while (!usernameInput.equals(aTMTester.getUsername()))
        {
            System.out.println("\nIncorrect input. Please try again. You have " + (count-1) + " remaining attempts.");
            System.out.println("Login: \n(case sensitive)");
            System.out.print("Username > ");
            count--;
            if(count==0)
            {
                System.out.println("No remaining attempts, system will now exit");
                System.exit(0);
            }
            usernameInput = input.nextLine();
        }

        System.out.print("Pin > ");
        int PINInput=input.nextInt();
        int count1=4;
        
        while (PINInput<aTMTester.getPIN() || PINInput>aTMTester.getPIN())
        {
            System.out.println("\nIncorrect input. Please try again. You have " + (count1-1) + " trys remaining");
            System.out.print("Pin > ");
            count1--;
            if(count1==0)
            {
                System.out.println("No remaining attempts, system will now exit");
                System.exit(0);
            }
            PINInput=input.nextInt();
        }
        
        System.out.println("\nWelcome, " + aTMTester.getUsername() +"!");
        
        while(PINInput==2468)
        {
            atm.main();
            options = input.nextInt();
            if (options==4)
            {
                atm.logout();
            }
            
            else if(options==3)
            {
                boolean ok = false;
                while (!ok) {
                    System.out.print("How much money will you be withdrawing? \n(Only amounts divisible by 20 are accepted)\n$");
                    atm.withdraw = input.nextInt();
                    if (atm.withdraw % 20 != 0) {
                        System.out.println("Invalid withdraw amount. \nPlease retry.");
                    } else if (atm.balance < atm.withdraw) {
                        System.out.println("You are not allowed to take out more then you have in your account \nPlease retry");
                    } else if (atm.withdraw < 0) {
                        System.out.println("You cannot withdraw a negative amount of money!");
                    } else {
                        ok = true;
                    }
                }
                atm.balance -= atm.withdraw;
                System.out.println("You took out $" + atm.withdraw + ".00");
                System.out.println("Your new balance is $" + atm.balance + "0");
            }
            
            else if(options==2)
            {
                System.out.println("How much money would you like to deposit?");
                System.out.print("$");
                atm.deposit = input.nextInt();
                atm.balance+=atm.deposit;
                System.out.println("Your new balance is $" + atm.balance +"0");
            }

            else if(options==1)
            {
                System.out.println("Your account balance is $"+atm.balance+"0");
            }
            
            else if(!(options==4))
            {
                System.out.println("That is not an available option, please try again. Select digits of 1-4.");
            }
        }
    }
}

public class aTMTester
{
    private static final String username = "David";
    private static final int PIN = 2468;
    public static int deposit, withdraw;
    public static double balance=0.00;
    private String menu;
     
    /*
     * Default constructor
     */
    
    public aTMTester()
    {
    }
     
    public static String getUsername()
    {
        return username;
    }
 
    public static int getPIN()
    {
        return PIN;
    }
     
    public static void main()
    {
        System.out.println("\n+++++++++++Account: "+ username +"+++++++++++");
        System.out.println("1. Check Account Balance");
        System.out.println("2. Deposit Checks");
        System.out.println("3. Withdraw Money");
        System.out.println("4. Logout");
         
        System.out.print("\nWhat would you like to do next?\n");
    }
     
    public static void logout()
    {
        System.out.println("Thanks for usinging the David Inc. ATM. We hope you expierence was fast and simple! Have a great day!");
        System.exit(0);
    }  
}

Was This Post Helpful? 0
  • +
  • -

#4 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Getting many cannot find variable errors in ATM Class

Posted 06 November 2016 - 03:24 AM

you can test the input using
https://docs.oracle....ml#hasNextInt()
https://www.tutorial..._hasnextint.htm

if it is not an int read the line, throw it away and try again
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1