📄 account.java
字号:
class AccountException extends Exception //自定义帐户异常类
{
public AccountException() {super();}
public AccountException(String s) {super(s);}
}
class NotEnoughBalanceException extends AccountException //余额不足异常
{
Account account;
public NotEnoughBalanceException(Account a)
{
super("余额不足!");
account = a;
}
public String toString()
{
return "帐户["+account.toString()+"]的余额不足!";
}
}
class OutOfLimitException extends AccountException //取款超出范围
{
Account account;
double maxLimit;
public OutOfLimitException(Account a, double limit)
{
super("超出取款最大金额!");
account = a;
maxLimit = limit;
}
public String toString()
{
return "帐户["+account.toString()+"]每次最多取款["+maxLimit+"]元!";
}
}
class Account // 帐户类
{
private String ID;
private double balance;
// ...
public Account(String aID, double b)
{
ID = aID;
balance = b;
}
public void drawing(double m) throws AccountException //取款方法
{
if(balance<m) // 检测错误
{
if(m>1000.00) // 检测错误
throw new OutOfLimitException(this,1000.00); //抛出异常
else
throw new NotEnoughBalanceException(this); //抛出异常
}
else
balance = balance - m;
}
public double getBalance()
{
return balance;
}
public String toString()
{
return ID;
}
// ...
}
class AccountTest
{
public static void main(String[] args) throws Exception
{
Account acc = new Account("10001",200.0);
if(args.length==1)
{
try
{
System.out.println("取款前余额:"+acc.getBalance());
acc.drawing(Double.parseDouble(args[0])); // 可能抛出异常
System.out.println("取款后余额:"+acc.getBalance());
}
catch(NotEnoughBalanceException e)
{
System.err.println(e);
}
catch(OutOfLimitException e)
{
System.err.println(e);
}
catch(AccountException e)
{
System.err.println("其它的帐号异常!");
}
catch(Exception e)
{
System.err.println("其它的异常!\n"+e);
throw e;
}
}
else
{
System.out.println("指定一个数值作为命令行参数!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -