银行账户存取项目(二)

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());
				
			}
		}
		
	}

}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值