Results 1 to 18 of 18
  1. #1
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Exclamation Help needed in Java Project

    HELP NEEDED in JAVA




    Hello. I am studying JAVA and need a help in Java.

    I was creating a GUI for my Java project named "THE ATM". everything was OK, till the part came where I had put the logic in the code.

    THE PROBLEM :-
    I wanted to implement a functionality in which I wanted the user to create a new account using the terminal window , when the user presses the "new account" button .
    But instead, when the user presses the "new user" button, the terminal window opens, and I can't input anything in it. When I come back to the GUI, it gets all forzen up and my program crashes.

    Please help by refining my code.

    THE Code :-
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * Atm.java
     *
     * Created on Oct 30, 2010, 1:45:17 PM
     */
    
    package atm;
    
    /**
     *
     * @author Suresh
     */
     import java.io.*;
    public class Atm extends javax.swing.JFrame {
        String username[] = new String[1000];
        int password[] = new int[1000];
        double balance[]= new double[1000];
        int a1;
        int posi;
    boolean flag;
        /** Creates new form Atm */
        public Atm() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jPasswordField1 = new javax.swing.JPasswordField();
            jButton1 = new javax.swing.JButton();
            jLabel4 = new javax.swing.JLabel();
            jTextField2 = new javax.swing.JTextField();
            jLabel5 = new javax.swing.JLabel();
            jTextField3 = new javax.swing.JTextField();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            status = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
            jLabel1.setText("    ATM by ABHILASH");
    
            jLabel2.setText("Username :");
    
            jLabel3.setText("Password :");
    
            jButton1.setText("Log In");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel4.setText("Balance :");
    
            jTextField2.setText("0");
    
            jLabel5.setText("Enter Amount :");
    
            jTextField3.setText("0");
    
            jButton2.setText("Deposit");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("Withdraw");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            jButton4.setText("New Account");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
    
            status.setText("Not Logged in");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jPasswordField1)
                                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(59, 59, 59)
                            .addComponent(jButton1)))
                    .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addGap(13, 13, 13)
                    .addComponent(jButton2)
                    .addGap(48, 48, 48)
                    .addComponent(jButton3)
                    .addGap(85, 85, 85))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(154, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(116, 116, 116))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(150, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(74, 74, 74))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(350, Short.MAX_VALUE)
                    .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(22, 22, 22)
                            .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(14, 14, 14)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton3)
                        .addComponent(jButton2)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(40, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try{
           BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
           System.out.println("**************** CREATE A NEW ACCOUNT ****************");
           System.out.println("\n\n\n\n");
           System.out.println("Enter new username :");
           username[a1] = in.readLine();
           System.out.println("Enter new password :");
           password[a1] = Integer.parseInt(in.readLine());
           System.out.println("Enter the amount to start with : ");
           balance[a1] = Double.parseDouble(in.readLine());
           a1++;
            }
        catch(Exception e) {
            System.out.println("Error Occured!("+e+" : Restart the program :");
            String a[]={"","","",""};
            main(a);
        }
        }                                        
    public void new_user() {
        String abutton = jTextField1.getText();
            int bbutton = Integer.parseInt(jPasswordField1.getText());
            for(int i=0;i<1000;i++) {
            if(abutton.equalsIgnoreCase(username[i])) {
                if(bbutton==password[i]) {
                    status.setText("Logged in!");
                    posi=i;
                    flag = true;
                    break;
                }
            }
            }
            if(flag!=true) {
                status.setText("User not valid");
                System.exit(0);
            }
    
    }
        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            new_user ();
        }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            double input = Double.parseDouble(jTextField3.getText());
            balance[a1]+=input;
            String ab = ""+balance[a1];
            jTextField2.setText(ab);
        }                                        
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           double input = Double.parseDouble(jTextField3.getText());
            balance[a1]-=input;
            String ab = ""+balance[a1];
            jTextField2.setText(ab);
        }                                        
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Atm().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JPasswordField jPasswordField1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField status;
        // End of variables declaration                   
    
    }

    Thank you... Looking forward for your help!

  2. #2
    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

    Seems you are working on NetBeans. If so why don't you try to debug the code, it's easy with NetBeans. Did you try that?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't mix console and Swing coding, but rather do it all in the GUI. You'll not regret this. As for your specific error, it will be hard to tell without an SSCCE. Check out the link for details.

    Good luck!

  4. #4
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Post

    Quote Originally Posted by Eranga View Post
    Seems you are working on NetBeans. If so why don't you try to debug the code, it's easy with NetBeans. Did you try that?
    It wasn't of much help. Thats the reason why I turned to the forum

  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

    Then lol, tell us what happen when you do that. Reason to say that is, most of us are lazy to run your code and find the errors for you. If you could provide more you've then yes, you can find lots of comments on your question.

  6. #6
    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

    Few things to point on you,

    1. As Fubarable says, don't messup the console and GUI. Your new account works on the console partially and then move to the GUI. It's not good, really dangerous when cross threading in UI.

    2. Don't use arrays with large number of elements. I can see that you've initialize some arrays with 1000 elements. Think about what happen you've only 1 element, or may be you've 5000 elements. Collections class in Java has a nice interface to handle those scenarios, dynamic growth of the size.

  7. #7
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    ok.
    I appreciate your "laziness"
    here's my problem:
    1) as soon as I execute the program the GUI comes up. ( no problem with that)


    2)As soon as I press the "New Account" button, the console opens up.

    3) as you can see, the console doesn't take any input.


    4) The program now freezes and I have to go to the Task manager to end the process

  8. #8
    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

    Actually I can do that. Seems something wrong with your PC, or may be with the BlueJ.

    As I said this cross threading from GUI to console is not good. That may cause lots of issues on BlueJ (IDE) as well.

    I can suggest you one thing. Run the project in console not from the BlueJ. Do you know how to do that?

  9. #9
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    --------------------------------------------------
    So now, if I want to migrate completely to the GUI form, then how can I create a new JFrame without creating a new class.
    If that is not possible, then how do I take global elements from the ATM class and use it for the newly created class?

  10. #10
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Actually I can do that. Seems something wrong with your PC, or may be with the BlueJ.

    As I said this cross threading from GUI to console is not good. That may cause lots of issues on BlueJ (IDE) as well.

    I can suggest you one thing. Run the project in console not from the BlueJ. Do you know how to do that?
    You mean like running it in blue J? How do you do that?
    (Please excuse my amature-ish-ness)

  11. #11
    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

    Quote Originally Posted by 1995ad View Post
    --------------------------------------------------
    So now, if I want to migrate completely to the GUI form, then how can I create a new JFrame without creating a new class.
    If that is not possible, then how do I take global elements from the ATM class and use it for the newly created class?
    Yes you've to write a new class for that. Initiate the required UI, actually you've to decide that according to your requirement. That's the main concept of Object Oriented programming.

  12. #12
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Question

    Quote Originally Posted by Eranga View Post
    Yes you've to write a new class for that. Initiate the required UI, actually you've to decide that according to your requirement. That's the main concept of Object Oriented programming.
    Ok. I have decided to do that. So now, how can I access another classes variables in another class, so that the change is retained even after the latter class is closed and the former class is still running?

    IS IT POSSIBLE TO BE DONE USING OBJECTS?

  13. #13
    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

    Quote Originally Posted by 1995ad View Post
    You mean like running it in blue J? How do you do that?
    (Please excuse my amature-ish-ness)
    Run in using the command prompt.

    Open the command prompt and change the working folder to location where the Java class "Atm.java" file is located.

    Compile the Atm class using javac command and then run it using java command.

    Check still your application freeze. I don't think it'll.

  14. #14
    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

    Quote Originally Posted by 1995ad View Post
    Ok. I have decided to do that. So now, how can I access another classes variables in another class, so that the change is retained even after the latter class is closed and the former class is still running?

    IS IT POSSIBLE TO BE DONE USING OBJECTS?
    There are many ways to do that. As I said it depends on your design. Why you really need data from one class to another? I don't know you've notice that, but you stuck with that because there is no central location to get data in your application. From the main class you've initialize few arrays and try to store data there.

    Rather doing that try to preserve data in a permanent location, in a text file or in a database and so on. Think about that once your application is close all data is loss.

  15. #15
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    I agree with your point. i will implement in in my project in the future. Can you now please enlighten the path in which you can teach me how to bring and change global variables in another class?

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

  17. #17
    1995ad is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Wink forget it

    Its Ok. I know that. Forget it. I just finished my program. Here it is, if you want it. I has got no problem. Hurrah!
    Java Code:
    /**
     *
     * @author Abhilash
     * Copyrighted 
     * DO NOT COPY
     * PROPERTY OF ABHILASH
     */
    
    import java.io.*;
    import javax.swing.*;
    public class Atm extends javax.swing.JFrame {
        String username[] = new String[100];
        int password[] = new int[100];
        double balance[]= new double[100];
        int a1;
        int posi;
    boolean flag;
        /** Creates new form Atm */
        public Atm() {
            initComponents();
        }
        
        @SuppressWarnings("unchecked")
        
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            usernameField = new javax.swing.JTextField();
            passwordField = new javax.swing.JPasswordField();
            loginButton = new javax.swing.JButton();
            jLabel4 = new javax.swing.JLabel();
            balanceField = new javax.swing.JTextField();
            jLabel5 = new javax.swing.JLabel();
            amountField = new javax.swing.JTextField();
            depositButton = new javax.swing.JButton();
            withdrawButton = new javax.swing.JButton();
            newaccountButton = new javax.swing.JButton();
            status = new javax.swing.JTextField();
            logoutButton = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("The ATM by Abhilash");
    
            jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
            jLabel1.setText("    ATM by ABHILASH");
    
            jLabel2.setText("Username :");
    
            jLabel3.setText("Password :");
    
            loginButton.setText("Log In");
            loginButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    loginButtonActionPerformed(evt);
                }
            });
    
            jLabel4.setText("Balance :");
    
            balanceField.setEditable(false);
            balanceField.setText("0.0");
    
            jLabel5.setText("Enter Amount :");
    
            amountField.setText("0.0");
    
            depositButton.setText("Deposit");
            depositButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    depositButtonActionPerformed(evt);
                }
            });
    
            withdrawButton.setText("Withdraw");
            withdrawButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    withdrawButtonActionPerformed(evt);
                }
            });
    
            newaccountButton.setText("New Account");
            newaccountButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    newaccountButtonActionPerformed(evt);
                }
            });
    
            status.setEditable(false);
            status.setText("Not Logged in");
    
            logoutButton.setText("Log Out");
            logoutButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    logoutButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(newaccountButton, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addGap(13, 13, 13)
                    .addComponent(depositButton)
                    .addGap(48, 48, 48)
                    .addComponent(withdrawButton)
                    .addGap(85, 85, 85))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(154, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(116, 116, 116))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(150, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(74, 74, 74))
                .addGroup(layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(passwordField)
                                .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(59, 59, 59)
                            .addComponent(loginButton)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(balanceField, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(logoutButton)
                            .addGap(18, 18, 18)
                            .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(balanceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(loginButton)
                                .addComponent(logoutButton)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(22, 22, 22)
                            .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(14, 14, 14)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(amountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(withdrawButton)
                        .addComponent(depositButton)
                        .addComponent(newaccountButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(40, Short.MAX_VALUE))
            );
    
            pack();
        }
    
        private void newaccountButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        try{
            new_user();
    
        status.setText("New account created");
            }
         catch(Exception e) {
         JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE);
         dispose();
         String a[] = {"","","",""};
         main(a);
         }
        }                                                
    
        public void new_user() {
        try{
           BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
           System.out.println("**************** CREATE A NEW ACCOUNT ****************");
           System.out.println("\n\n\n");
           System.out.println("Enter new username :");
           username[a1] = in.readLine();
           System.out.println("Enter new password :");
           password[a1] = Integer.parseInt(in.readLine());
           System.out.println("Enter the amount to start with : ");
           balance[a1] = Double.parseDouble(in.readLine());
           System.out.println("Account successfully created !");
    
           a1++;
            }
        catch(Exception e) {
            System.out.println("Error Occured!("+e+" : Restart the program :");
            dispose();
            String a[]={"","","",""};
            main(a);
        }
        
    
    }
    
        private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
           try{
               String abutton = usernameField.getText();
    
            int bbutton = Integer.parseInt(passwordField.getText());
            for(int i=0;i<1000;i++) {
            if(abutton.equalsIgnoreCase(username[i])) {
                if(bbutton==password[i]) {
                    status.setText("Logged in!");
                    posi=i;
                    flag = true;
                    break;
                }
            }
            }
            if(flag!=true) {
                status.setText("User not valid");
                System.exit(0);
            }
    
            if(flag){
                String a=""+balance[posi];
                balanceField.setText(a);
                usernameField.setEditable(false);
                passwordField.setEditable(false);
                amountField.setEditable(true);
            }
            }
            catch(Exception e) {
                JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE);
                dispose();
                String a[] = {"","","",""};
                main(a);
         }
    
        }                                           
    
        private void depositButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
         try{
             if(flag) {
            double input = Double.parseDouble(amountField.getText());
            balance[posi]=balance[posi]+input;
            String ab = ""+balance[posi];
            balanceField.setText(ab);
            }
     else{
             status.setText("User not logged in!");
     }
            }
         catch(Exception e) {
             JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE);
             dispose();
             String a[] = {"","","",""};
             main(a);
         }
        }                                             
    
        private void withdrawButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
          try {
              if(flag) {
            double input = Double.parseDouble(amountField.getText());
            balance[posi]=balance[posi]-input;
            String ab = ""+balance[posi];
            balanceField.setText(ab);}
     else{
          status.setText("User not logged in!");
     }
            }
           catch(Exception e) {
               JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE);
               dispose();
               String a[] = {"","","",""};
               main(a);
         }
        }                                              
    
        private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
            try{
                posi = 0;
    
            flag = false;
            status.setText("Logged out");
            balanceField.setText("0.0");
            amountField.setText("0.0");
            amountField.setEditable(false);
            usernameField.setEditable(true);
            passwordField.setEditable(true);
            }
             catch(Exception e) {
                 JOptionPane.showMessageDialog(this, "An error occured with the program. Restarting...", "Error Occured", JOptionPane.ERROR_MESSAGE);
                 dispose();
                 String a[] = {"","","",""};
                 main(a);
         }
        }                                            
    
       
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Atm().setVisible(true);
                }
            });
        }
    
        // Variables declaration 
        private javax.swing.JTextField amountField;
        private javax.swing.JTextField balanceField;
        private javax.swing.JButton depositButton;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JButton loginButton;
        private javax.swing.JButton logoutButton;
        private javax.swing.JButton newaccountButton;
        private javax.swing.JPasswordField passwordField;
        private javax.swing.JTextField status;
        private javax.swing.JTextField usernameField;
        private javax.swing.JButton withdrawButton;
        // End of variables declaration
    
    }

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

Similar Threads

  1. Programmer needed for J2ME project
    By Sber in forum Reviews / Advertising
    Replies: 0
    Last Post: 11-04-2009, 02:51 PM
  2. help needed in runnin java project
    By anurag.25 in forum Advanced Java
    Replies: 14
    Last Post: 02-15-2009, 07:00 PM
  3. help needed with project
    By Ice_Angel in forum JDBC
    Replies: 1
    Last Post: 06-22-2008, 08:00 PM
  4. Java experts needed- 30 minute online Java projects
    By michelle in forum Jobs Offered
    Replies: 0
    Last Post: 03-05-2008, 11:47 PM
  5. Uni Project - GUI - Help Needed
    By deeadeed in forum New To Java
    Replies: 0
    Last Post: 12-03-2007, 11:10 PM

Posting Permissions

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