/*Account类:表示银行账户*/
//创建包:
//package com.mybank.domain;
//定义类的时候,要注意访问限制符的使用.
public class Account
{
protected double balance;//表示余额
protected Account(double initBalance)//构造器,来初始化成员变量.
{
this.balance=initBalance; //开户.
}
public double getBalance() //获取余额.
{
//想获得balance的值,需要使用
return balance;
}
public boolean deposit(double amt) //存款,amt:存款金额
{
balance=balance+amt;
return true;
}
public boolean withdraw(double amt) //取款amt:取款金额
//增加业务规则:取款金额和余额. 保证余额〉=取款金额.
{
boolean result=false; //假设操作失败.
if(balance>=amt)
{
balance=balance-amt;
result=true; //操作成功.
}
return result;
}
}/*类定义完成.*/
/**
建立Bank类:
属性:
customers:Customer[]
numberOfCustomers:int
构造器:
Bank
方法:
addCustomer(x:String,m:String)
getNumOfCustomers():int
getCustomer(index:int):Customer
达到以下输出效果
Customer[1]is 李逵
Customer[2]is 宋江
Customer[3]is 燕青
customer[4]is 鲁智深
*/
class Bank
{
private Customer[]customers;//Customer类型的对象数组
private int numberOfCustomers;//记录下一个customers数组索引的一个整数.
public Bank(){
//代码为空.至少长度为5来初始化customers数组
customers=new Customer[5];
numberOfCustomers=0;
}
public void addCustomer(String x,String m)//参数为姓x、名m
{
customers[numberOfCustomers]=new Customer(x,m);//构造一个新的Customer对象
numberOfCustomers++; //指向下一个编号.
}
public int getNumOfCustomers(){ //获得客户数量.
return numberOfCustomers;//返回
}
public Customer getCustomer(int index){ //实现的功能是啥呢?通过索引来检索客户的信息
return customers[index]; //通过customers数组来返回一个Customer对象
}
}
/*定义银行客户类*/
//创建包
//package com.mybank.domain;
public class Customer
{
private String xing;//姓
private String ming;//名
private Account account;//定义Account类型的一个成员变量,表示客户有一个账户
//构造器
public Customer(String x,String m)
{
xing=x;
ming=m;
}
//方法
public String getXing() //获得姓
{
return xing;
}
public String getMing()//获得名
{
return ming;
}
public Account getAccount() //获得客户的账户方法
{
return account; //返回账户
}
public void setAccount(Account acct) //设置客户的账户.
{
this.account=acct;
}
}
/*
SavingAccount:储蓄账户类.
是账户类的子类.
*/
class SavingAccount extends Account
{
//属性.
private double interestRate; //利率.
//构造器.对成员变量初始化。
public SavingAccount(double initBalance,double interestRate)
{
super(initBalance); //调用父类的构造器. 把父类的构造器给包含过来.
this.interestRate=interestRate;
}
}
/*
透支账户类
透支账户类是账户类的子类.
*/
class CheckingAccount extends Account
{
//属性
private double overdraftAmount; //透支金额
//构造器.成员变量初始化.
public CheckingAccount(double initBalance,double overdraft)
{
super(initBalance);
this.overdraftAmount=overdraft;
}
public CheckingAccount(double initBalance)
{
super(initBalance);
}
//方法.重写取钱方法,因为透支账户取钱可以透支.
public boolean withdraw(double amt)
{
return false;
}
}
class TestBanking
{
public static void main(String[] args)
{
Bank lsBank=new Bank();
lsBank.addCustomer("李","逵");//customers[0]=李逵
lsBank.addCustomer("宋","江");//customers[1]=宋江
lsBank.addCustomer("燕","青");
lsBank.addCustomer("鲁","智深");
//System.out.println("长度"+lsBank.getNumOfCustomers());
//Customer objCust=new Customer();
for(int i=0;i