java基础:8.2 异常 编程练习
发布日期:2022-04-04 06:36:30
浏览次数:15
分类:博客文章
本文共 3000 字,大约阅读时间需要 10 分钟。
建立一个Account类
: 银行账号
建立一个类: CheckingAccount
支票账户,具备透支额度,继承Account
package Bank;public class Account { protected double balance; String name; //create account public Account() { this.balance = 0; name = "unknow "; } public Account(String name,double balance) { this.name = name; this.balance = balance; } // query balance public double getBalance() { return balance; } public void printInformation() { System.out.println("User: " + name + "'s account balance is " + balance); } //deposit money public void deposit(double askMoney) { balance += askMoney System.out.println(name + " 存款成功 ! 余额:"+this.getBalance()); } // withdraw money public void withdraw(double askMoney) throws OverdraftExceptions { if( balance < askMoney) { throw new OverdraftExceptions("取款失败,余额不足。 ",(balance-askMoney)); } else{ this.balance = balance - askMoney; System.out.println(name +" 取款成功 !余额:"+this.getBalance()); } }}
package Bank;public class CheckingAccount extends Account{ protected double overdraftProtection; protected CheckingAccount() { super(); } protected CheckingAccount(String name,double balance) { super(name,balance); } public CheckingAccount(String name,double balance, double protect) { super(name,balance); overdraftProtection = protect; } //get Overdraft Protection . public double getOverdraftProtection() { //添加的方法获得透支额度 return overdraftProtection; } @Override public void withdraw(double askMoney) throws OverdraftExceptions { if((balance-askMoney) >= 0) { this.balance = balance - askMoney; System.out.println("取款成功 !余额:"+this.getBalance()); } else if((balance-askMoney) >= -overdraftProtection) { this.balance = balance - askMoney; System.out.println("取款成功 !透支 " + this.getBalance() +" 剩余透支额: " + (overdraftProtection+balance)); } else throw new OverdraftExceptions("取款失败,额度不足。",(balance-askMoney)); } }
package Bank;public class OverdraftExceptions extends Exception{ double deficit; public OverdraftExceptions() { } public OverdraftExceptions(String message, double deficit) { super(message); this.deficit = deficit; } }
package Bank;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Account u1 = new Account("Amy",1000); u1.printInformation(); u1.deposit(200); u1.deposit(500); try { u1.withdraw(500); u1.withdraw(1000); u1.withdraw(500); } catch(OverdraftExceptions e) { e.printStackTrace(); } CheckingAccount u2 = new CheckingAccount("bob",1000,2000); u2.printInformation(); u2.deposit(400); u2.deposit(600); try { u2.withdraw(1900); u2.withdraw(500); u2.withdraw(900); u2.withdraw(1000); } catch(OverdraftExceptions e) { e.printStackTrace(); } }}
运行结果
转载地址:https://www.cnblogs.com/l20902/p/10610888.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年03月19日 20时14分28秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java读文件几种方式_JAVA读取文件的几种方式
2019-04-21
如何实现java文档注释_JavaDoc注释与帮助说明文档
2019-04-21
java判断ipv6地址类型_IPv6地址类型与格式
2019-04-21
linux java jdk_linux安装JDK
2019-04-21
JAVA中math的作用_Java中Random 和 Math 的概述及使用(二十六)
2019-04-21
mysql排序rank_MySQL_实现组内排序-Oracle中的rank()函数的功能
2019-04-21
linux 模拟键盘输入到进程,Linux 下模拟键盘输入
2019-04-21
linux服务器上已安装R 用户下载R包,R语言安装R package的2种方法
2019-04-21
linux下mysql 备份方法,Linux下mysql数据库备份方法小结
2019-04-21
肺部ct重建_胸片检查容易漏诊肺癌,去年正常今年晚期常发生,体检一定要做CT...
2019-04-21