package 实验;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(int id, double balance, double 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 (amount <= balance) {
balance -= amount;
} else {
System.out.println("余额不足!");
System.out.println("余额为" + balance);
}
}
public void deposit (double amount) {
if(amount>0) {
balance += amount;
}else {
System.out.println("存款错误,存款数目过小!");
}
System.out.println("余额为"+balance);
}
}
package 实验;
/*
*
*
* 创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性
overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法,其算法如下:
如果(取款金额<账户余额),
可直接取款
*/public class CheckAccount extends Account {
private double overdraft;
public CheckAccount(int id,double overdraft, double balance, double annualInterestRate) {
super(id, balance, annualInterestRate);
this.overdraft =overdraft;
}
/*如果(取款金额>账户余额),
计算需要透支的额度
判断可透支额 overdraft 是否足够支付本次透支需要,如果可以
将账户余额修改为 0,冲减可透支金额
如果不可以
提示用户超过可透支额的限额
*/public void withdraw (double amount) {
if(amount<=getBalance()) {
double amt = getBalance() - amount;
setBalance(amt);
}else {
if(overdraft>=amount-getBalance()) {
setOverdraft(overdraft-(amount-getBalance())) ;
setBalance(0);
}else {
System.out.println("超过可透支余额");
}
System.out.println("余额不足!");
System.out.println("余额为"+getBalance());
}
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
}
package 实验;
public class test {
public static void main(String[] args) {
// Account account = new Account(001, 20000, 0.045);
// account.setId(001);
// account.setBalance(20000);
// account.setAnnualInterestRate(0.045);
// account.withdraw(30000);
// account.withdraw(2500);
// account.deposit(3000);
// System.out.println("月利率为:"+account.getMonthlyInterest());
CheckAccount checkAccount = new CheckAccount(002,5000, 20000, 0.045);
checkAccount.setId(002);
checkAccount.setBalance(20000);
checkAccount.setAnnualInterestRate(0.045);
checkAccount.setOverdraft(5000);
checkAccount.withdraw(5000);
System.err.println("余额:"+checkAccount.getBalance()+"透支额度:"+checkAccount.getOverdraft());
checkAccount.withdraw(18000);
System.err.println("余额:"+checkAccount.getBalance()+"透支额度:"+checkAccount.getOverdraft());
checkAccount.withdraw(3000);
System.err.println("余额:"+checkAccount.getBalance()+"透支额度:"+checkAccount.getOverdraft());
}
}
1.父类有参构造中没有参数赋值
2.报错的内容贴出来看