首页 > 解决方案 > 我如何在存款或取款时更新我的​​余额?

问题描述

我希望在我存款或取款时更新我的​​余额。我还是 Java、GUI、ArrayList 的新手。我想创建一个简单的银行程序,只需简单的登录显示选项(取款、存款、余额)。我是否在存款或取款时使用了错误的代码来更新余额?我想得到你的帮助或提示。我很抱歉,如果它这么久。// 账户类

                            public class Account {
                            
                                private double Withdraw;
                                private double Deposit;
                                private double Balance;
                                
                                public Account (double Withdraw, double Deposit, double Balance) {
                                    
                                    this.Withdraw = Withdraw;
                                    this.Deposit = Deposit;
                                    this.Balance = Balance;
                                }
                                
                                public Account () {
                                    
                                }
                                
                                public void Withdraw (double Sum) {
                                    this.Balance = this.Balance - Sum;
                                }
                                
                                public void Deposit (double Sum) {
                                    this.Balance = this.Balance - Sum;
                                }
                                
                                public void setBalance (double Blnc) {
                                    this.Balance = Blnc;
                                }
                                
                                public double getBalance () {
                                    return this.Balance;
                                }
                                
                                public double getWithdraw()
                                {
                                    return this.Withdraw;
                                }
                            
                                public double getDeposit()
                                {
                                    return this.Deposit;
                                }
                            }
                    
                    // Balance  class

                        import javax.swing.JFrame;
                        import javax.swing.JLabel;
                        import javax.swing.JTextArea;
                        import javax.swing.JTextField;
                        
                        import java.awt.event.ActionEvent;
                        import java.awt.event.ActionListener;
                        import java.util.ArrayList;
                        import java.util.List;
                        
                        import javax.swing.JButton;
                        
                        public class Balance implements ActionListener {
                            
                            private List<Account> store;
                            
                            private static JLabel balance;
                            private static JTextField Balance;
                            
                            JFrame frame = new JFrame();
                            JButton OK = new JButton("OK");
                            // JButton Show = new JButton ("SHOW");
                            // JTextArea text = new JTextArea();
                            
                            Balance() {
                                
                                store = new ArrayList<Account>();  
                                    
                                    frame.setSize(500, 300);
                                    frame.setTitle("BALANCE ACCOUNT"); 
                                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                    frame.setLayout(null);
                                    
                                    balance = new JLabel("YOUR BALANCE IS: " + acnt.getBalance());
                                    balance.setBounds(200, 50, 200, 50);
                                    frame.add(balance);
                                    
                                //  text = new JTextArea();
                                //  text.setBounds(200,  80, 100, 50);
                                //  frame.add(text);
                                    
                                //  Show.addActionListener(this);
                                //  Show.setBounds(250, 200, 100, 20);
                                //  frame.add(Show);
                                    
                                    OK.addActionListener(this);
                                    OK.setBounds(380, 200, 100, 20);
                                    frame.add(OK);
                                                
                                    frame.setVisible(true);             
                            }
                            
                            Account acnt = new Account();
                        
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                if (e.getSource()== OK) {
                                    acnt.setBalance(acnt.getBalance()+Integer.parseInt(balance.getText()));
                                    frame.setVisible(false);
                                     ATMOptionPanel atmoptionpanel = new ATMOptionPanel();
                                     // text.append("\nBalance="+acnt.getBalance());
                                }
                                
                                }
                                    
                        }
            // Deposit Class
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import java.util.ArrayList;
                import java.util.List;
                
                import javax.swing.JButton;
                import javax.swing.JFrame;
                import javax.swing.JLabel;
                import javax.swing.JOptionPane;
                import javax.swing.JTextField;
                // import java.util.Scanner;
                
                public class Deposit implements ActionListener{
                    
                    private List<Account> store;
                    
                    //Scanner scanner = new Scanner(System.in);
                    int Balance = 0, Deposit, Withdraw; 
                    
                    private static JLabel deposit;
                    private static JTextField depositText;
                    
                    JButton submit = new JButton("SUBMIT");
                    JButton back = new JButton("BACK");
                    
                    JFrame frame = new JFrame();
                    
                    Deposit () {
                            
                        store = new ArrayList<Account>();  
                        
                            frame.setSize(500, 300);
                            frame.setTitle("DEPOSIT MENU"); 
                            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            frame.setLayout(null);
                            
                            deposit = new JLabel("DEPOSIT: ");
                            deposit.setBounds(150, 50, 100, 50);
                            frame.add(deposit);
                            
                            depositText = new JTextField(20);
                            depositText.setBounds(250, 65, 100, 20);
                            frame.add(depositText);
                            
                            submit.addActionListener(this);
                            submit.setBounds(230, 100, 100, 20);
                            frame.add(submit);
                            
                            back.addActionListener(this);
                            back.setBounds(340, 100, 100, 20);
                            frame.add(back);
                            
                            frame.setVisible(true);     
                    }
                
                    Account acnt = new Account();
                
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            
                            // int Deposit = Integer.parseInt(depositText.getText());
                            
                            
                            if (e.getSource()== submit) {
                                store.add(acnt);
                                frame.setVisible(false);
                
                                Balance balance = new Balance();
                                acnt.setBalance(acnt.getBalance());
                            }
                            
                            else if (e.getSource()== back) {
                                frame.setVisible(false);
                                store.add(acnt);
                                ATMOptionPanel atmoptionpanel = new ATMOptionPanel(); 
                            }
                        }
                }

// 退出类

            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.util.ArrayList;
            import java.util.List;
            
            import javax.swing.JButton;
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.JOptionPane;
            import javax.swing.JTextField;
            
            public class Withdraw implements ActionListener{
                
                private List<Account> store;
                
                int Balance = 0, Withdraw, Deposit;
                
                private static JLabel withdraw;
                private static JTextField withdrawText;
                
                JButton back = new JButton("BACK");
                JButton submit = new JButton("SUBMIT");
                
                JFrame frame = new JFrame();
                
                    Withdraw() {
                        
                        store = new ArrayList<Account>();  
                        
                        frame.setSize(500, 300);
                        frame.setTitle("WITHDRAW MENU"); 
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setLayout(null);
                        
                        withdraw = new JLabel("WITHDRAW: ");
                        withdraw.setBounds(150, 50, 100, 50);
                        frame.add(withdraw);
                        
                        withdrawText = new JTextField(20);
                        withdrawText.setBounds(250, 65, 100, 20);
                        frame.add(withdrawText);
                        
                        back.setBounds(320, 150, 100, 20);
                        frame.add(back);
                        
                        submit.addActionListener(this);
                        submit.setBounds(200, 150, 100, 20);
                        frame.add(submit);
                        
                        frame.setVisible(true);     
                }
                    
                    Account acnt = new Account();
            
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        if (e.getSource()== submit) {
                            if (Balance <= Withdraw) {
                                acnt.setBalance(acnt.getBalance()-Integer.parseInt(withdrawText.getText()));
                                store.add(acnt);
                                JOptionPane.showMessageDialog(null, "Withdraw success!.", "SUCCESS!", JOptionPane.INFORMATION_MESSAGE);
                            } else if (Balance >= Withdraw)
                                {
                                JOptionPane.showMessageDialog(null, "You don't have enough balance\nPlease deposit an amount first.", "INSUFFICIENT BALANCE!", JOptionPane.ERROR_MESSAGE);
                                }
                            
                            if (e.getSource()== back) {
                                frame.setVisible(false);
                                
                            }
                        }
                    }
            }
    // OptionPanel
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JOptionPane;
        import javax.swing.JPanel;
        
        public class ATMOptionPanel implements ActionListener {
            
            private static JLabel BankOptions;
            
            JFrame frame = new JFrame();
            JButton withdraw = new JButton("WITHDRAW");
            JButton deposit = new JButton("DEPOSIT");
            JButton balance = new JButton("BALANCE");
            
            ATMOptionPanel() {
                
                frame.setSize(500, 300);
                frame.setTitle("BANK ACCOUNT");
                frame.setLayout(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                BankOptions = new JLabel ("MENU");
                BankOptions.setBounds(230, 10, 200, 20);
                frame.add(BankOptions);
                
                withdraw.addActionListener(this);
                withdraw.setBounds(100, 80, 140, 25);
                withdraw.setFocusable(false);
                frame.add(withdraw);
                
                deposit.addActionListener(this);
                deposit.setBounds(250, 80, 140, 25);
                deposit.setFocusable(false);
                frame.add(deposit);
                
                balance.addActionListener(this);
                balance.setBounds(180, 120, 140, 25);
                balance.setFocusable(false);
                frame.add(balance);
                
                frame.setVisible(true);
            }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource()==withdraw) {
                frame.setVisible(false);
                Withdraw withdraw = new Withdraw();
            } 
            
            else if (e.getSource()==deposit) {
                frame.setVisible(false);
                Deposit deposit = new Deposit();
            }
            
            else if (e.getSource()==balance) {
                frame.setVisible(false);
                Balance balance = new Balance();
            }
        }
        
        }
// Login
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Login  implements ActionListener {
    
        private static JLabel welcome;
        private static JLabel userLabel;
        private static JTextField userText;
        private static JLabel passwordLabel;
        private static JTextField passwordText; 
        
        JFrame frame = new JFrame();
        JButton login = new JButton("Login"); // create a Button with a text of "Login"
        
        Login() {
            frame.setSize(500, 300);
            frame.setTitle("BANK ACCOUNT"); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setBackground(Color.BLACK);
            frame.setLayout(null);
            
            welcome = new JLabel ("WELCOME TO THE LOGIN PANEL",JLabel.CENTER);
            welcome.setBounds(150, 10, 220, 20);
            frame.add(welcome);
            
            userLabel =  new JLabel ("USERNAME:\t"); // Word "User" will appear.
            userLabel.setBounds(100, 50, 100, 50); // (x, y, width, height)
            frame.add(userLabel);
            
            userText = new JTextField(20); // rectangle box will appear at the left side of the word "User".
            userText.setBounds(250, 65, 80, 20);
            frame.add(userText);
            
            passwordLabel = new JLabel ("PASSWORD:\t"); // Word "Password" will appear. 
            passwordLabel.setBounds(100, 90, 100, 50);
            frame.add(passwordLabel);
            
            passwordText = new JTextField(20); // rectangle box will appear at the left side of the word "Password".
            passwordText.setBounds(250, 105, 80, 20);
            frame.add(passwordText);
    
            login.addActionListener(this);
            login.setBounds(200, 150, 100, 20);
            frame.add(login);
            
            frame.setVisible(true); 
            
            System.out.println("USERNAME:\tBank");
            System.out.println("PASSWORD:\tabcd123");
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            
            String user = userText.getText();
            String password = passwordText.getText();
            
            if (user.equals("Bank") && (password.equals("abcd123"))) {
                System.out.println("\nLogin Successfully!");
                
                frame.setVisible(false);
                
                ATMOptionPanel atmoptionpanel = new ATMOptionPanel();
            } 
            
            else    
            {
                JOptionPane.showMessageDialog(null, "PLEASE INPUT THE CORRECT USERNAME & PASSWORD! \n Write what is on the console.", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    
    }

// ATM主类

public class ATMMain {

    public static void main(String[] args) {
        
        Login login = new Login();

    }

}

标签: java

解决方案


推荐阅读