Java实验 银行取款异常得抛出

(1)在定义一个银行类 Bank 时, 有 deposit、withdraw、showBalance 等方
法。若取钱数大于余额则作为异常处理(InsufficientFundsException)。产生异常的
条件是余额少于取额,因此是否抛出异常要判断条件,要定义好自己的异常类。
package DD;
import java.util.*;
public class Bank {
public double withdraw;
public double balance;
public Bank(double balance) {
this.balance=balance;
}
public void Deposit(double deposit) {
System.out.println("请输入存款金额:");
balance = balance+deposit;
System.out.println("余额为:"+balance);
}
public void withDraw (double withDraw) throws InsufficientFundsException  {
System.out.println("请输入取款金额:");
if (withdraw throw new InsufficientFundsException ();
}
balance=balance-withDraw;
}
public static void main (String []args) {
Bank ba = new Bank (0);
ba.Deposit(100);
try {
ba.withDraw(200);
} catch (InsufficientFundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

package DD;


public class InsufficientFundsException  extends Exception{
Bank bal;
public double withdraw;
InsufficientFundsException (){};
@Override
public String toString() {
String str="余额不足";
return str;
}
}


你可能感兴趣的:(Java实验 银行取款异常得抛出)