银行账户算术java

-1 java user-interface swing

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

  1. 当我存入500英镑或更多时,10英镑的奖励将被添加到余额中,但这只是在我提取资金后记入的,为什么?
  2. 存款/取款后我的第一次余额计算是正确的,但在此之后它永远不正确.我的方法有错误吗?
  3. 在我的代码中,初始余额为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);


}
}
Run Code Online (Sandbox Code Playgroud)

允许提款和存款的账户框架

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

     }
Run Code Online (Sandbox Code Playgroud)

经常账户类

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

主要课程

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

    }

}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

reg*_*res 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; 
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:存款可以更短:

/* 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;     
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,改变这一点.另外,当你有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");
     }           
 }
Run Code Online (Sandbox Code Playgroud)

并且,您可能还想在以下位置使用撤消方法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");
     }           
 }
Run Code Online (Sandbox Code Playgroud)

设定金额:

// 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");
     }           
}
Run Code Online (Sandbox Code Playgroud)

没有测试这个,所以那里可能会有拼写错误.告诉我它是否不起作用.