银行的客户分为两类,储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度.
注意:CreditAccount需要多一个属性 ceiling 透支额度
为这两种用户编写相关的类
同时要求编写Bank类,属性:
1.当前所有的账户对象的集合,存放在数组中
2.当前账户数量
方法:
1.用户开户,需要的参数:id,密码,姓名,身份证号码,邮箱,账户类型,返回新创建的Account对象
2.用户登录,参数:id,密码 返回Account对象
3.用户存款,参数:id,存款数额,返回修改过的Account对象
4.用户取款,参数:id,取款数额,返回修改过的Account对象
5.设置透支额度 参数:id,新的额度 ,返回修改过的Account对象.这个方法需要验证账户是否是信用账户
用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数
另外,请为Bank类添加几个统计方法
1.统计银行所有账户余额总数
2.统计所有信用账户透支额度总数
写个主方法测试你写的类
个人分析:
1储蓄账户(SavingAccount)和信用账户(CreditAccount)需要写两个类并且继承Account类,
2且由于账户的性质不同所以两个账户的取款方法不同,但存款方法相同,所以可以在父类中的取款方法定义为一个抽象类,
3Bank类的创建是为了业务需要,一个Bank类里面应该有Account属性,作为组合模式。在Bank类的方法中的信用账户透支额度总数实现起来需要进行一下类型分析,然后在进行相应的操作
4在创建Account对象的时候用的是数组,考虑到数组满的问题,写了一个expand()方法,此方法实现数组容量扩大一倍。
package Business;

import Account.Account;
import Account.CreditAccount;
import Account.SavingAccount;


public class Bank {
private Account[] as =new Account[100];
private int size;

//用户注册
public Account Register(String pass1,String pass2,String name,String personId,String email,int type){
if(!(pass1.equals(pass2))){
System.out.print("你输入的密码错误");
return null;
}
Account c=null;
if(type==0) c=new SavingAccount(pass1, name, personId, email);
else if(type==1) c=new CreditAccount(pass1,name,personId,email);
else return null;
//判断数组是否已经满了
if (size==as.length) expand();
as[size]=c;
size++;
return c;
}
//增大数组容量的方法
private void expand() {
Account[] a=new Account[as.length*2];
System.arraycopy(as,0,a,0,as.length);
as=a;
}
//用户登陆
public Account Login(long id,String pass1){
Account c =getAccountById(id);
if(c==null){
System.out.print("此账户不存在");
return null;
}
else if(!(pass1.equals(pass1)))
return null;
else System.out.println("欢迎您"+c.getName());
return c;
}
//通过id得到Account账户
private Account getAccountById(long id) {
for(int i=0;i<size;i++){
if(as[i].getId()==id) return as[i];
}
return null;
}
//设置信用额度
private Account setCeiling(long id,double ceiling){
Account c=getAccountById(id);
if(c instanceof CreditAccount){
CreditAccount ca=(CreditAccount)c;
ca.setCeiling(ceiling);
}
return c;
}
//存款方法
public Account depoist(long id,double money){
Account c=this.getAccountById(id);
c.deposit(money);
return c;
}
//取款方法
public Account withdraw(long id,double money){
Account c=this.getAccountById(id);
c.withdraw(money);
return c;
}


//统计所有账户余额总和
public double getAllBalance(){
double d=0;
for(int i=0;i<size;i++){
d=d+as[i].getBalance();
}
return d;
}
//统计所有信用账户透支额度总数
public double getAllCeilingBalance(){
double d=0;
for(int i=0;i<size;i++){
if(as[i] instanceof CreditAccount){
CreditAccount ca=(CreditAccount)as[i];
d=d+ca.getBalance();
}
}
return d;
}


public static void main(String[] args){
Bank b=new Bank();
b.Register( "123", "123", "jj", "123", "jj@163.com", 1);
b.Register( "456", "456", "ff", "456", "ff@163.com", 0);
b.setCeiling(10001L,100000);
b.depoist(10001L, 1000);
System.out.println(b.getAllBalance());
System.out.println(b.getAllCeilingBalance());
}
}

package Account;

public class CreditAccount extends Account{
private double ceiling;

public double getCeiling() {
return ceiling;
}

public void setCeiling(double ceiling) {
this.ceiling = ceiling;
}

public CreditAccount() {
super();
}

public CreditAccount(String password, String name, String personId, String email) {
super(password, name, personId, email);
}
public void withdraw(double money){
if(this.getBalance()-money>=(-1)*this.getCeiling())
this.setBalance(this.getBalance()-money);
}

}
package Account;

public class SavingAccount extends Account {

public SavingAccount() {
super();
}

public SavingAccount(String password, String name, String personId, String email) {
super(password, name, personId, email);
}
public void withdraw(double money){
if(this.getBalance()-money>=0)
this.setBalance(this.getBalance()-money);
}
}
注意:CreditAccount需要多一个属性 ceiling 透支额度
为这两种用户编写相关的类
同时要求编写Bank类,属性:
1.当前所有的账户对象的集合,存放在数组中
2.当前账户数量
方法:
1.用户开户,需要的参数:id,密码,姓名,身份证号码,邮箱,账户类型,返回新创建的Account对象
2.用户登录,参数:id,密码 返回Account对象
3.用户存款,参数:id,存款数额,返回修改过的Account对象
4.用户取款,参数:id,取款数额,返回修改过的Account对象
5.设置透支额度 参数:id,新的额度 ,返回修改过的Account对象.这个方法需要验证账户是否是信用账户
用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数
另外,请为Bank类添加几个统计方法
1.统计银行所有账户余额总数
2.统计所有信用账户透支额度总数
写个主方法测试你写的类
个人分析:
1储蓄账户(SavingAccount)和信用账户(CreditAccount)需要写两个类并且继承Account类,
2且由于账户的性质不同所以两个账户的取款方法不同,但存款方法相同,所以可以在父类中的取款方法定义为一个抽象类,
3Bank类的创建是为了业务需要,一个Bank类里面应该有Account属性,作为组合模式。在Bank类的方法中的信用账户透支额度总数实现起来需要进行一下类型分析,然后在进行相应的操作
4在创建Account对象的时候用的是数组,考虑到数组满的问题,写了一个expand()方法,此方法实现数组容量扩大一倍。








private Account[] as =new Account[100];
















































































































































