题目:
代码:
Account类
package com.jichengsup;
public class Account {
private int id ;
private double balance ;
private double annual ;
public Account(int id, double balance, double annual) {
super();
this.id = id;
this.balance = balance;
this.annual = annual;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnual() {
return annual;
}
public void setAnnual(double annual) {
this.annual = annual;
}
//返还月利润率
public double getMonthly() {
return annual / 12 ;
}
public void withdraw(double amt) {
if(balance >= amt) {
balance -= amt ;
return ;
}
System.out.println("余额不足");
}
public void deposit(double amt) {
if(amt > 0) {
balance += amt ;
}
}
}
AccountTest类
package com.jichengsup;
public class AccountTest {
public static void main(String[] args) {
Account acc = new Account(1122, 20000, 0.045);
acc.withdraw(30000);
System.out.println("您的账户余额为:" + acc.getBalance());
acc.withdraw(2500);
System.out.println("您的账户余额为:" + acc.getBalance());
acc.withdraw(3000);
System.out.println("您的账户余额为:" + acc.getBalance());
}
}
CheckAccount类
package com.jichengsup;
public class CheckAccount extends Account {
private double famecard ;
public CheckAccount(int id, double balance, double annual, double famecard) {
super(id, balance, annual);
this.famecard = famecard;
}
public double getFamecard() {
return famecard;
}
public void setFamecard(double famecard) {
this.famecard = famecard;
}
@Override
public void withdraw(double amt) {
if(getBalance() > amt) {
super.withdraw(amt);
}else if(famecard > amt - getBalance()) {
famecard -= (amt - getBalance());
super.withdraw(getBalance());
// set.Balance(0);
}else {
System.out.println("超过可透支余额");
}
}
}
CheckAccounttest类
package com.jichengsup;
public class CheckAccounttest {
public static void main(String[] args) {
CheckAccount acc = new CheckAccount(1122, 20000, 0.045, 5000);
acc.withdraw(5000);
System.out.println("账户余额:" + acc.getBalance());
System.out.println("可透支余额:" + acc.getFamecard());
acc.withdraw(16000);
System.out.println("账户余额:" + acc.getBalance());
System.out.println("可透支余额:" + acc.getFamecard());
acc.withdraw(5000);
System.out.println("账户余额:" + acc.getBalance());
System.out.println("可透支余额:" + acc.getFamecard());
}
}