步骤 2 : 答案-异常综合1 步骤 3 : 练习-异常综合2 步骤 4 : 答案-异常综合2
这是一个类图
Account类: 银行账号 属性: balance 余额 方法: getBalance() 获取余额 方法: deposit() 存钱 方法: withdraw() 取钱 OverdraftException: 透支异常,继承Exception 属性: deficit 透支额
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
5分18秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器 package exception;
public class Account {
protected double balance;
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void deposit(double amt){
this.balance+=amt;
}
public void withdraw(double amt) throws OverDraftException{
if(this.balance<amt)
throw new OverDraftException("余额不足", amt-this.balance);
this.balance-=amt;
}
public static void main(String[] args) {
//开户存了1000
Account a = new Account(1000);
//存钱1000
a.deposit(1000);
//查看余额
System.out.println(a.getBalance());
try {
//取2001
a.withdraw(2001);
} catch (OverDraftException e) {
System.err.println("透支金额:"+e.getDeficit());
e.printStackTrace();
}
}
}
package exception;
public class OverDraftException extends Exception{
private double deficit;
public double getDeficit() {
return deficit;
}
public OverDraftException(String msg, double deficit) {
super(msg);
this.deficit = deficit;
}
}
类: CheckingAccount 支票账户,具备透支额度,继承Account
属性:overdraftProtection 透支额度
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
4分41秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器 package exception;
public class CheckingAccount extends Account {
private double overdraftProtection;
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance, double overdraftProtection) {
super(balance);
this.overdraftProtection = overdraftProtection;
}
public void withdraw(double amt) throws OverDraftException {
if (amt > this.balance + overdraftProtection) {
double deficit = amt - (this.balance + overdraftProtection);
throw new OverDraftException("透支额度超标", deficit);
}
this.balance -= amt;
}
public static void main(String[] args) {
//开户存了1000块,拥有500的透支额度
CheckingAccount a = new CheckingAccount(1000, 500);
//存了1000
a.deposit(1000);
//查询余额
System.out.println(a.getBalance());
try {
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
} catch (OverDraftException e) {
System.err.println("透支超额:"+e.getDeficit());
e.printStackTrace();
}
}
}
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2022-09-03
支票账户答案
2 个答案
RobertLewandowski9 跳转到问题位置 答案时间:2023-09-26 存入99.0
当前余额为2122.0
取出99999.0
对不起,已透支,透支额为97877.0
LUO萝北 跳转到问题位置 答案时间:2023-05-13 public class Account {
private static double balance;
public static double getBalance() {
return balance;
}
public static void deposit(double in) {
balance+=in;
}
public static void withdraw(double wd) {
balance-=wd;
}
}
class OverdraftException extends Exception{
public OverdraftException() {}
public OverdraftException(String gripe) {
super(gripe);
}
}
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2022-09-03
银行账号答案
3 个答案
mucheng 跳转到问题位置 答案时间:2023-11-11 class Account{
double balance;
Account(double balance){
this.balance=balance;
}
double getBalance(){
return balance;
}
void deposit(double money){
balance+=money;
}
void withdraw(double money) throws OverdraftException{
if(money>balance){
throw new OverdraftException("余额不足",money-balance);
}
balance-=money;
}
public static void main(String[] args) {
Account ac = new Account(1000);
ac.deposit(1000);
System.out.println(ac.getBalance());
try {
ac.withdraw(2001);
} catch (OverdraftException e) {
System.out.println("透支金额:" + e.getDeficit());
e.printStackTrace();
}
}
}
class OverdraftException extends Exception{
double deficit;
OverdraftException(String message,double deficit){
super(message);
this.deficit=deficit;
}
double getDeficit(){
return deficit;
}
}
月姑娘 跳转到问题位置 答案时间:2023-11-03 package com.zhy.middle.exception;
public class OverdraftException extends Exception{
private double deficit;
public OverdraftException(String message,double deficit) {
super(message);
this.deficit = deficit;
}
public double getDeficit() {
return deficit;
}
}
Aeclar 跳转到问题位置 答案时间:2023-04-28 运行结果:
你的余额:500.0
存钱成功,已存300.0入户
你的余额:800.0
取钱成功,已从账户取走500.0元
你的余额:300.0
你的透支额度不够!(你当前的额度为:200.0)
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2022-05-12
答案
2021-09-04
答: 练习1,练习2
2021-05-23
练习-异常综合
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 30 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|