【问题标题】:Trouble with transfer and withdraw methods for Bank program银行程序的转账和取款方式出现问题
【发布时间】:2016-11-21 03:37:37
【问题描述】:

我正在编写一个银行 GUI 程序,该程序将允许用户向支票和储蓄账户转账、取款或存款,还允许他们检查每个账户的余额。余额和存款方​​式运行良好。

只要可用,转账方法应允许用户转账他们在文本字段中输入的金额。如果不是,则抛出异常。问题是,即使有足够的,仍然会抛出异常。

至于我的提款方法,它允许用户以 20 美元的增量提款。它有效,但它一直显示一条消息,说资金不足。在用户为两个帐户总共提款四次后,它还应该收取 1.50 美元。

银行类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Banking extends JPanel
{ 
   private JFrame frame;
   private JPanel panel;
   private JButton withdraw;
   private JButton deposit;
   private JButton transfer;
   private JButton balance;
   private JRadioButton checking;
   private JRadioButton savings;
   private JTextField input;
   private Account checkingAccount;
   private Account savingsAccount;
   private Account currentAccount;
   private double amount;

   public Banking(Account checkingAccount,Account savingsAccount)
   {

       frame=new JFrame("ATM");
       panel=new JPanel();
       withdraw=new JButton("Withdraw");
       deposit=new JButton("Deposit");
       transfer=new JButton("Transfer");
       balance=new JButton("Balance");
       checking=new JRadioButton("Checking");
       savings=new JRadioButton("Savings");
       input=new JTextField("");
       this.checkingAccount=checkingAccount;
       this.savingsAccount=savingsAccount;

       panel.setLayout(new GridLayout(4,2));

       panel.add(withdraw);panel.add(deposit);
       panel.add(transfer);panel.add(balance);
       panel.add(checking);panel.add(savings);
       panel.add(input);

       frame.add(panel);
       frame.pack();
       frame.setLocationRelativeTo(null);
       frame.setSize(600,300);
       frame.setVisible(true);


       checking.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e)
           {
               if(checking.isSelected())
               {
                   currentAccount=checkingAccount;
                   savings.setSelected(false);
               }
           }
       });

       savings.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e)
           {
               if(savings.isSelected())
               {
                   currentAccount=savingsAccount;
                   checking.setSelected(false);
               }
           }
       });


       withdraw.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e)
           {


             try
             {
                 amount=Double.parseDouble(input.getText());
                 if(amount%20==0)
                 {
                     currentAccount.withdraw(amount);
                     JOptionPane.showMessageDialog(null, "You've withdrawn $"+amount);


                 }
                 else
                 {
                     JOptionPane.showMessageDialog(null, "You can only withdraw money in increments of $20");

                 }
             }
             catch(NumberFormatException a)
             {
                 JOptionPane.showMessageDialog(null, "Please enter a numerical number");
             } 
             catch (InsufficientFunds e1)
             {
                 JOptionPane.showMessageDialog(null, "Not enough funds");

             }

           }
       });


     transfer.addActionListener(new ActionListener(){

         public void actionPerformed(ActionEvent e)
         {
             if(currentAccount.equals(savingsAccount))
             {

                 try
                 {
                     currentAccount.transferTo(savingsAccount, amount);
                 }
                 catch(NumberFormatException a)
                 {
                     JOptionPane.showMessageDialog(null, "Please enter a numerical number");
                 } 
                 catch (InsufficientFunds e1)
                 {
                     JOptionPane.showMessageDialog(null, "Not enough funds");

                 }
             }
             else
             {
                 try
                 {
                     currentAccount.transferTo(checkingAccount, amount);
                 }
                 catch(NumberFormatException a)
                 {
                     JOptionPane.showMessageDialog(null, "Please enter a numerical number");
                 } 
                 catch (InsufficientFunds e1)
                 {
                     JOptionPane.showMessageDialog(null, "Not enough funds");
             }
     }
         }
   });

   deposit.addActionListener(new ActionListener(){

       public void actionPerformed(ActionEvent e)
       {
           try
           {
               amount=Double.parseDouble(input.getText());
               currentAccount.deposit(amount);
                 JOptionPane.showMessageDialog(null, "You've deposited $"+amount);


           }
          catch(NumberFormatException a)
          {
                 JOptionPane.showMessageDialog(null, "Please enter a numerical number");
          } 
       }
   });


   balance.addActionListener(new ActionListener(){

       public void actionPerformed(ActionEvent e)
       {

           JOptionPane.showMessageDialog(null, "You're balance is $ "+currentAccount.getBalance());

       }
   });


}
   public static void main(String[] args)
   {
       Account checking=new Account(3000.00);
       Account savings=new Account(5000.00);
       Banking myBank=new Banking(checking,savings);
   }
} 

帐户类

public class Account
{
private double balance;
static int counter=0;

public Account(double balance)
{
    this.balance=balance;
}

public void withdraw(double amount) throws InsufficientFunds
{
     int counter=0;

    if(amount<=balance)
    {
        balance=balance-amount;
        counter++;

        if(counter>4)
        {
            if(balance<1.50)
            {
                throw new InsufficientFunds(1.50);
            }
            else
            {
                balance=balance-1.50;
            }
        }

    else
    {
        double needed=amount-balance;
        throw new InsufficientFunds(needed);
    }
    }


}

public double getBalance()
{
    return balance;
}

public void deposit(double amount)
{
    balance=balance+amount;
}

public void transferTo(Account bank, double amount) throws InsufficientFunds
{
    if(amount<=balance)
    {
        withdraw(amount);
        bank.deposit(amount);
    }
    else
    {
        throw new InsufficientFunds(amount);
    }
}
}

资金不足类

import java.io.*;
public class InsufficientFunds extends Exception
{
private double amount;

public InsufficientFunds(double amount)
{
      this.amount = amount;
}

public double getAmount()
{
      return amount;
}
}

【问题讨论】:

  • 我认为您遇到了if...then...else 大括号问题。在withdraw() 中,else throw InsufficientFunds() 与外部account &lt;= balance 缩进,但实际上与counter&gt;4 配对。此外,您的 counter 是局部变量而不是类成员,因此无法正常工作。
  • 如果用户没有足够的钱支付 1.50 美元的费用,我试图抛出异常。另外,我认为静态变量是类成员?
  • 谢谢,解决了问题

标签: java swing user-interface inheritance swingx


【解决方案1】:

从你的解释中我只能看到 2 个问题,

*在withdraw方法中,你声明了一个新的计数器,这将使计数器在增加counter++之后总是为1。

public void withdraw(double amount) throws InsufficientFunds{
     int counter = 0; // should be removed ..
}

**如果计数器大于4,在扣除提款金额之前,你应该已经检查账户是否有足够的钱支付1.50的费用

if(amount<=balance)
    {

    if(counter>4)
    {

        if(balance<(amount+1.50))
        {
            throw new InsufficientFunds((amount+1.50)-balance);
        }
        else
        {
            balance=balance-amount-1.50;
            counter++;
        }

    }else{

    balance=balance-amount;
    counter++;
    }
}

【讨论】:

  • 我其实已经解决了这个问题,但是我又遇到了一个问题:我的传输按钮在点击时根本不起作用
  • “但是我又遇到了一个问题” 所以在另一个问题线程中再问一个问题。 SO 是一个问答网站,而不是帮助台。如果它有助于解决问题,请accept the answer
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 2022-06-16
  • 2019-07-22
  • 1970-01-01
相关资源
最近更新 更多