题目:*(封装、继承)设计如下的继承树:
Accout 表示银行账户,id 属性表示账户id,balance 表示账户余额,password 表示账户密码;
SavingAccount 表示储蓄账户,interestRate 表示存款利率;
CreditAccount 表示信用账户,creditLine 表示信用额度。
完成下列任务:
1) 所有属性都应设为私有,根据需要增加构造方法和get/set 方法。
2) *修改setPassword 方法,要求:
setPassword 判断新密码长度是否是6 位,如果不是则不予修改;
修改getPassword 方法,要求每次都返回null 值。
3) *修改interestRate 的set 方法,要求利率大于0 并小于10%。
10. **(综合)在第18 题的基础上,创建一个Bank 类,其中包括三个方法:开户、存款、取款
a) 开户:
Account openAccount(long id, String password, int type)
其中,id 表示账户id,password 表示账户密码,type 表示账户类型。如果type 为0则创建一个Account 账户,如果type 为1 则创建一个储蓄账户SavingAccount,如果type为2 则创建一个信用账户CreditAccount。返回值为开户时创建的Account 对象
b) 存款
double deposit(Account a, double amount)
其中,a 表示存入账号,amount 表示存入的金额。返回值表示存款之后的余额
c) 取款
double withdraw(Account a, double amount)
其中,a 表示取款账号,amount 表示取出的金额,返回值表示取款之后的余额。特别的,除非Account 类型是CreditAccount,否则不允许透支。
答案如下:
package day4; public class Account {
private long id;
private double balance;
private String password;
public Account() {
super();
// TODO Auto-generated constructor stub
}
public Account(long id, double balance, String password) {
super();
this.id = id;
this.balance = balance;
setPassword(password);
}
public long getId() {
System.out.println("请看我是id");
return id;
}
public void setId(long id) {
this.id = id;
}
public double getBalance() {
System.out.println("哈哈~~~余额奥");
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword() {
System.out.println("密码怎么能随意给你呢~~~你想太多了!");
return null;
}
public void setPassword(String password) {
if(password.length()==6){
this.password = password;
}else{
System.out.println("您输入的密码不是6位,请您重新输入!");
} } } package day4; public class Bank {
Account account=null;
//开户
/**
*
* @param id :账户
* @param password:账户密码
* @param type:账户类型
* @return
*/
public Account openAccount(long id, String password, int type){ if(type==0){
account = new Account(id,0,password);
}else if(type==1){
account = new SavingAccount(id,0,password,0.09);
}else if(type==2){
new CreditAccount(id, 0, password, 10000);
}else{
System.out.println("没有您需要的类型!");
}
return account;
}
//存款
public double deposit(Account a, double amount){
a.setBalance(amount+a.getBalance());
return a.getBalance();
}
//取款
public double withdraw(Account a, double amount){
if(a.getBalance()<amount && !(a instanceof CreditAccount) ){
System.out.println("您的余额不足,穷了吧~~~");
}
a.setBalance(a.getBalance()-amount);
return a.getBalance();
}
} package day4; public class CreditAccount extends Account{
private double creditLine; public CreditAccount() {
super();
// TODO Auto-generated constructor stub
} public CreditAccount(long id, double balance, String password,double creditLine) {
super(id, balance, password);
this.creditLine=creditLine;
// TODO Auto-generated constructor stub
} public double getCreditLine() {
return creditLine;
} public void setCreditLine(double creditLine) {
this.creditLine = creditLine;
} } package day4;
/**
*
* @author Administrator
* 储蓄账户
*/
public class SavingAccount extends Account{
private double interestRate; public SavingAccount() {
super();
} public SavingAccount(long id, double balance, String password,double interestRate) {
super(id, balance, password);
this.interestRate=interestRate;
} public double getInterestRate() {
return interestRate;
} public void setInterestRate(double interestRate) {
if(interestRate>0&&interestRate<0.1){
this.interestRate = interestRate;
}else{
System.out.println("利率给的不对~~~");
}
} } package day4; import java.util.Scanner; public class TestBank {
public static void main(String[] args) {
Bank bank = new Bank();
//开户
Scanner s = new Scanner(System.in);
System.out.println("请您输入账户名:");
int id=s.nextInt();
System.out.println("请您输入一个六位数字的密码");
String password=s.next();
System.out.println("请你输入开户类型:");
int type=s.nextInt();
Account account = bank.openAccount(id, password, type);
System.out.println(account.getId() +account.getPassword()+account.getBalance());
}
}