1、写一个名为 Account 的类模拟账户。该类包括的属性:账号 id,余额 balance,年利率 annualInterestRate ;包含的方法:访问器方法 getter 和 setter 方法,返回月利率的方法getMonthlyInterest(),取款方法 withdraw(),存款方法deposit()
public class Account {
private int id;
private double balance;
private double annuallnterestRate;
//constructor
public Account(){
}
public Account(int id, double balance, double annuallnterestRate) {
super();
this.id = id;
this.balance = balance;
this.annuallnterestRate = annuallnterestRate;
}
//*********method*********
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 getAnnuallnterestRate() {
return annuallnterestRate;
}
public void setAnnuallnterestRate(double annuallnterestRate) {
this.annuallnterestRate = annuallnterestRate;
}
//获取月利率
public double getMonthlyinterest(){
return annuallnterestRate/12;
}
//取钱
public void withdraw(double amount){
if(amount>balance){
System.out.println("余额不足!");
System.out.println("您的账户余额为:"+balance);
}else{
balance-=amount;
//System.out.println("您的账户余额为:"+balance);
}
}
//存钱
public void deposit(double amount){
balance+=amount;
System.out.println("您的账户余额为:"+balance);
}
}
2.创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法,其算法如下:
public class CheckAccount extends Account {
private double overdraft;//可透支限额
public CheckAccount(){
super();
}
public CheckAccount(int id, double balance, double annuallnterestRate,double overdraft){
super(id,balance,annuallnterestRate);
this.overdraft=overdraft;
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
public void withdraw(double amount){
if(amount<=getBalance()){
System.out.println("可直接取款");
//方式1
//setBalance(getBalance()-amount);
//方式2
super.withdraw(amount);
}else if(overdraft>(amount-getBalance())){
overdraft-=(amount-getBalance());
setBalance(0);
}else{
System.out.println("超过可透支额的限额!");
}
}
}
3.测试Account类和CheckAccount类
(1)写一个用户程序测试Account 类。在用户程序中,创建一个账号为 1122 、余额为 20000 、
年利率 4.5% 的 Account 对象。使用 withdraw 方法提款 30000 元,并打印余额。再使用withdraw 方法提款 2500 元,使用 deposit 方法存款 3000 元,然后打印余额和月利率。
(2)写一个用户程序测试 CheckAccount 类。在用户程序中,创建一个账号为 1122 、余额为 20000 、年利 率 4.5%4.5%,可透支限额为 5000 元的 CheckAccount 对象。使用withdraw 方法提款 5000 元,并打印账户余额和可透支额。再使用withdraw 方法提款 18000 元,并打印账户余额和可透支额。再使用withdraw 方法提款 3000 元,并打印账户余额和可透支额。
public class AccountTest {
public static void main(String[] args){
Account account=new Account(1122, 20000, 0.045);
account.withdraw(30000);
account.withdraw(2500);
account.deposit(3000);
System.out.println("月利率为"+account.getMonthlyinterest());
System.out.println("******************************");
CheckAccount c=new CheckAccount(1122, 20000, 0.045, 5000);
c.withdraw(5000);
System.out.println("账户余额为:"+c.getBalance());
System.out.println("可透支额为:"+c.getOverdraft());
c.withdraw(18000);
System.out.println("账户余额为:"+c.getBalance());
System.out.println("可透支额为:"+c.getOverdraft());
c.withdraw(3000);
System.out.println("账户余额为:"+c.getBalance());
System.out.println("可透支额为:"+c.getOverdraft());
}
}