⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bank.java

📁 银行账户管理系统 简称BAM(项目介绍及源码)
💻 JAVA
字号:
package service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import readWriteAccount.GetFileName;
import readWriteAccount.WriteAccountToFile;
import service.Bank;
import exception.*;

import model.*;

public class Bank {
	private WriteAccountToFile write = null;
	private HashMap<Long,Account> accountList;// 存放 账户对象的集合

	// 以下几行代码就将Bank类,设成单例模式.
	private static Bank bank = null;
	private Bank() {
		FindFile find = new FindFile();// GetFileName.fileName()返回保存账户信息的文件路径
		write = new WriteAccountToFile(find.getAccountFileName());// 将账户信息写出到文件fileName中
		accountList = write.readAccount();// 从文件读出所有银行账户信息给集合accoulList

		// System.out.println("accountList长度:"+accountList.size());
	}

	public static Bank getInstance() {
		if (bank == null)
			bank = new Bank();
		return bank;
	}

	/**
	 * 1.用户开户 需要的参数:id,密码,密码确认,姓名,身份证号码,邮箱,账户类型(int),返回新创建的Account对象
	 */
	public Account registerAccount(String password, String password1,
			String name, String personId, String email, double balance, int type)
			throws RegisterException {
		if (!password.equals(password1)) {
			throw new RegisterException("密码不一致,开户失败!");
		}
		Account account = null;
		if (password.equals(password1)) {

			switch (type) {
			case 1: {
				// 储蓄账户类(SavingAccount)--开户成功
				account = new SavingAccount(password, name, personId, email,
						balance);
				break;
			}
			case 2: {
				// 信用账户类(CreditAccount) --开户成功
				account = new CreditAccount(password, name, personId, email,
						balance);
				break;
			}
			case 3: {
				// 贷款储蓄账户类(LoadSavingAccount)--开户成功
				account = new LoanSavingAccount(password, name, personId,
						email, balance);
				break;
			}
			case 4: {
				// 贷款信用账户(LoadCreditAccount) --开户成功
				account = new LoanCreditAccount(password, name, personId,
						email, balance);
				break;
			}
			}
		}
		accountList.put(new Long(account.getId()), account);// 将新开的账户存入到银行集合中
		write.writeAccount(accountList);// 写入文件
//		System.out.println("==============调用show()方法====================");
//		showAccountList() ;
		return account;
	}

	/**
	 * 2.用户登录 参数:id,密码 返回Account对象,提示 用s1.equals(s2)判断s1和s2两个字符串内容是否相等
	 * 
	 */
	public Account loginAccount(long id, String password) throws LoginException {
		if (!accountList.containsKey(new Long(id))) {
			throw new LoginException("id不存在!");
		}
		Account account = (Account) accountList.get(new Long(id));
		if (!(password.equals(account.getPassword()))) {
			throw new LoginException("密码错误!");
		}

		return account;
	}

	/**
	 * 3.用户存款 参数:id,存款数额,返回修改过的Account对象
	 */
	public Account depositAccount(long id, double money) {
		Account account = (Account) accountList.get(new Long(id));
		account.deposit(money);
		write.writeAccount(accountList);
		return account;
	}

	/**
	 * 4.用户取款 参数:id,存款数额,返回修改过的Account对象
	 * 
	 */
	public Account withdrawAccount(long id, double money)
			throws BussinessException {
		Account account = (Account) accountList.get(new Long(id));
		account.withdraw(money);
		write.writeAccount(accountList);// 写入文件
		return account;
	}

	/**
	 * 5.设置透支额度 参数:id,新的额度 ,返回修改过的Account对象.这个方法需要验证账户是否是信用账户
	 * 
	 */
	public Account setCeilingAccount(long id, double ceiling) {
		Account account = (Account) accountList.get(new Long(id));
		if (account instanceof CreditAccount) {
			CreditAccount ca = (CreditAccount) account;
			ca.setCeiling(ceiling);
		}
		write.writeAccount(accountList);// 写入文件
		return account;
	}

	/**
	 * 7.统计银行所有账户余额总数
	 * 
	 */
	// 利用多态,获得所有帐户余额总和
	public double getAllBalance() {
		double sum = 0;
		Collection cs = accountList.values();
		Iterator it = cs.iterator();
		while (it.hasNext()) {
			Account account = (Account) it.next();
			sum += account.getBalance();
		}
		return sum;
	}

	/**
	 * 7.统计所有信用账户透支额度总数
	 * 
	 * @return
	 */
	public double getAllCeiling() {
		double sum = 0;
		Collection cs = accountList.values();
		Iterator it = cs.iterator();
		while (it.hasNext()) {
			Account account = (Account) it.next();
			if (account instanceof CreditAccount) {
				CreditAccount ca = (CreditAccount) account;
				sum += ca.getCeiling();
			}
		}
		return sum;
	}

	/**
	 * 8.查找账户
	 * 
	 */
	public Account findAccount(long id) {

		Account account = (Account) accountList.get(new Long(id));

		return account;
	}

	/**
	 * 9.显示所有账户
	 */
	public void showAccountList() {
		Collection cs = accountList.values();
		Iterator it = cs.iterator();
		while (it.hasNext()) {
			Account account = (Account) it.next();

			if (account instanceof LoanCreditAccount) {// 贷款信用账户
				// LoanCreditAccount

				System.out.println("账号:" + account.getId() + "\t户名:"
						+ account.getName() + "\t是贷款信用账户-----》" + "\t货款:"
						+ ((LoanCreditAccount) account).getLoan() + "\t透支:"
						+ ((LoanCreditAccount) account).getCeiling()
						+ "\t账户余额:"
						+ ((LoanCreditAccount) account).getBalance());
			} else if (account instanceof LoanSavingAccount) {// 贷款储蓄账户
				// LoanSavingAccount

				System.out.println("账号:" + account.getId() + "\t户名:"
						+ account.getName() + "\t是贷款储蓄账户-----》" + "\t货款:"
						+ ((LoanSavingAccount) account).getLoan() + "\t账户余额:"
						+ ((LoanSavingAccount) account).getBalance());
			} else if (account instanceof SavingAccount) {// 储蓄账户
				// SavingAccount

				System.out.println("账号:" + account.getId() + "\t户名:"
						+ account.getName() + "\t是储蓄账户-----》" + "\t\t账户余额:"
						+ ((SavingAccount) account).getBalance());
			} else {
				// CreditAccount 信用账户

				System.out.println("账号:" + account.getId() + "\t户名:"
						+ account.getName() + "\t是信用账户-----》" + "\t\t透支:"
						+ ((CreditAccount) account).getCeiling()
						+ "\t账户余额:" + ((CreditAccount) account).getBalance());
			}

		}
	}

	/**
	 * 10.代款
	 * 
	 * @param id
	 * @param money
	 * @return
	 */
	public Account requetLoan(long id, double money) {
		Account account = (Account) accountList.get(new Long(id));
		if (account instanceof Loanable) {
			Loanable l = (Loanable) account;
			l.requestLoan(money);
			write.writeAccount(accountList);// 写入文件
		}
		return account;
	}

	/**
	 * 11.还款
	 * 
	 */
	public Account payLoan(long id, double money) throws LoanException,
			BalanceNotEnoughException {
		Account account = (Account) accountList.get(new Long(id));// 按ID查找用户
		if (account instanceof Loanable) {
			Loanable l = (Loanable) account;
			l.payLoan(money);
			write.writeAccount(accountList);// 写入文件
		}
		return account;
	}

	/**
	 * 12.统计所有账户贷款的总额
	 * 
	 * @return
	 */
	public double getAllLoan() {
		double m = 0;
		Collection cs = accountList.values();
		Iterator it = cs.iterator();
		while (it.hasNext()) {
			Account c = (Account) it.next();
			if (c instanceof Loanable) {
				Loanable ca = (Loanable) c;
				m += ca.getLoan();
			}
		}
		return m;
	}

	// get,set方法
	public HashMap<Long,Account>  getAccountList() {
		return accountList;
	}

	public void setAccountList(HashMap<Long,Account> accountList) {
		this.accountList = accountList;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -