Java 银行账户算法

Java 银行账户算法,java,swing,user-interface,Java,Swing,User Interface,我正在尝试设计一个银行账户GUI,它适用于两种类型的账户;活期存款和储蓄。关于这一点,我有几个问题 当我存入500英镑或以上时,余额中会增加10英镑的奖励,但这只有在我提取款项后才能贷记,为什么 我在存款/取款后的第一次余额计算是正确的,但此后就再也不正确了。我的方法有错误吗 在我的代码中,初始余额为0,但如果我希望用户在gui中设置余额,该怎么办 这已经让我发疯好几天了,所以任何建议或帮助都将不胜感激 主菜单 package assignment; import java.awt.event

我正在尝试设计一个银行账户GUI,它适用于两种类型的账户;活期存款和储蓄。关于这一点,我有几个问题

  • 当我存入500英镑或以上时,余额中会增加10英镑的奖励,但这只有在我提取款项后才能贷记,为什么
  • 我在存款/取款后的第一次余额计算是正确的,但此后就再也不正确了。我的方法有错误吗
  • 在我的代码中,初始余额为0,但如果我希望用户在gui中设置余额,该怎么办
  • 这已经让我发疯好几天了,所以任何建议或帮助都将不胜感激

    主菜单

    package assignment;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class mainMenuFrame extends javax.swing.JFrame 
    {
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 250;
        private JPanel infoPanel;
    
        public mainMenuFrame()
        {
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            setLocationRelativeTo(null);
    
            accountFrame frame = new accountFrame();
            savingsAccountFrame sFrame= new savingsAccountFrame();
    
    
    
            /* Create menu bar and add menu items so the user can choose what type of account 
             they would like to set up*/
            JMenuBar theMenuBar = new JMenuBar();        
            JMenu account = new JMenu("Choose type of account");     
            JMenuItem currentAcc = new JMenuItem();
            currentAcc.setText("Current Account");
            JMenuItem savingsAcc = new JMenuItem();
            savingsAcc.setText("Savings Account");
    
            account.add(currentAcc);
            account.add(savingsAcc);        
            theMenuBar.add(account);
            setJMenuBar(theMenuBar);
    
            //Attach an actionListener to the currentAcc menuItem
            currentAcc.addActionListener(new ActionListener()
            {
        @Override
               public void actionPerformed(ActionEvent ev) 
               {
                       frame.setVisible(true);
               }
    
            });
    
           //Attach an actionListener to the savingsAcc menuItem
            savingsAcc.addActionListener(new ActionListener()
                    {
                        @Override
                        public void actionPerformed(ActionEvent ev)
                        {
                            sFrame.setVisible(true);
                        }
                    });
    
            //Display information about the program        
            String content = "Welcome to our online banking program\n" + "To create a new account\n"+ "please use the menu above";
    
            infoPanel = new JPanel();
            infoPanel.add(new JTextArea(content));
            add(infoPanel);
    
    
    }
    }
    
    允许取款和存款的账户框架

    package assignment;
    
    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.JPanel;
    import javax.swing.JTextField;
    
        public class accountFrame extends JFrame 
        {
           private static final int FRAME_WIDTH = 300;
           private static final int FRAME_HEIGHT = 250;
           private static final double INITIAL_BALANCE = 0.00;
           double result;
           private JLabel initialLabel;
           private JLabel depositLabel;
           private JLabel withdrawLabel;
           private JTextField depositField;  
           private JTextField withdrawField;
           private JButton depositButton;
           private JButton withdrawButton;
           private final JLabel resultLabel;
           private JPanel controlPanel;
           private final cAccount account; 
    
    
           public accountFrame()
           { 
    
              account = new cAccount();      
              resultLabel = new JLabel("New Balance: £" + account.getBalance());
    
              createTextField();
              createButton();
              createControlPanel();
              setSize(FRAME_WIDTH, FRAME_HEIGHT);
                  setLocationRelativeTo(null);
    
           }
    
           private void createTextField()
           {
              final int FIELD_WIDTH = 5;
    
              initialLabel = new JLabel("Initial Balance £" + INITIAL_BALANCE);       
              depositLabel = new JLabel("Deposit: ");
              depositField = new JTextField(FIELD_WIDTH);
                  withdrawLabel = new JLabel("Withdraw: ");
                  withdrawField = new JTextField(FIELD_WIDTH);
           }
    
           private void createButton()
           {
    
                   //Create deposit button and assign an action listener
              depositButton = new JButton("Deposit");
    
              class DepositListener implements ActionListener
              {
                     @Override
                 public void actionPerformed(ActionEvent event)
                 {     
                    double depositAmount = Double.parseDouble(depositField.getText());
                        double amount = account.getBalance() + depositAmount;
    
                    account.deposit(amount);
                    result = amount;
                    resultLabel.setText("New Balance: " + result);
                        depositField.setText("0.00");
                 }           
              }
    
              ActionListener d = new DepositListener();
    
              depositButton.addActionListener(d);     
    
                  //Implement action listener for withdraw button
              withdrawButton = new JButton("Withdraw");
    
              class WithdrawListener implements ActionListener
              {
                     @Override
                 public void actionPerformed(ActionEvent event)
                 {    
                    double withdrawl = Double.parseDouble(withdrawField.getText());
                    double amount = account.getBalance() - withdrawl;
    
                    result = amount;
                    resultLabel.setText("New Balance: " + result);
                        withdrawField.setText("0.00");
                 }           
              }
    
              ActionListener w = new WithdrawListener();
              withdrawButton.addActionListener(w);
           }
    
           private void createControlPanel()        
           {
    
              controlPanel = new JPanel();
              controlPanel.add(initialLabel);
              controlPanel.add(depositLabel);
              controlPanel.add(depositField);
              controlPanel.add(depositButton);
              controlPanel.add(withdrawLabel);
              controlPanel.add(withdrawField);
              controlPanel.add(withdrawButton);
              controlPanel.add(resultLabel);
              add(controlPanel);
           }
    
         }
    
    经常账户类别

    package assignment;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class cAccount extends account
    {
        //private double balance;
        private JFrame frame;
        private int numOfDeposits = 0;
        private int numOfWithdrawls = 0;
    
        public cAccount()
        {
            balance=0.00;
           // this.setBalance(initialDeposit);
        }
    
    /* Method to withdraw money from current account. If withdrawl causes the 
        balance to fall below -£200 transaction will not be executed*/  
    
        @Override
        public void withdraw (double amount) 
        {        
            double newBalance = balance -= amount;
    
            if((balance-amount)<-200)
            {
                JOptionPane.showMessageDialog(frame,"Your account balance cannot fall below -£200");
            }   
            balance = newBalance; 
            numOfWithdrawls++;
        }
    
    /* Method to deposit money into the account. If more than £500 is desposited
        in a month, £10 is rewarded to account balance */    
        @Override
        public void deposit (double amount)
        {
            double newBalance = balance += amount;
    
            if (amount<500)
            {
                balance = newBalance;     
            }
            else{
            balance = newBalance += 10; 
        }
        }
    
        public double getBalance()
        {
            return balance;
        }
    }
    

    是的,您的
    取款
    存款
    方法似乎正在进行不必要的更改

    使用
    a+=b
    a-=b
    将更改该值,因为它与
    a=a+b相同
    a=a-b

    试试这个:

    @Override
    public void withdraw (double amount) 
    {        
        double newBalance = balance - amount;
    
        if(newBalance < -200)
        {
            JOptionPane.showMessageDialog(frame,"Your account balance cannot fall below -£200");
            return; // Don't do withdraw
        }
    
        balance = newBalance; 
        numOfWithdrawls++;
    }
    
    /* Method to deposit money into the account. If more than £500 is desposited
    in a month, £10 is rewarded to account balance */    
    @Override
    public void deposit (double amount)
    {
        double newBalance = balance + amount;
    
        if (amount<500)
        {
            balance = newBalance;     
        }
        else{
            balance = newBalance + 10; 
        }
    }
    
    还有,改变这个。否则,当您有100英镑,并存入50英镑时,您可能会得到250英镑:

    class DepositListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {     
            double depositAmount = Double.parseDouble(depositField.getText());
    
            account.deposit(depositAmount);
            result = account.getBalance();
            resultLabel.setText("New Balance: " + result);
            depositField.setText("0.00");
         }           
     }
    
    而且,您可能还希望在
    帐户中使用提款方法:

    class WithdrawListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {    
            double withdrawl = Double.parseDouble(withdrawField.getText());
    
            account.withdraw(withdrawl);
    
            result = account.getBalance();
            resultLabel.setText("New Balance: " + result);
            withdrawField.setText("0.00");
         }           
     }
    
    要设置金额,请执行以下操作:

    // Add this method in class cAccount:
    public void setToAmount(double amount) {
        balance = amount;
    }
    
    // Add a text field named "setAmountField" to enter the amount, and a button (choose a name) to apply it, and add this listener to the button:
    class SetAmountListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {     
            double setToAmount = Double.parseDouble(setAmountField.getText());
    
            account.setToAmount(setToAmount);
    
            result = account.getBalance();
            resultLabel.setText("New Balance: " + result);
            setAmountField.setText("0.00");
         }           
    }
    

    没有测试这个,所以里面可能有打字错误。如果它不起作用,请告诉我。

    你能发布
    帐户吗?
    ?希望你不介意,但我编辑了标题以反映算术问题。如果它被同行评议会接受,它就会改变,就像现在是学习使用调试器的时候了一样。newBalance变量实际上不是必需的。余额+=金额就可以了。算术似乎有点混乱,不必那么复杂。同意!我也会那样做。但我决定保留原始代码,让fitzy知道他做错了什么。但我已经把它添加到我的答案中;-)我已经照你说的做了,但由于某种原因,余额在我提款之前仍然不计入奖励。这可能是actionlistener中的错误吗?存款时余额是否增加了正确的金额(减去奖励)?或者是存款金额+奖励直到取款后才被贷记?ewnc-是的,它增加了正确的金额减去奖励(尽管在此之后的任何存款/取款都不会给出正确的余额)
    class WithdrawListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {    
            double withdrawl = Double.parseDouble(withdrawField.getText());
    
            account.withdraw(withdrawl);
    
            result = account.getBalance();
            resultLabel.setText("New Balance: " + result);
            withdrawField.setText("0.00");
         }           
     }
    
    // Add this method in class cAccount:
    public void setToAmount(double amount) {
        balance = amount;
    }
    
    // Add a text field named "setAmountField" to enter the amount, and a button (choose a name) to apply it, and add this listener to the button:
    class SetAmountListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {     
            double setToAmount = Double.parseDouble(setAmountField.getText());
    
            account.setToAmount(setToAmount);
    
            result = account.getBalance();
            resultLabel.setText("New Balance: " + result);
            setAmountField.setText("0.00");
         }           
    }