简体   繁体   中英

Bank account arithmetic java

I'm trying to design a bank account GUI that works for two types of accounts; current and savings. I have a few questions about this

  1. When I deposit £500 or more a £10 reward is meant to be added to the balance but this is only credited after I've withdrawn money, why??
  2. My first balance calculation after depositing/withdrawing is correct but after that it's never correct. Is there an error in my method?
  3. In my code the initial balance is 0 but what if I want the user to set the balance in the gui?

This has been driving me mad for days so any advice or help would be greatly appreciated!

Main menu

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


}
}

account frame that allows withdrawls & deposits

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

     }

current account class

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

Main class

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

    }

}

在此处输入图片说明

Yes, It looks like you might be doing unwanted changes in your withdraw and deposit methods.

Using a += b; or a -= b; will change the value, because it's the same as a = a + b; and a = a - b; .

Try this:

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

EDIT: Deposit can be even shorter:

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

Also, change this. Else when you have 100, and deposit 50, you might end up with 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");
     }           
 }

And, you might also want to use your withdraw method in 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");
     }           
 }

To set an amount:

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

Didn't test this, so there could be typos in there. Tell me if it doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM