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();
}
}
}
}