不完善的小银行
package Cun; public class Account { int id; double balance; double yearRate; public Account(int id, double balance, double yearRate) { super(); this.id = id; this.balance = balance; this.yearRate = yearRate; } 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 getYearRate() { return yearRate; } public void setYearRate(double yearRate) { this.yearRate = yearRate; } //存款 public void deposit(double d){ balance+=d; System.out.println("存款成功"); System.out.println("余额"+balance); } public void withdraw(double d){ if(this.balance<d){ System.out.println("余额不足,取款失败"); }else{ this.balance-=d; System.out.println("取款成功"); System.out.println("余额"+balance); } } }
package Cun; public class Check extends Account{ double overdraft; public Check(int id, double balance, double yearRate,double overdraft) { super(id, balance, yearRate); this.overdraft=overdraft; } @Override public void withdraw(double d) { if(super.getBalance()>=d){ super.withdraw(d); }else { if(d<=super.getBalance()+this.overdraft){ this.overdraft=this.overdraft-(d-getBalance()); super.setBalance(0); System.out.println("取款成功:"); System.out.println("余额:"+super.getBalance()); System.out.println("可透支额度:"+this.overdraft); } else{ System.out.println("余额不足"); } } } }
package Cun; public class Test { public static void main(String[] args) { Check c=new Check(122, 10000,4.65, 5000); c.withdraw(5000); c.withdraw(6000); } }