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

📄 banktransaction.java

📁 一个模拟银行间存款
💻 JAVA
字号:
package banking;

import java.util.*;

/**
 * 银行业务处理类
 * 处理功能:1.创建帐户;2.注销帐号;3.存款;4.取款;5.转帐;6.打印所有帐号信息
 * 进行各项处理失败时,将错误信息存入message
 * @author rainliu
 */
public class BankTransaction {
	/** 所有帐户信息的列表 */
	private static List accountList = new ArrayList();
	/** 操作发生错误时的提示信息 */
	private String message = "";

	public BankTransaction() {}

	/** 信息载入 */
	static {
		FileRW reader =  new FileRW();
		//FileAccesser reader =  new FileAccesser();
		//FileProperties reader = new FileProperties();
		//IDataFile reader =  new FileRW();
		//ADataFile reader =  new FileRW();
		accountList = reader.readAccountInfo();
		if (accountList==null) {
			accountList = new ArrayList();
		}
	}

	/**
	 * 创建一个新的帐号
	 * @param newAccount  新的帐户ID
	 * @param password    输入的密码
	 * @return true 创建成功
	 */	
	public boolean createAccount(String newAccount,String password) {
		//检查要创建的帐号是否已经存在
		for (int i=0;i<accountList.size();i++) {
			AccountInfo ai = (AccountInfo)accountList.get(i);
			//目前帐户信息为空
			if (ai==null) break;
			String aid = ai.accountId;
			if (newAccount.equals(aid)) {
				setMessage("该帐号已经存在!");
				return false;
			}
		}
		//创建一个新的帐号
		AccountInfo acc = new AccountInfo();
		acc.accountId = newAccount;
		acc.password = password;
		accountList.add(acc);
		//将最新信息更新到文件
		saveData();
		return true;
	}

	/**
	 * 创建一个新的帐号
	 * @param accountId 待删除的帐户ID
	 * @param password  待删除帐户的密码
	 * @return true 删除成功
	 */	
	public boolean deleteAccount(String accountId,String password) {
		//身份验证
		if (!checkAccount(accountId, password)) {
			return false;
		}
		for (int i=0;i<accountList.size();i++) {
			AccountInfo ai = (AccountInfo)accountList.get(i);
			if (accountId.equals(ai.accountId)) {
				accountList.remove(i);
			}
		}
		//将最新信息更新到文件
		saveData();
		return true;
	}

	/**
	 * 存款操作的处理
	 * @param accountid 要存款的帐户ID
	 * @param password  密码
	 * @param money     操作的金额数
	 * @return true 操作成功
	 */
	public boolean deposit(String accountId, String password, double money) {
		//身份验证
		if (!checkAccount(accountId, password)) {
			return false;
		}
		//金额操作
		for (int i = 0; i < accountList.size(); i++) {
			//待修改的帐户
			AccountInfo ai = (AccountInfo) accountList.get(i);
			String aa = ai.accountId;
			//检索该帐户
			if (ai.accountId.equals(accountId)
				&& ai.password.equals(password)) {
				//增加金额
				ai.balance += money;
				//将最新信息更新到文件
				saveData();
				return true;
			}
			
		}
		return false;
	}

	/**
	 * 取款操作的处理
	 * @param accountid 要取款的帐户ID
	 * @param password  密码
	 * @param money     操作的金额数
	 * @return true 操作成功
	 */
	public boolean withdraw(String accountId, String password, double money) {
		//身份验证
		if (!checkAccount(accountId, password)) {
			return false;
		}
		//金额操作
		for (int i = 0; i < accountList.size(); i++) {
			//待修改的帐户
			AccountInfo ai = (AccountInfo) accountList.get(i);
			String aa = ai.accountId;
			//检索该帐户
			if (ai.accountId.equals(accountId)
				&& ai.password.equals(password)) {
				if (ai.balance < money) {
					setMessage("对不起,您的余额不足!");
					return false;
				} else {
					ai.balance -= money;
					//将最新信息更新到文件
					saveData();
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 转帐操作的处理
	 * @param fromId    要转出的帐户ID
	 * @param fromPass  要转出的帐户密码
	 * @param toId      要转入的帐户ID
	 * @param toPass    要转入的帐户密码
	 * @param money     操作的金额数
	 * @return true 操作成功
	 */
	public boolean transfer(String fromId,String fromPass,String toId,String toPass,double money) {
		//身份验证
		if (!checkAccount(fromId,fromPass) || !checkAccount(toId,toPass)) {
			return false;
		}

		//先从帐户fromId取款
		if (withdraw(fromId,fromPass,money)) {
			//在存入帐户toId
			deposit(toId,toPass,money);
			//将最新信息更新到文件
			saveData();
			return true;
		}
		return false;
	}


	/**
	 * 执行操作前,检验帐户的信息是否合法
	 * @param accountId 帐户ID
	 * @param password  密码
	 * @return true 若用户名和密码均存在且对应正确
	 */
	private boolean checkAccount(String accountId,String password) {
		//检查要创建的帐号是否已经存在并合法
		for (int i=0;i<accountList.size();i++) {
			AccountInfo ai = (AccountInfo)accountList.get(i);
			String aa = ai.accountId;
			if (accountId.equals(aa) && ai.password.equals(password)) {
				return true;
			}
		}
		setMessage("帐户不存在或密码有误!");
		return false;
	}

	/**
	 * 将最新帐户信息保存到数据文件中
	 */
	private void saveData() {
		//将最新信息更新到文件
		FileRW writer = new FileRW();
		//FileAccesser writer = new FileAccesser();
		//FileProperties writer = new FileProperties();
		//IDataFile writer = new FileRW();
		//ADataFile writer = new FileRW();
		writer.writeAccountInfo(accountList);
	}

	/**
	 * 打印出所有的帐号和余额
	 */
	public void printAllAcount() {
		System.out.println("--------------------------------");
		System.out.println("accountId\t\tbalance");
		System.out.println("--------------------------------");
		for (int i=0;i<accountList.size();i++) {
			AccountInfo ai = (AccountInfo)accountList.get(i);
			System.out.println(ai.accountId + "\t\t" + ai.balance);
		}
		System.out.println("--------------------------------");
	}


	public void setMessage(String msg) {
		this.message = msg;
	}
	public String getMessage() {
		return message;
	}
	
}

⌨️ 快捷键说明

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