银行账户算术java

时间:2015-11-30 16:44:17

标签: java swing user-interface

我正在尝试设计适用于两种帐户的银行帐户GUI;现在和节省。我对此有几个问题

  1. 当我存入500英镑或以上时,10英镑的奖励将被添加到余额中,但这只是在我提取资金后记入的,为什么?
  2. 存款/取款后我的第一次余额计算是正确的,但在此之后它永远不正确。我的方法有错误吗?
  3. 在我的代码中,初始余额为0,但如果我希望用户在gui中设置余额怎么办?
  4. 这让我疯了几天,所以任何建议或帮助都会非常感激!

    主菜单

    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;
        }
    }
    

    主要课程

    package assignment;
    
    import javax.swing.JFrame;
    
    public class Assignment 
    {
        int accType;
        private final boolean answer = true ;
    
        public static void main(String[] args) 
        {
            mainMenuFrame frame = new mainMenuFrame();
            frame.setTitle("Bank Account");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);        
    
        }
    
    }
    

    enter image description here

1 个答案:

答案 0 :(得分:5)

是的,看起来您可能会在withdrawdeposit方法中进行不必要的更改。

使用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; 
    }
}

编辑:存款可以更短:

/* 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)
{
    balance += amount;

    if (amount>=500)
    {
        balance += 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");
     }           
 }

并且,您可能还想在account中使用撤消方法:

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");
     }           
}

没有对此进行测试,因此可能存在拼写错误。告诉我它是否无效。