JAVA---super练习
Account.java
package exer;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(int id,double balance,double annualInterestRate){
super();
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;
return;
}
System.out.println("余额不足");
}
public void deposit(double amount){
if(amount>0){
balance+=amount;
}
}
}
AccountTest.java
package exer;
/*
* 写一个用户程序测试Account类。在用户程序中,
* 创建一个账号为1122、余额为20000、年利率4.5%的Account对象。
* 使用withdraw方法提款30000元,并打印余额。
* 再使用withdraw方法提款2500元,
* 使用deposit方法存款3000元,然后打印余额和月利率。
*
*/
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.getMonthlyInterest()*100)+"%");
}
}
CheckAccount.java
package exer;
/*
* 创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。
* 在CheckAccount类中重写withdraw方法,其算法如下:
如果(取款金额<账户余额),
可直接取款
如果(取款金额>账户余额),
计算需要透支的额度
判断可透支额overdraft是否足够支付本次透支需要,如果可以
将账户余额修改为0,修改可透支金额
如果不可以
提示用户超过可透支额的限额
*
*
*/
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;
}
@Override
public void withdraw(double amount) {
if(getBalance() >= amount){
super.withdraw(amount);
}else if(overdraft >=amount-getBalance()){
overdraft-=(amount-getBalance());
super.withdraw(getBalance());
}else{
System.out.println("超过可透支限额!");
}
}
}
CheckAccountTest.java
package exer;
/*
* 写一个用户程序测试CheckAccount类。
* 在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。
使用withdraw方法提款5000元,并打印账户余额和可透支额。
再使用withdraw方法提款18000元,并打印账户余额和可透支额。
再使用withdraw方法提款3000元,并打印账户余额和可透支额。
*
*/
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());
}
}