java 异常练习_异常处理系列教材 (六)- Java 异常综合练习题

3 个答案

DQ970913

答案时间:2021-02-03

public class Account {

public double balance;//余额

public double getBalance() {

return balance;

}

public Account(){}

public Account(double a){

balance=a;

}

public double deposit(double a){

balance=balance+a;

return a;

}//存钱

//取钱

public double withDraw(double b) throws OverdraftException{

balance=balance-b;

if (balance<0)

throw new OverdraftException("您透支了"+(-balance)+"元");

return b;

}

public static void main(String[] args) {

double money;

double cunmoney;

Account account=new Account(50000);

cunmoney=account.deposit(600);

System.out.println("存入:"+cunmoney+"元。\n您的余额为:"+account.getBalance()+"元");

try {

money=account.withDraw(60000);

System.out.println("您成功取出 "+money+"元"+"\n您的余额为"+account.getBalance()+"元");

}catch (OverdraftException a){

a.printStackTrace();

}

}

}

public class OverdraftException extends Exception{

public double deficit;

public OverdraftException(){}

public OverdraftException(String a){

super(a);

}

}

zsy12345678902

答案时间:2020-11-17

class Account{

private double balance;

public double getBalance(){

return this.balance;

}

public void deposit(double money){

this.balance+=money;

}

public void withdraw(double money) throws OverdraftException {

if(balance

throw new OverdraftException("透支异常",money-balance);

}else{

balance-=money;

}

}

}

class OverdraftException extends Exception{

private double deficit;

public OverdraftException() {

}

public OverdraftException(String message,Double deficit) {

super(message);

}

public double getDeficit() {

return deficit;

}

}

173385

答案时间:2020-11-10

package grammar.test.exception.prectise;

public class Account {

private Double balance;

public void deposit(Double upamount) {

this.balance += upamount;

}

public void withdraw(Double reduceAmount) throws OverdraftException {

if(this.balance < reduceAmount) {

throw new OverdraftException(this,reduceAmount);

}else {

this.balance -= reduceAmount;

}

}

public Double getBalance() {

return balance;

}

public void setBalance(Double balance) {

this.balance = balance;

}

}

package grammar.test.exception.prectise;

public class OverdraftException extends Exception {

public OverdraftException() {

}

public OverdraftException(Account amount,Double money) {

super("账户余额为:" + amount.getBalance().toString() + ",不足抵扣" + money);

}

public OverdraftException(CheckingAccount amount,Double money) {

super("账户余额" + amount.getBalance().toString() +"加上透支金额"+ amount.getOverdraftProtection() + ",不足抵扣" + money);

}

}

package grammar.test.exception.prectise;

public class CheckingAccount extends Account {

private Double overdraftProtection;

@Override

public void deposit(Double upamount) {

super.setBalance(super.getBalance() + upamount);

}

@Override

public void withdraw(Double reduceAmount) throws OverdraftException{

if(super.getBalance() + this.overdraftProtection < reduceAmount) {

throw new OverdraftException(this,reduceAmount);

}

}

public Double getOverdraftProtection() {

return overdraftProtection;

}

public void setOverdraftProtection(Double overdraftProtection) {

this.overdraftProtection = overdraftProtection;

}

}

package grammar.test.exception.prectise;

public class TestAmount {

public static void main(String[] args) {

Account lisi = new Account();

lisi.setBalance(1234.23);

try {

lisi.withdraw(5431.23);

}catch(OverdraftException e) {

System.out.println("透支异常:");

e.printStackTrace();

}finally {

try {

CheckingAccount newAmount = new CheckingAccount();

newAmount.setBalance(1234.23);

newAmount.setOverdraftProtection(1542.33);

newAmount.withdraw(5431.23);

}catch(OverdraftException e) {

System.out.println("透支额度不足:");

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值