Results 1 to 7 of 7

Thread: Java ATM help

  1. #1
    uzmen is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Java ATM help

    Hi, I have made a JAVA ATM, now the only problem is, accessing methods from different classes. I can;t figure that out. Basically when i run the main class i want to be able to check if a certain username exists in the file. If it does, retireve that users information. If that user does exist, then i have to check and validate the users pin and then display the menu. I can;t figure out how to do that. My code is as follows.
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaatm;
    import java.io.*;
    import java.util.*;
    /**
     *
     * @author Owner
     */
    //instantiates an account based on data in file. If user exists then return the user account else create
    //a new account and set balance to zero. Class account has the ability to get information and also
    //set information.
    class account
    {
        private String name;
        private int pin;
        private double balance;
        public account (String name, int pin)
        {
            this.name = name;
            this.pin = pin;
            this.balance = 0.0;
        }    
        public account (String name, int pin, double balance)
        {
            this.name = name;
            this.pin = pin;
            this.balance = balance;
        }
        public String getName()
        {
            return name;
        }
        public int getPin()
        {
            return pin;
        }
        public double getBalance()
        {
            return balance;
        }
        public void setName(String name)
        {
            this.name = name;
        }
        public void setPin (int pin)
        {
            this.pin = pin;
        }
        public void setBalance (double balance)
        {
            this.balance = balance;
        }
    }
    
    
    //modelling the bank, it opens up the information stored in the text file and reads it in. 
    //if the file does not exist, it creates it. The bank also has the ability to create a 
    //new account, print the accounts and get account information according to the variables 
    //passed through the methods.
    class bank
    {
        private account[] accounts = new account[100];
        private int i;
        public bank ()
        {
            try
            {
                i = 0;
                String name;
                int pin;
                double balance;
                String temp;
                File accountsFile = new File ("filefullofmoney.txt");
                FileReader in = new FileReader(accountsFile);
                BufferedReader reader = new BufferedReader (in);
                try {
                    while ((temp = reader.readLine()) != null) 
                    {
                        name = temp;
                        temp = reader.readLine();
                        pin = Integer.parseInt(temp);
                        temp = reader.readLine();
                        balance = Double.parseDouble(temp); 
                        accounts[i] = new account(name, pin, balance);
                        i++;
                    }
                    reader.close();
                } catch (IOException ex) 
                {
                }
            }
            catch (FileNotFoundException notFound)
            {
                try
                {
                    File accountsFile = new File ("filefullofmoney.txt");
                    FileWriter out = new FileWriter(accountsFile);
                }
                catch(IOException notFound2)
                {                
                }
            }
        }
        public void printAccounts()
        {
            int j;
            for (j = 0; j < i; j++)
            {
                System.out.println(accounts[j].getName() + '\t' + accounts[j].getPin() + '\t' + accounts[j].getBalance());
            }
        }
        //return = 1 for unique account, return = 0 for duplicate account
        public int createAccount(String name, int pin)
        {        
            int j;
            for (j = 0; j < i; j++)
            {
                if (accounts[j].getName().compareTo(name) == 0)
                {
                    return 0;
                }
            }
            accounts[i] = new account(name, pin);
            i++;
            try
            {
                FileWriter out = new FileWriter("filefullofmoney.txt", true);
                BufferedWriter writer = new BufferedWriter(out);
                writer.newLine();
                writer.write(name);
                writer.newLine();
                writer.write("" + pin);
                writer.newLine();
                writer.write("0.0");
                writer.close();
            }
            catch (IOException error)
            {
            }
            return 1;
        }
        
        public account getAccount(String name)
        {
            int j;
            for (j = 0; j < i; j++)
            {
                if (accounts[j].getName().compareTo(name) == 0)
                {
                    return accounts[j];
                }
            }
            return null;
        }
    }
    
    class ATM {
        public int PIN;
        //public double starting_bal;
        public double current_bal;
        private double trans_amount;
        private bank atm = new bank();
        
        public ATM(int user_pin, double user_start_bal) {
            PIN = user_pin;
            current_bal = user_start_bal;        
        }
    
        public ATM(int user_pin) {
            PIN = user_pin;
            current_bal = 42.00;      
        }
            
        public ATM() {
            PIN = 4242;
            current_bal = 0.0;        
        }        
        
        public int get_PIN(){
            return(PIN);
        }
        
        public void set_amount(double amount) {
            trans_amount = amount;
        }
        
        public void deposit(){
            current_bal += trans_amount;
        }
        
        public void withdraw() {
            current_bal -= trans_amount;
        }
        
        public double inquiry(){
            return (current_bal);
        }
        
        public void inquiry(String str){
            System.out.println(str + current_bal);
        }
        
        
    }
    
    
    public class Main {
    
    
        public static void main(String[] args) {
            //Overloaded constructors for initialization of object
            
            ATM account1 = new ATM();
            
            //ATM account1 = new ATM(1234);
            //ATM account1 = new ATM();
            int PIN_val = 0, menu_val = 0, pin_try = 0, menu_select = 0;
            double amnt= 0 ;
            String username;
            
            
            System.out.println("Welcome to UZI's Bank");
            
            // Create a scanner to read from keyboard
            // Scanner overcomes problems with System.in.read()        
            Scanner scanner = new Scanner (System.in);
            System.out.printf("\n1.Teller\n2.ATM\nPlease select an option:");
            menu_select = scanner.nextInt();
            
            if(menu_select == 1)
            {
    
            while(pin_try != 3)
            {
               
            try {         //Input validation 
                System.out.printf("Please enter user name");
                username = scanner.nextLine();
                System.out.printf("\nEnter PIN number: ");
                PIN_val = scanner.nextInt ();
                //System.out.println (" You entered " + int_val +"\n");
                account account2 = new account(username, PIN_val);
                bank account = new bank();
            } catch  (InputMismatchException e) {
                System.out.println ("Mismatch exception:" + e );
            }
            
            if (PIN_val == account1.get_PIN()){
                System.out.printf("PIN Matched.  ");
                //System.out.printf("Current balance " + account1.current_bal);
                account1.inquiry("Present Balance: ");
                try {
                    
                    System.out.printf ("\n\nATM Menu\n1. Deposit\n2. Withdraw\n3. Balance\n4. Exit\n\nEnter choice: ");
                    menu_val = scanner.nextInt ();
                    while(menu_val != 4)
                    {
                        switch(menu_val) {
                            case 1: 
                                try {
                                    System.out.printf ("Transaction amount: ");
                                    amnt = scanner.nextDouble();
                                } catch  (InputMismatchException e) {
                                    System.out.println ("Mismatch exception:" + e );
                                }
                                //Set transaction amount & perform transaction
                                account1.set_amount(amnt);
                     
                                account1.deposit();
                                //Try to get account balance from public attribute
                                //System.out.println("Current balance " + account1.current_bal);
                                //Try to get account balanace from public method
                                System.out.println("Current balance " + account1.inquiry());
                                
                                break;
                           case 2:
                                System.out.println("Transaction amount: ");
                                amnt = scanner.nextDouble();
                                account1.set_amount(amnt);
                                account1.withdraw();
                                System.out.println("Current balance " + account1.inquiry());
                            
                                break;
                           case 3: 
                                System.out.println("Current balance " + account1.inquiry());
                                break;
                           default:
                                System.out.println("Invalid menu entry: " + menu_val);
                                break;
                           }
                        System.out.printf ("\n\nWhat next?\n1. Deposit\n2. Withdraw\n3. Balance\n4. Exit\n\nEnter choice: ");
                        menu_val = scanner.nextInt ();
                        amnt = 0;
                   
                    }
                    if(menu_val == 4)
                        System.out.println("Exiting Program.\nGoodBye!\n");
                        
                    
                } catch  (InputMismatchException e) {
                    System.out.println ("Mismatch exception:" + e );
                }
            } else {
                System.out.print("Incorrect PIN.  Please re-enter pin.");
                
            }
            pin_try++;
            }
            System.out.print("Incorrect PIN.  Exitting.");
            
        }
        }
    
    }
    Last edited by uzmen; 11-06-2008 at 06:38 PM. Reason: code brackets..

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Can you please put some code brackets around your classes and indent it so its easier for everyone to read.

  3. #3
    uzmen is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Sorry, I didn;t know how to, but I have now. I am basically having a problem with making the Main Class.
    Last edited by uzmen; 11-06-2008 at 06:40 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Really too much code to scan. Can you localize your problem some.
    problem with making the Main Class
    Can you explain the problem? Perhaps with a small program.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You saying that don't know how to main method? Or you are confusing where you should do it in your application? So, from where you are trying to look at the output when you developing it?

  6. #6
    uzmen is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Hey guys, thanx for the replies.
    I was able to make the necessary modifications and write the main class, i initially had issues accessing methods from my bank and account class. I figured it out after some researching...now the code is huge...but thanx for the intent of helping.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I think if you learn more about OOP concepts, you can write this code more effectively, more clearly. Use separate classes for quite same process. You can use many number of classes and methods, there is no any limitation. So use them lol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •