AccountTest
package www.exer2;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
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 getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest(){
return annualInterestRate/12;
}
public void withdraw (double amount){
if (balance >= amount){
balance -= amount;
}else {
System.out.println("余额不足");
}
}
public void deposit(double amount){
if (amount > 0){
balance += amount;
}
}
}
package www.exer2;
public class AccountTest {
public static void main(String[] args) {
Account acct = new Account(1122, 20000, 0.045);
acct.withdraw(30000);
System.out.println("您的账户余额为: "+ acct.getBalance());
acct.withdraw(2500);
System.out.println("您的账户余额为: "+ acct.getBalance());
acct.deposit(3000);
System.out.println("您的账户余额为: "+ acct.getBalance());
System.out.println("月利率为 :"+ (acct.getAnnualInterestRate())*100+"%");
}
}
CheckAccountTest
package www.exer2;
public class CheckAccountTest {
public static void main(String[] args) {
CheckAccount acct = new CheckAccount(1122,20000,0.045,5000);
acct.withdraw(5000);
System.out.println("您的账户余额为 :" + acct.getBalance());
System.out.println("您的可透支额度为 :" + acct.getOverdraft());
acct.withdraw(18000);
System.out.println("您的账户余额为 :" + acct.getBalance());
System.out.println("您的可透支额度为 :" + acct.getOverdraft());
acct.withdraw(3000);
System.out.println("您的账户余额为 :" + acct.getBalance());
System.out.println("您的可透支额度为 :" + acct.getOverdraft());
}
}
package www.exer2;
public class CheckAccount extends Account {
private double overdraft;
public CheckAccount(int id, double balance, double annualInterestRate,double overdraft){
super(id, balance, annualInterestRate);
this.overdraft = overdraft;
}
@Override
public void withdraw(double amount) {
if (getBalance() >= amount){
super.withdraw(amount);
}else if (overdraft >= (amount - getBalance())){
overdraft -= amount - getBalance() ;
setBalance(0);
}else {
System.out.println("超过可透支限额");
}
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
}