UML类图及内存分析可在公众号查看:
建立银行,用户,账户三个类间的联系,实现存取:
package bank5;
//账户类
public class Account {
protected double balance;
public Account(double init_balance) {//带参构造器
balance = init_balance;
}
public double getBalance() {
return balance;
}
//存钱
public double deposit(double amt) {
balance += amt;
return balance;
}
//取钱
public boolean withdraw(double amt) {
if(amt<balance) {
balance-=amt;
return true;
}
else {
System.out.println("余额不足" + (amt-balance));
return false;
}
}
}
//储蓄卡类
package bank5;
public class SavingAccount extends Account{
private double interestRate;
public SavingAccount(double balance,double init_Rate){
super(balance);
this.interestRate = init_Rate;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate() {
this.interestRate = interestRate;
}
}
//信用卡类
package bank5;
public class CheckingAccount extends Account {
private double overdraftProtection;//透支额度
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance,double protect) {
super(balance);
this.overdraftProtection = protect;
}
public double getOverdraftProtection() {
return overdraftProtection;
}
public void setOverdraftProtection() {
this.overdraftProtection = overdraftProtection;
}
//重写取钱方法
public boolean withdraw(double amt) {
if(balance>=amt) {
balance -=amt;
return true;
}else if(overdraftProtection>=(amt-balance)) {
overdraftProtection -= (amt-balance);
balance = 0;
return true;
}else {
return false;
}
}
}
//用户类
package bank5;
public class Customer {
private String firstName;
private String lastName;
//private Account account;//将Account类定义为此类的成员变量
private Account[] accounts;
int numberOfAccounts;
public Customer(String f, String l) {
firstName = f;
lastName = l;
accounts = new Account[5];
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/*public Account getAccount() {
return account;
}
public void setAccount(Account acct) {
account = acct;
}
*/
public void addAccount(Account acct) {
accounts[numberOfAccounts] = acct;
numberOfAccounts ++;
}
public int getnumberOfAccounts() {
return numberOfAccounts;
}
//返回指定账户
public Account getAccount(int index) {
return accounts[index];
}
}
//银行类
package bank5;
public class bank {
private Customer[] customers;//采用数组存放银行用户
private int numberOfCustomers;
public bank() {
customers = new Customer[5];//初始化bank对象
}
public void addCustomer(String f,String l) {//添加用户
Customer cust = new Customer(f,l);
customers[numberOfCustomers] = cust;
numberOfCustomers ++;
}
public int getNumOfCustomers() {//获取用户数量
return numberOfCustomers;
}
public Customer getCustomer(int index) {//返回索引用户
return customers[index];
}
}
//测试类
package testbank;
import bank5.Account;
import bank5.CheckingAccount;
import bank5.Customer;
import bank5.SavingAccount;
import bank5.bank;
public class testbank5 {
public static void main(String[] args) {
bank ban = new bank();
Customer customer;
//添加用户
ban.addCustomer("jane", "smith");
customer = ban.getCustomer(0);
customer.addAccount(new SavingAccount(500.00,0.05));
customer.addAccount(new CheckingAccount(400.00,200.00));
ban.addCustomer("oven", "karry");
customer = ban.getCustomer(1);
customer.addAccount(new CheckingAccount(500.00));
ban.addCustomer("bob", "kaly");
customer = ban.getCustomer(2);
customer.addAccount(new SavingAccount(1500.00,0.05));
customer.addAccount(new CheckingAccount(400.00));
ban.addCustomer("bob", "july");
customer = ban.getCustomer(3);
customer.addAccount(new SavingAccount(500.00,0.05));
customer.addAccount(new SavingAccount(1500.00,0.05));
//打印测试输出
System.out.println("-----------");
for(int cust_idx = 0; cust_idx < ban.getNumOfCustomers();
cust_idx ++) {
customer = ban.getCustomer(cust_idx);
System.out.println();
System.out.println(customer.getFirstName() + " " +
customer.getLastName());
for(int acct_idx = 0;acct_idx < customer.getnumberOfAccounts();
acct_idx ++) {
Account account = customer.getAccount(acct_idx);
//判断账户类型
String account_type = "";
if(account instanceof SavingAccount) {
account_type = "SavingAccount";
}
if(account instanceof CheckingAccount) {
account_type = "CheckingAccount";
}
//输出
System.out.println(account_type + ":¥" +
account.getBalance());
}
}
}
}