我的计算机科学课的任务是抛出异常。 该作业创建了一个小程序来管理用户的银行帐户。
我的问题如下:只有当用户试图提取的金额超过余额时才需要抛出异常(InsufficientFundsException)。但是,我只想在提款而不是存款的情况下抛出InsufficientFundsException。
以下是问题主要针对我遇到问题的部分的方法的代码:
actionPerformed方法(我正在实现ActionListener接口)
此方法需要根据点击的按钮 - 存款或取款按钮 - 处理该交易方法。事务方法只是将传递给方法的金额加到或减去Account对象的余额。然后,如果有异常,它将捕获异常并打印错误等。但是当前捕获的异常同时适用于撤销和存款交易,而仍然需要抛出的那个 - InsufficientFundsException - 仅适用于撤回交易。另外,我无法修改Account对象中的withdraw方法以抛出异常。
getAmount方法:
我没有选择在此方法中抛出InsufficientFundsException
我试着添加这个:
if(getAmount(tf4) > acc1.getBalance())
{
throw InsufficientFundsException("Insufficient Funds");
}
在退出电话会议之前:
if (e.getSource() == withdraw)
{
try
{
acc1.withdraw(getAmount(tf4));
refreshFields();
status.setText("Withdrawal Processed");
}
catch(EmptyFieldException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
catch(NumberFormatException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
catch(NegativeAmountException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
}
但是我收到错误,因为actionPerformed方法不能throws
InsufficientFundsException。
另外如果我在一个try / catch块中尝试catch
一个InsufficientFundsException,我得到另一个try / catch块的错误,因为我没有捕获异常...因为我不想要抓住那个例外。
根据要求,这是完整的代码:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class AccountApplet extends JApplet implements ActionListener
{
Container c = getContentPane();
JPanel p1 = new JPanel(),
p2 = new JPanel(),
p3 = new JPanel(),
p4 = new JPanel();
JLabel lab1 = new JLabel("Account ID");
JLabel lab2 = new JLabel("Account Balance");
JLabel lab3 = new JLabel("Deposit");
JLabel lab4 = new JLabel("Withdraw");
JLabel status = new JLabel("Status Bar");
JTextField tf1 = new JTextField(),
tf2 = new JTextField(),
tf3 = new JTextField(),
tf4 = new JTextField();
JButton deposit = new JButton("Deposit"),
withdraw = new JButton("Withdraw");
Account acc1 = new Account(1234, 1000.00);
double amount;
public void init()
{
tf1.setEditable(false);
tf2.setEditable(false);
c.setLayout(new BorderLayout());
c.add(p1, BorderLayout.WEST);
p1.setBorder(new TitledBorder("Display Account Information"));
p1.setLayout(new GridLayout(2,2));
p1.add(lab1);
p1.add(tf1);
p1.add(lab2);
p1.add(tf2);
c.add(p2, BorderLayout.EAST);
p2.setBorder(new TitledBorder("Deposit or Withdraw Funds"));
p2.setLayout(new BorderLayout());
p2.add(p3, BorderLayout.WEST);
p2.add(p4, BorderLayout.EAST);
p3.setLayout(new GridLayout(2,1));
p3.add(lab3);
p3.add(lab4);
p4.setLayout(new GridLayout(2,2));
p4.add(tf3);
p4.add(deposit);
p4.add(tf4);
p4.add(withdraw);
c.add(status, BorderLayout.SOUTH);
deposit.addActionListener(this);
withdraw.addActionListener(this);
refreshFields();
}
public void actionPerformed(ActionEvent e)
{
status.setText("");
if (e.getSource() == deposit)
{
try
{
acc1.deposit(getAmount(tf3));
refreshFields();
status.setText("Withdrawal Processed");
}
catch(EmptyFieldException ex)
{
status.setText(ex.getMessage() + "deposit");
}
catch(NumberFormatException ex)
{
status.setText(ex.getMessage() + "deposit");
}
catch(NegativeAmountException ex)
{
status.setText(ex.getMessage() + "deposit");
}
}
if (e.getSource() == withdraw)
{
try
{
acc1.withdraw(getAmount(tf4));
refreshFields();
status.setText("Withdrawal Processed");
}
catch(EmptyFieldException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
catch(NumberFormatException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
catch(NegativeAmountException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
}
}
public void refreshFields()
{
tf1.setText(String.valueOf(acc1.getId()));
tf2.setText("$" + String.valueOf(acc1.getBalance()));
tf3.setText("");
tf4.setText("");
status.setText("");
}
public double getAmount(JTextField tf) throws EmptyFieldException ,
NumberFormatException,
NegativeAmountException
{
if(tf.getText().equals(""))
{
throw new EmptyFieldException("Empty field not allowed for ");
}
try
{
amount = Double.parseDouble(tf.getText());
}
catch(Throwable ex)
{
throw new NumberFormatException("Insufficient funds", ex);
}
if(amount < 0)
{
throw new NegativeAmountException("Negative amount not allowed for ");
}
else
{
return Double.parseDouble(tf.getText());
}
}
}
帐户类代码:
public class Account
{
private int id;
private double balance;
Account(int id, double balance)
{
this.id = id;
this.balance = balance;
}
public int getId()
{
return id;
}
public double getBalance()
{
return balance;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public void deposit(double amount)
{
balance += amount;
}
public void withdraw(double amount)
{
balance -= amount;
}
}
异常代码:
public class EmptyFieldException extends Exception
{
EmptyFieldException(String message)
{
super(message);
}
}
,
public class InsufficientFundsException extends Exception
{
InsufficientFundsException(String message)
{
super(message);
}
}
,
public class NegativeAmountException extends Exception
{
NegativeAmountException(String message)
{
super(message);
}
}
任何人都可以帮助我吗?我应该在哪里抛出这个例外?
答案 0 :(得分:1)
使用异常链接机制。例如尝试这样的事情。
if (e.getSource() == withdraw) {
try
{
if(getAmount(tf4) > acc1.getBalance())
{
throw InsufficientFundsException("Insufficient Funds");
}
acc1.withdraw(getAmount(tf4));
refreshFields();
status.setText("Withdrawal Processed");
}
catch(InsufficientFundsException ex)
{
//rethrow some unchecked exception like IllegalArgumentException
//or your own checked exception like EmptyFieldException
throw new EmptyFieldException("some message", ex);
//or add the exception message and trace to status.
status.setText(ex.getMessage() + "withdraw");
}
catch(EmptyFieldException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
catch(NumberFormatException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
catch(NegativeAmountException ex)
{
status.setText(ex.getMessage() + "withdraw");
}
}
在您的代码中确定没有办法有意义地抛出InsufficientFundsException。最好的方法是避免它,只需使用status.setText()直接在if检查中打印消息。
if(getAmount(tf4) > acc1.getBalance())
{
status.setText("Insufficient Funds - Withdraw cancelled");
}