1、写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:
账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器方法(getter 和
setter 方法),返回月利率的方法 getMonthlyInterest(),取款方法 withdraw(),存款方法
deposit()。
Account
private int id
private double balance
private double annualInterestRate
public Account (int id, double balance, double annualInterestRate )
public int getId()
public double getBalance()
public double getAnnualInterestRate()
public void setId( int id)
public void setBalance(double balance)
public void setAnnualInterestRate(double annualInterestRate)
public double getMonthlyInterest()
public void withdraw (double amount)
public void deposit (double amount)
写一个用户程序测试 Account 类。在用户程序中,创建一个账号为 1122、余额为 20000、
年利率 4.5%的 Account 对象。使用 withdraw 方法提款 30000 元,并打印余额。
再使用 withdraw 方法提款 2500 元,使用 deposit 方法存款 3000 元,然后打印余额和月利
率。
提示:在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不
能,应给出提示。
运行结果如图所示:
2、创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性
overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法,其算法如下:
如果(取款金额<账户余额),
可直接取款
如果(取款金额>账户余额),
计算需要透支的额度
判断可透支额 overdraft 是否足够支付本次透支需要,如果可以
将账户余额修改为 0,冲减可透支金额
如果不可以
提示用户超过可透支额的限额
要求:写一个用户程序测试 CheckAccount 类。在用户程序中,创建一个账号为 1122、余
额为 20000、年利率 4.5%,可透支限额为 5000 元的 CheckAccount 对象。
使用 withdraw 方法提款 5000 元,并打印账户余额和可透支额。
再使用 withdraw 方法提款 18000 元,并打印账户余额和可透支额。
再使用 withdraw 方法提款 3000 元,并打印账户余额和可透支额。
提示:
(1) 子类 CheckAccount 的构造方法需要将从父类继承的 3 个属性和子类自己的属性全
部初始化。
(2) 父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw
方法中需要修改它的值,因此应修改父类的 balance 属性,定义其为 protected。
运行结果如下图所示:
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 getAnnualInterestRate()/12;
}
//取钱
public void withdraw (double amount){
if (balance>=amount){
balance-=amount;
//System.out.println("取款: "+amount+","+"当前余额为: "+balance);
return;
}
else{
//System.out.println("余额不足!");
//System.out.println("当前余额为: "+balance);
return;
}
}
//存钱
public void deposit (double amount){
balance+=amount;
System.out.println("存款: "+amount+","+"当前余额为: "+balance);
}
}
public class AccountTest {
public static void main(String[] args) {
Account account=new Account(1122,20000,0.045);
account.withdraw(30000);
account.deposit(3000);
System.out.println("月利率为: "+account.getMonthlyInterest());
}
}
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;
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
//取钱
public void withdraw (double amount){
if (amount<=getBalance()) {
//方式一,但是银行一般不会提供setblance来设置余额,所以用第二种
//setBalance(getBalance()-amount);
//方式二
super.withdraw(amount);
System.out.println("您的账户余额为: "+getBalance()+"\n"+"您的可透支余额为: "+getOverdraft());
}
else if (amount>getBalance()){
if (amount<=(getBalance()+getOverdraft())){
amount-=getBalance();
setBalance(0);
setOverdraft(getOverdraft()-amount);
System.out.println("您的账户余额为: "+getBalance()+"\n"+"您的可透支余额为: "+getOverdraft());
}else {
System.out.println("超出可透支额度!");
System.out.println("您的账户余额为: " + getBalance() + "\n" + "您的可透支余额为: " + getOverdraft());
}
}
}
}
public class CheckAccountTest {
public static void main(String[] args) {
CheckAccount checkAccount=new CheckAccount(1122,20000,0.045,5000);
checkAccount.withdraw(5000);
checkAccount.withdraw(18000);
checkAccount.withdraw(3000);
}
}
创作不易,喜欢就点个赞吧.