java异常类基础复习
在这里插入代码片
package ssudt.com.cok;//设计一个项目,客户可以从银行里取钱,但是每次只能取500,超过就会报错
import java.util.Scanner;
class overnumberException extends Exception{
String message;
overnumberException(){
message="存取的钱超过了100,取太多了,给爷爬";
}
public String toString(){
return message;
}
}
class bank{
double deposite;
public void savemoney(double amount){
deposite=deposite+amount;
System.out.println("存钱可没有限制");
}
bank(double deposite){
this.deposite=deposite;
}
public void countmoney(double amount ) throws overnumberException{
if(amount>100)
throw new overnumberException();
else
deposite=deposite-amount;
System.out.println("算你识相");
}
public double getRestMoney(){
return deposite;
}
}
public class Main {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("请输入账户内本来有的本金");
bank Bank=new bank(input.nextDouble());
try {
System.out.println("请输入要存入的金额");
Bank.savemoney(input.nextDouble());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("显示剩余的费用");
System.out.println(Bank.getRestMoney());
System.out.println("请输入要取走的金额");
Bank.countmoney(input.nextDouble());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("显示剩余的费用");
System.out.println(Bank.getRestMoney());
System.out.println("请输入要取走的金额");
Bank.countmoney(input.nextDouble());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("显示剩余的费用");
System.out.println(Bank.getRestMoney());
}catch (overnumberException e){
System.out.println(e.toString());
}finally {
System.out.println("利用finally显示最终费用");
System.out.println(Bank.getRestMoney());
}
}
}