一、写一个账户类(Account),包含属性:
id:账户号码 长整数
password:账户密码
name:真实姓名
personId:身份证号码 字符串类型
email:客户的电子邮箱
balance:账户余额
包含方法:
deposit: 存款方法,参数是double型的金额
withdraw:取款方法,参数是double型的金额
包含构造方法:
有参和无参,有参构造方法用于设置必要的属性
二、银行的客户分为两类:储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度。
注意:CreditAccount需要多一个属性 ceiling 透支额度
为这两种用户编写相关的类。
三、同时要求编写Bank类,属性:
1.当前所有的账户对象的集合,存放在数组中(数组的大小自己定义)
2.当前账户数量
四、写个主方法测试你写的类
五、继续改进你的代码
1.修改Account类,银行用户的账号(id)是自动生成的,初始值为100000,第一个开户的用户id为100001,第二个为100002,依此类推.
提示:构造对象的时候采用static属性为id赋值
2.对于Account类,有两个方法,存款方法和取款方法,请修改这两个方法.
存款方法改为不允许子类修改
取款方法根据不同的子类而不同,因此,改为抽象方法,在两个子类中分别实现
3.将Bank类作成单例
单例类代码:
public class single {
private ststic single s ;
private single(){
}
static single getInstance(){
if (s ==null){
s = new single();
}
return single;
}
}
为SavingAccount和CreditAccount各自添加一个子类
LoanSavingAccount类:用户可以贷款,不可以透支
LoanCreditAccount类:用户可以贷款,可以透支
说明:贷款和透支是不一样的,透支指的是账户余额小于0,而贷款用户需要一个贷款额的属性.
在ATM机上,用户可以选择贷款,也可以选择还贷款,而还贷款就是要把账户余额上的资金转到贷款额上
例如:用户余额10000元,贷款额100000元,用户可以选择还款5000元,则用户余额变为5000,贷款额变为95000元.
利用接口来抽象出LoanSavingAccount类和LoanCreditAccount类的共性
接口中的方法:
requestLoan:贷款
payLoan:还贷
getLoan:获取用户贷款总额
为Bank类添加三个方法
贷款:参数 id,贷款额,返回修改过的Account对象
还贷款:参数 id,还款额,返回修改过的Account对象
统计所有账户贷款的总数
Account父类
package igeekhome;
//银行卡父类
public abstract class Account {
private static long currentID = 100000;//记录当前编号
//属性
private long id;//账户号码
private String password;//账户密码
private String name;//真实姓名
private String personId;//身份证号
private String email;//
private double balance=0.0;
//get/set
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public static long getCurrentID() {
return currentID;
}
public static void setCurrentID(long currentID) {
Account.currentID = currentID;
}
//构造函数
public Account(){
this.id = ++currentID;
}
public Account(String password,String name,String personId,String email){
this.id = ++currentID;
this.password=password;
this.name=name;
this.personId=personId;
this.email=email;
}
//取款方法(抽象)
public abstract void withdraw(double money);
//存款(不允许子类修改,所以用final进行修饰)
public final void deposit( double money){
this.balance += money;
}
}
储蓄类
package igeekhome;
/*
* 储蓄账户子类
*/
public class SavingAccount extends Account{
//构造函数
public SavingAccount(){
}
public SavingAccount(String password,String name,String personId,String email){
super(password,name,personId,email);
}
//储蓄账户取款
public void withdraw(double money) {
if (super.getBalance()>=money) {
super.setBalance(super.getBalance()-money);
}else{
System.out.print("账户余额不足!");
}
System.out.println("余额为:"+super.getBalance());
}
}
信用类
package igeekhome;
//信用卡子类
public class CreditAccount extends Account {
private double ceiling;// 透支额度
// get/set
public double getCeiling() {
return ceiling;
}
public void setCeiling(double ceiling) {
this.ceiling = ceiling;
}
// 构造函数
public CreditAccount() {}
public CreditAccount(String password, String name, String personId, String email, double ceiling) {
super(password, name, personId, email);
this.ceiling = ceiling;
}
// 信用账户取款
public void withdraw(double money) {
/*
* 取款的金额<=帐户余额+可透支额度
*
* 假设:
* 余额:5000
* 可透支额度:10000
* 支取的金额:3000
*
* 剩余 :
* 余额:2000
* 可透支额度:10000
* 支取金额:5000
*
* 剩余 :
* 余额:-3000
* 可透支额度:10000
*
*/
//此条件满足可以取款
if (ceiling + super.getBalance() - money >= 0) {
this.setBalance(this.getBalance()-money);
}else{
System.out.println("您的余额不足,并且可透支额度已经用完,取款失败");
}
}
}
两个可以贷款的银行卡子类
package igeekhome;
//普通银行卡号的子类(可以贷款)
public class LoanSavingAccount extends SavingAccount implements ILoan {
private double loanAmount = 0;// 贷款额度
// 构造函数
public LoanSavingAccount() {
}
public LoanSavingAccount(String password, String name, String personId, String email) {
super(password, name, personId, email);
}
// 实现接口中的方法
// 贷款
public void requestLoan(double money) {
// TODO Auto-generated method stub
this.loanAmount += money;// 将要贷款的金额加入用户贷款的总额度
System.out.println("您当前的贷款金额是:" + this.loanAmount);
}
// 还贷 。假设还了500
public void payLoan(double money) {
//如果当前的余额大于还款金额 ,允许进行相关操作
if (this.getBalance() >= money) {
// 把账户余额上的资金转到贷款额上
double m = this.getBalance();// 得到当前帐户余额
this.setBalance(m - money);// 修改帐户余额 ,从帐户余额上-500
this.loanAmount -= money;// 修改贷款的额度 ,用户贷款的金额上-500
System.out.println("您的当前余额是:" + this.getBalance());
System.out.println("您的贷款金额是:" + this.loanAmount);
}else{
System.out.println("您的余额不足,还款失败!");
}
}
// 获取用户贷款总额
public double getLoan() {
// TODO Auto-generated method stub
return this.loanAmount;
}
}
package igeekhome;
//信用卡类的子类(可以透支,可以贷款)
public class LoanCreditAccount extends CreditAccount implements ILoan {
private double loanceiling = 0;// 贷款额度
// 构造函数
public LoanCreditAccount() {
}
public LoanCreditAccount(String password, String name, String personId, String email, double ceiling) {
super(password, name, personId, email, ceiling);
}
// set/get
public double getLoanceiling() {
return loanceiling;
}
public void setLoanceiling(double loanceiling) {
this.loanceiling += loanceiling;
}
// 实现接口中的方法
// 贷款
public void requestLoan(double money) {
this.loanceiling += money;// 将要贷款的金额加入用户贷款的总额度
System.out.println("您当前的贷款金额是:" + this.loanceiling);
}
// 还贷款
public void payLoan(double money) {
// TODO Auto-generated method stub
if (this.getBalance() >= money) {
// 把账户余额上的资金转到贷款额上
double m = this.getBalance();// 得到当前帐户余额
this.setBalance(m - money);// 修改帐户余额 ,从帐户余额上-500
this.loanceiling -= money;// 修改贷款的额度 ,用户贷款的金额上-500
System.out.println("您的当前余额是:" + this.getBalance());
System.out.println("您的贷款金额是:" + this.loanceiling);
}else{
System.out.println("您的余额不足,还款失败!");
}
}
// 得到用户的贷款金额
public double getLoan() {
// TODO Auto-generated method stub
return this.loanceiling;
}
}
接口类
package igeekhome;
//接口
public interface ILoan {
void requestLoan(double money);//贷款
void payLoan(double money);//还贷
double getLoan();//获取用户贷款总额,因此此方法有返回值
}
Bank类
package igeekhome;
/**
* 银行类
*
* @author cp
*
*/
public class Bank {
// 帐户数组
private Account[] accountArray = new Account[5];
// 当前的帐户的数量(数组下标)
private static int count = 0;
public Account[] getAccountArray() {
return accountArray;
}
public void setAccountArray(Account[] accountArray) {
this.accountArray = accountArray;
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
Bank.count = count;
}
/**
* 向银行添加银行卡
*/
public boolean addAccount(Account a) {
if(count
随便测试下
package igeekhome;
public class Test {
public static void main(String[] args) {
//创建银行对象
Bank bank = Bank.getInstance();
//开户
SavingAccount a1 = new SavingAccount("111111", "zs1", "1111", "[email protected]");
CreditAccount a2 = new CreditAccount("111111", "zs2", "1111", "[email protected]",10000);
LoanSavingAccount a3 = new LoanSavingAccount("111111", "zs3", "1111", "[email protected]");
LoanCreditAccount a4 = new LoanCreditAccount("111111", "zs4", "1111", "[email protected]",5000);
LoanSavingAccount a5 = new LoanSavingAccount("111111", "zs5", "1111", "[email protected]");
//向银行添加银行卡帐户
bank.addAccount(a1);
bank.addAccount(a2);
bank.addAccount(a3);
bank.addAccount(a4);
bank.addAccount(a5);
//模拟操作
a1.deposit(5000);
a2.deposit(2000);
a3.deposit(1500);
a4.deposit(6000);
a5.deposit(500);
a5.withdraw(5000);
a3.requestLoan(50000);
}
}