粤嵌打卡第6天(GUI银行项目)

今天我们来做一个银行业务项目
这次我们也来使用mvc结构创建这个项目吧!

一、view层

1.1 创建主界面类 MainView.java


import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import model.Account;
import util.FileUtil;
import util.StringUtil;

/**
 * 显示界面类
 * @author LinChi
 *
 */
public class MainView {
	private FileUtil fileUtil = new FileUtil();
	private List<Account> list = new ArrayList<Account>();
	//记录登陆用户位置
	private int index = -1;
	//初始化
	public MainView() {
		list = fileUtil.readAccs();
	}
	/**
	 * 后台功能
	 */
	public void bankOper() {
		//设计结束标记
		boolean flag = true;
		while(flag) {
				String info = "=================== 柜台功能 ===============\n"+
							"1- 新建用户\n"+
							"2- 注销账户\n"+
							"3- 解锁账户\n"+
							"4- 冻结账户\n"+
							"5- 查看所有用户\n"+
							"6- 用户登陆\n"+
							"7- 退出";
			String input = JOptionPane.showInputDialog(info);
			if (StringUtil.isEmpty(input)) {
				JOptionPane.showMessageDialog(null, "请正确输入功能编号", "错误", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			if (!input.matches("[1-7]")) {
				JOptionPane.showMessageDialog(null, "请正确输入功能编号", "错误", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			switch (input.charAt(0)) {
			// 新建用户
			case '1':
				this.createNewAcc();
				break;
			// 注销账户
			case '2':
				deleteAcc();
				break;
			// 解冻账户
			case '3':
				jieLockAcc();
				break;
			// 冻结账户
			case '4':
				lockAcc();
				break;
			// 查看所有用户
			case '5':
				printAllAcc();
				break;
			// 用户登陆
			case '6':
				login();
				break;
			// 退出
			default:
				flag = false;
			}
		}
	}

	/**
	 * 账户登陆
	 */
	private void login() {
		String sccNo = JOptionPane.showInputDialog("请输入账号");
		// 判断输入是否正确
		if (StringUtil.isEmpty(sccNo)) {
			JOptionPane.showMessageDialog(null, "账号输入有误", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 正则判断账户有效性
		if (!sccNo.matches("\\d{4,10}")) {
			JOptionPane.showMessageDialog(null, "账号输入有误", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 获取账号
		int index = this.getIndexAccByNO(sccNo);
		if (index == -1) {
			JOptionPane.showMessageDialog(null, "账号输入有误", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 判断是否冻结账号
		if (!this.list.get(index).isFlag()) {
			JOptionPane.showMessageDialog(null, "账号已被冻结,请携带有关证件去柜台办理解冻手续。", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 输入密码
		for (int i = 0; i < 3; i++) {
			String accPwd = JOptionPane.showInputDialog("请输入登陆密码");
			if (this.list.get(index).getPassword().equals(accPwd)) {
				// 登陆成功
				break;
			} else if (i == 2) {
				// 第三次登陆密码失败,冻结此账户
				this.list.get(index).setFlag(false);
				// 整体覆盖保存
				this.fileUtil.saveAllAcc(list);
				JOptionPane.showMessageDialog(null, "密码错误3次,账户已被冻结!", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			} else {
				JOptionPane.showMessageDialog(null, "密码错误,还有" + (2 - i) + "次输入机会", "错误", JOptionPane.ERROR_MESSAGE);
			}
		}
		// 记录当前登陆账号
		this.index = index;
		// 用户操作
		accOper();
	}
	/**
	 * 用户操作
	 */
	private void accOper() {
//		JOptionPane.showMessageDialog(null, "用户操作");
		boolean flag = true;
		while(flag) {
			String info = "=================== 用户功能 ===============\n"+
					"1- 存款\n"+
					"2- 取款\n"+
					"3- 查询余额\n"+
					"4- 转账\n"+
					"5- 退出\n";
			String input = JOptionPane.showInputDialog(info);
			if (StringUtil.isEmpty(input) || !input.matches("[1-5]")) {
				JOptionPane.showMessageDialog(null, "请正确输入功能编号", "错误", JOptionPane.ERROR_MESSAGE);
				continue;
			}
			switch (input.charAt(0)) {
			// 存款
			case '1':
				saveMoney();
				break;
			// 取款
			case '2':
				remMoney();
				break;
			// 查询余额
			case '3':
				Account acc = this.list.get(index);
				JOptionPane.showMessageDialog(null, acc.getPubName() + "  当前余额:" + acc.getMoney());
				break;
			// 转账
			case '4':
				// 输入转账账号
				// 判断账户是否存在(不能给自己转)
				// 输入转账金额
				// 判断当前用户是否金额不足
				// 扣除当前账户金额,并且转入目标账户
				giveMoney();
				break;
			// 退出
			default:
				flag = false;
			}
		}
	}

	/**
	 * 转账操作
	 */
	private void giveMoney() {
		// 输入取款账号
		String quNo = JOptionPane.showInputDialog("请输入转账账号");
		// 判断输入是否为null或者输入账号是否满足条件
		// 100 2.54 .45
		if (StringUtil.isEmpty(quNo) || !quNo.matches("\\d{4,10}")) {
			JOptionPane.showMessageDialog(null, "账号输入无效", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 获取要转入账号对象
		int zhuanIndex = getIndexAccByNO(quNo);
		Account zhuanAcc = this.list.get(zhuanIndex);
		// 获取当前账户对象
		Account acc = this.list.get(index);
		// 判断账号是否存在
		if (index == -1 || zhuanAcc == acc) {
			JOptionPane.showMessageDialog(null, "账号输入无效", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 输入转账金额
		String input = JOptionPane.showInputDialog("请输入转账金额");
		double money = Double.parseDouble(input);
		// 判断输入金额是否满足条件
		if (money <= 0) {
			JOptionPane.showMessageDialog(null, "转账金额输入有误", "错误", JOptionPane.ERROR_MESSAGE);
		}
		// 判断当前用户是否余额不足

		// 判断所取金钱是否超过账户余额
		if (money > acc.getMoney()) {
			JOptionPane.showMessageDialog(null, "您当前账户没有这么多钱可以转出", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 确认是否真的转账操作
		if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,
				"您是否真的给" + zhuanAcc.getPubName() + "转" + money + "元钱?", "提示", JOptionPane.YES_NO_OPTION,
				JOptionPane.INFORMATION_MESSAGE)) {
			// 减去当前账户金额余额
			acc.setMoney(acc.getMoney() - money);
			// 给转账用户加上所转金额
			zhuanAcc.setMoney(zhuanAcc.getMoney() + money);
			// 保存进文件
//				this.fileUtil.saveAcc(acc);
//				this.fileUtil.saveAcc(zhuanAcc);
			this.fileUtil.saveAllAcc(list);
			// 提示存款成功
			JOptionPane.showMessageDialog(null, "转账成功!当前余额:" + acc.getMoney());
		}
	}

	/**
	 * 取款操作
	 */
	private void remMoney() {
		// 输入取款金额
		String input = JOptionPane.showInputDialog("请输入存款金额");
		// 判断输入是否为null或者输入金额是否满足条件
		// 100 2.54 .45
		if (StringUtil.isEmpty(input) || !input.matches("(\\d*|\\d+\\.\\d{2}|\\.\\d{2})")) {
			JOptionPane.showMessageDialog(null, "取款输入无效", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		double money = Double.parseDouble(input);
		// 判断输入金额是否满足条件
		if (money <= 0) {
			JOptionPane.showMessageDialog(null, "取款金额输入有误", "错误", JOptionPane.ERROR_MESSAGE);
		}
		// 获取当前账户对象
		Account acc = this.list.get(index);
		// 判断所取金钱是否超过账户余额
		if (money > acc.getMoney()) {
			JOptionPane.showMessageDialog(null, "您当前账户没有这么多钱可以取出", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 确认是否真的取款操作
		if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "您是否真的取" + money + "元钱?", "提示",
				JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE)) {
			// 设置取款
			acc.setMoney(acc.getMoney() - money);
			// 保存进文件
			this.fileUtil.saveAllAcc(list);
			// 提示存款成功
			JOptionPane.showMessageDialog(null, "取款成功!当前余额:" + acc.getMoney());
		}
	}

	/**
	 * 存款操作
	 */
	private void saveMoney() {
		// 输入存款金额
		String input = JOptionPane.showInputDialog("请输入存款金额");
		// 判断输入是否为null或者输入金额是否满足条件
		// 100 2.54 .45
		if (StringUtil.isEmpty(input) || !input.matches("(\\d*|\\d+\\.\\d{2}|\\.\\d{2})")) {
			JOptionPane.showMessageDialog(null, "存款输入无效", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		double money = Double.parseDouble(input);
		if (money <= 0) {
			JOptionPane.showMessageDialog(null, "存入金额输入无效", "错误", JOptionPane.ERROR_MESSAGE);
		}
		// 获取当前账户对象
		Account acc = this.list.get(index);
		// 设置存款
		acc.setMoney(acc.getMoney() + money);
		// 保存进文件
		this.fileUtil.saveAllAcc(list);
		// 提示存款成功
		JOptionPane.showMessageDialog(null, "存款成功!当前余额:" + acc.getMoney());
	}

	/**
	 * 注销账号
	 */
	private void deleteAcc() {
		String deleNo = JOptionPane.showInputDialog("请输入要注销的账号");
		// 判断输入是否为null
		if (StringUtil.isEmpty(deleNo)) {
			JOptionPane.showMessageDialog(null, "注销账号输入无效", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 查询是否有此账号
		int index = getIndexAccByNO(deleNo);
		if (index == -1) {
			JOptionPane.showMessageDialog(null, "查无此账户", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		Account acc = this.list.remove(index);
		// 确认是否真的注销此账号
		if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "是否真的注销账户: " + acc, "提示",
				JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE)) {
			// 解冻此账号
//			acc.setFlag(true);
			// 将修改的对象存到文件 覆盖全写所有对象
			this.fileUtil.saveAllAcc(this.list);
			// 账号冻结成功
			JOptionPane.showMessageDialog(null, "注销账户成功");
		}
	}

	/**
	 * 解绑账号
	 */
	private void jieLockAcc() {
		String jieLockNo = JOptionPane.showInputDialog("请输入要解冻的账号");
		// 判断输入是否为null
		if (StringUtil.isEmpty(jieLockNo)) {
			JOptionPane.showMessageDialog(null, "解冻账号输入无效", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 查询是否有此账号
		int index = getIndexAccByNO(jieLockNo);
		if (index == -1) {
			JOptionPane.showMessageDialog(null, "查无此账户", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 获得当前账号
		Account acc = this.list.get(index);
		// 判断是否已经解冻
		if (acc.isFlag()) {
			JOptionPane.showMessageDialog(null, "已经解冻了!", "提示", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		// 确认是否真的解冻此账号
		if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "是否真的解冻账户: " + acc, "提示",
				JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE)) {
			// 解冻此账号
			acc.setFlag(true);
			// 将修改的对象存到文件 覆盖全写所有对象
			this.fileUtil.saveAllAcc(this.list);
			// 账号冻结成功
			JOptionPane.showMessageDialog(null, "解冻账户成功");
		}
	}

	/**
	 * 冻结账号
	 */
	private void lockAcc() {
		String lockNo = JOptionPane.showInputDialog("请输入要冻结的账号");
		// 判断输入是否为null
		if (StringUtil.isEmpty(lockNo)) {
			JOptionPane.showMessageDialog(null, "冻结账号输入无效", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 查询是否有此账号
		int index = getIndexAccByNO(lockNo);
		if (index == -1) {
			JOptionPane.showMessageDialog(null, "查无此账户", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 获得当前账号
		Account acc = this.list.get(index);
		// 判断是否已经冻结
		if (!acc.isFlag()) {
			JOptionPane.showMessageDialog(null, "已经被冻结了!", "提示", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		// 确认是否真的冻结此账号
		if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "是否真的冻结账户: " + acc, "提示",
				JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE)) {
			// 冻结此账号
			acc.setFlag(false);
			// 将修改的对象存到文件 覆盖全写所有对象
			this.fileUtil.saveAllAcc(this.list);
			// 账号冻结成功
			JOptionPane.showMessageDialog(null, "冻结账户成功");
		}
	}

	private int getIndexAccByNO(String lockNo) {
		for (int i = 0; i < list.size(); i++) {
			if (lockNo.equals(list.get(i).getAccNo())) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 查看所有账号
	 */
	private void printAllAcc() {
		if (this.list.size() <= 0) {
			System.out.println("  新银行,没用户 ...");
			return;
		}
		System.out.println("-------------------");
		for (Account acc : list) {
			System.out.println(acc);
		}
		System.out.println("-------------------");
	}

	/**
	 * 新建用户
	 */
	public void createNewAcc() {
//		String name = JOptionPane.showInputDialog("请输入持卡人姓名");
//		if (StringUtil.isEmpty(name)) {
//			JOptionPane.showMessageDialog(null, "持卡人姓名不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
//			return;
//		}
//		String moneyStr = JOptionPane.showInputDialog("请输入开户金额");
//		if (StringUtil.isEmpty(moneyStr)) {
//			JOptionPane.showMessageDialog(null, "开户金额不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
//			return;
//		}
//		// 正则判断是否输入的金额是double型
//		if (!moneyStr.matches("(\\d*|\\d+\\.\\d{2}|\\.\\d{2})")) {
//			JOptionPane.showMessageDialog(null, "输入金额格式不正确!", "错误", JOptionPane.ERROR_MESSAGE);
//			return;
//		}
//		double money = Double.parseDouble(moneyStr);
//		String password = null;
//		// 重复输入密码
//		while (true) {
//			String pass1 = JOptionPane.showInputDialog("请输入支付密码");
//			if (StringUtil.isEmpty(pass1)) {
//				JOptionPane.showMessageDialog(null, "支付密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
//				continue;
//			}
//			String pass2 = JOptionPane.showInputDialog("请再次输入支付密码");
//			if (StringUtil.isEmpty(pass1)) {
//				JOptionPane.showMessageDialog(null, "确认密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
//				continue;
//			}
//			if (!pass1.equals(pass2)) {
//				JOptionPane.showMessageDialog(null, "密码验证失败!", "错误", JOptionPane.ERROR_MESSAGE);
//				continue;
//			}
//			password = pass1;
//			break;
//		}
		// 封装账户对象
//		Account acc = new Account(m.getUserName(), m.getUserPwd1(), 1000);
//		// 保存到文件
//		if (this.fileUtil.saveAcc(acc) == null) {
//			JOptionPane.showMessageDialog(null, "新建用户失败,后台错误,请与开发人员联系!", "错误", JOptionPane.ERROR_MESSAGE);
//			return;
//		}
//
//		JOptionPane.showMessageDialog(null, "新建用户成功,请记住你的账号:" + acc.getAccNo(), "成功", JOptionPane.INFORMATION_MESSAGE);
		//启动窗口
		ReeAccFrame frame = new ReeAccFrame();
		while(frame.isVisible()) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		frame.dispose();
		// 读取文件中所有用户到列表
		this.list = this.fileUtil.readAccs();
//		
//		
	}
}

1.2 创建用户界面类 ReeAccFrame.java


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.time.temporal.JulianFields;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import controller.ReeAccLis;

public class ReeAccFrame extends JFrame {
	private JTextField userNameJtf;
	private JComboBox<Double> userMoneyBox;
	private JPasswordField userPwd;
	private JPasswordField reUserPwd;
	private JButton confirmBtn;
	private JButton cancleBtn;
	public ReeAccFrame() {
		super("开户");
		this.setSize(300,300);
		//默认居中
		this.setLocationRelativeTo(null);
		//添加组件
		addComponent();
		this.setVisible(true);
//		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	private void addComponent() {
		//添加信息
		JPanel pan = new JPanel(new GridLayout(4,2,5,15));
		//定义边框
		pan.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"注册信息"));
		//添加组件到面板
		pan.add(new JLabel("用户姓名:"));
		userNameJtf = new JTextField();
		pan.add(userNameJtf);
		
		//开户金额
		Double[] moneys = {1000.0,2000.0,3000.0,5000.0,10000.0};
		userMoneyBox = new JComboBox<Double>(moneys);
		pan.add(new JLabel("开户金额:"));
		pan.add(userMoneyBox);
		//用户密码
		pan.add(new Label("用户密码:"));
		userPwd = new JPasswordField();
		pan.add(userPwd);
		//确认密码
		pan.add(new Label("确认密码:"));
		reUserPwd = new JPasswordField();
		pan.add(reUserPwd);
		//添加注册信息组件到中部
		this.add(pan);
		
		//添加按钮
		JPanel btnPan = new JPanel(new FlowLayout(FlowLayout.CENTER,50,5));
		cancleBtn = new JButton("取消");
		btnPan.add(cancleBtn);
		confirmBtn = new JButton("注册");
		btnPan.add(confirmBtn);
		//添加按钮面板到南部
		this.add(btnPan,BorderLayout.SOUTH);
		//定义按钮对象
		ReeAccLis rl = new ReeAccLis(this);
		//添加事件对象到两个按钮
		this.cancleBtn.addActionListener(rl);
		this.confirmBtn.addActionListener(rl);
	}
	public JTextField getUserNameJtf() {
		return userNameJtf;
	}
	public void setUserNameJtf(JTextField userNameJtf) {
		this.userNameJtf = userNameJtf;
	}
	public JComboBox<Double> getUserMoneyBox() {
		return userMoneyBox;
	}
	public void setUserMoneyBox(JComboBox<Double> userMoneyBox) {
		this.userMoneyBox = userMoneyBox;
	}
	public JPasswordField getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(JPasswordField userPwd) {
		this.userPwd = userPwd;
	}
	public JPasswordField getReUserPwd() {
		return reUserPwd;
	}
	public void setReUserPwd(JPasswordField reUserPwd) {
		this.reUserPwd = reUserPwd;
	}
	public JButton getConfirmBtn() {
		return confirmBtn;
	}
	public void setConfirmBtn(JButton confirmBtn) {
		this.confirmBtn = confirmBtn;
	}
	public JButton getCancleBtn() {
		return cancleBtn;
	}
	public void setCancleBtn(JButton cancleBtn) {
		this.cancleBtn = cancleBtn;
	}
	
}

二、controller层

2.1创建监听器 ReeAccLis.java


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import model.Account;
import util.FileUtil;
import util.StringUtil;
import view.ReeAccFrame;

public class ReeAccLis implements ActionListener{
	private ReeAccFrame window;

	public ReeAccLis(ReeAccFrame window) {
		super();
		this.window = window;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//判断是否为注册按钮
		if(e.getSource() == window.getConfirmBtn()) {
			//获取注册信息
			String userName = window.getUserNameJtf().getText();
			if(StringUtil.isEmpty(userName)) {
				JOptionPane.showMessageDialog(window, "持卡人姓名不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			}
			Double money = (Double)window.getUserMoneyBox().getSelectedItem();
			String userPwd = String.valueOf(window.getUserPwd().getPassword());
			String reUserPwd = String.valueOf(window.getReUserPwd().getPassword());
			if(StringUtil.isEmpty(userPwd) || StringUtil.isEmpty(reUserPwd)) {
				JOptionPane.showMessageDialog(window, "注册密码和确认密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(!userPwd.equals(reUserPwd)) {
				JOptionPane.showMessageDialog(window, "密码确认失败!", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			}
			// 封装账户对象
			Account acc = new Account(userName,userPwd,money);
			// 保存到文件
			//获得账号
			String createNo = new FileUtil().saveAcc(acc);
			if (createNo == null) {
				JOptionPane.showMessageDialog(null, "新建用户失败,后台错误,请与开发人员联系!", "错误", JOptionPane.ERROR_MESSAGE);
			}else {
				JOptionPane.showMessageDialog(null, "新建用户成功,请记住你的账号:" + createNo, "成功", JOptionPane.INFORMATION_MESSAGE);
			}
			//关闭窗口
			window.setVisible(false);
			}else {
			//取消按钮
			window.setVisible(false);
		}
	}
	
	
}

三、model层

3.1 创建账户类 Account.java


import java.io.Serializable;

/**
 * 账户类
 * 
 * @author LinChi
 *
 */
public class Account implements Serializable {
	// 账号
	private String accNo;
	// 姓名
	private String name;
	// 密码
	private String password;
	// 金钱
	private double money;
	// 设置冻结状态 true 表示正常 false 表示冻结
	private boolean flag = true;

	public Account() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Account(String name, String password, double money) {
		this.name = name;
		this.password = password;
		this.money = money;
	}

	public String getAccNo() {
		return accNo;
	}

	public void setAccNo(String accNo) {
		this.accNo = accNo;
	}

	public String getPubName() {
		// 小明 *明
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < this.name.length(); i++) {
			if (i == this.name.length() - 1) {
				sb.append(this.name.charAt(i));
			} else {
				sb.append("*");
			}
		}
		return sb.toString();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public double getMoney() {
		return money;
	}

	public void setMoney(double money) {
		this.money = money;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	@Override
	public String toString() {
		return "状态:" + (this.flag ? "正常" : "冻结") + "|[accNo=" + accNo + ", name=" + name + ", password=" + password
				+ ", money=" + money + "]";
	}

}

3.2 创建接口 Constence


import java.io.File;

/**
 * 创建文件路径接口
 * @author LinChi
 *
 */
public interface Constant {
	//文件路径
	File BANK_DIR = new File("I:/bank.dir");
	//账号文件
	File ACC_NO_FILE = new File(BANK_DIR,"acc_no.no");
	//账户文件
	File ACC_FILE = new File(BANK_DIR,"accs.scc");
}

四、util工具类

4.1 创建获取文件工具类 FileUtil.java

/**
 * 文件工具类
 * @author LinChi
 *
 */

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import model.Account;
import model.Constant;

public class FileUtil {
	// 创建输入流
	private FileInputStream fin;
	// 创建输出流
	private FileOutputStream fout;
	// 创建对象输入流
	private ObjectInputStream objIn;
	// 创建对象输出流
	private ObjectOutputStream objOut;

	public FileUtil() {
		init();
	}

	/**
	 * 初始化
	 */
	public void init() {
		// 创建必要的文件
		// 判断文件夹是否存在
		if (!Constant.BANK_DIR.exists()) {
			Constant.BANK_DIR.mkdirs();
		}
		try {
			// 判断账户文件是否存在,不存在创建账户和账号文件
			if (!Constant.ACC_FILE.isFile()) {
				Constant.ACC_FILE.createNewFile();
				Constant.ACC_NO_FILE.createNewFile();
				// 向账号文件写入初始账号
				fout = new FileOutputStream(Constant.ACC_NO_FILE);
				objOut = new ObjectOutputStream(fout);
				objOut.writeObject("1000");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeAll();
		}
	}

	/**
	 * 保存账户信息
	 * 
	 * @param acc
	 * @return
	 */
	public String saveAcc(Account acc) {
		// 获取新账号
		String newAccNo = getNewAccNo();
		// 将账号设置到账户中
		acc.setAccNo(newAccNo);
		// 保存文件到用户文件
		try {
			// 添加文件内容
			if (Constant.ACC_FILE.length() > 0) {
				fout = new FileOutputStream(Constant.ACC_FILE, true);
				objOut = new MyObjectOutputStream(fout);
			} else {
				fout = new FileOutputStream(Constant.ACC_FILE);
				objOut = new ObjectOutputStream(fout);
			}
			// 将对象写入对象输出流
			objOut.writeObject(acc);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			closeAll();
		}
		return newAccNo;
	}

	/**
	 * 覆盖保存所有用户
	 */
	public void saveAllAcc(List<Account> accList) {
		try {
			fout = new FileOutputStream(Constant.ACC_FILE);
			objOut = new ObjectOutputStream(fout);
			for (Account acc : accList) {
				objOut.writeObject(acc);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll();
		}
	}

	/**
	 * 读取数据
	 * 
	 * @return
	 */
	public List<Account> readAccs() {
		// 创建集合对象,存放账户对象
		List<Account> list = new ArrayList<Account>();
		try {
			fin = new FileInputStream(Constant.ACC_FILE);
			objIn = new ObjectInputStream(fin);
			while (true) {
				list.add((Account) objIn.readObject());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EOFException e) {
			System.out.println("读到文件尾,正确读取结束!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeAll();
		}
		return list;
	}

	/**
	 * 获取新账号
	 * 
	 * @return
	 */
	private String getNewAccNo() {
		// 定义账号对象
		String accNo = null;
		try {
			// 读入账号文件中的内容
			fin = new FileInputStream(Constant.ACC_NO_FILE);
			// 将读入的文件保存在对象输入流中
			objIn = new ObjectInputStream(fin);
			// 将读取到的文件对象信息保存在对象中
			accNo = (String) objIn.readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeAll();
		}
		// 将账号+1重新写回文件,保证账号不重复
		try {
			// 写入账号文件中的内容
			fout = new FileOutputStream(Constant.ACC_NO_FILE);
			// 将读入的文件保存在对象输入流中
			objOut = new ObjectOutputStream(fout);
			// 将读取到的文件对象信息保存在对象中
			objOut.writeObject(String.valueOf(Integer.parseInt(accNo) + 1));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.closeAll();
		}
		return accNo;
	}

	/**
	 * 关闭流对象
	 */
	private void closeAll() {
		try {
			if (objOut != null) {
				objOut.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (fout != null) {
				fout.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (objIn != null) {
				objIn.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (fin != null) {
				fin.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

4.2、创建检索字符串类 StringUtil.java

/**
 * 越界限制工具类
 * @author LinChi
 *
 */
public class StringUtil {
	public static boolean isEmpty(String str) {
		if(str == null || str.length()<=0 || str.trim().length()<=0) {
			return true;
		}
		return false;
	}
}

4.3、创建自定义输出流 MyObjectOutputStream.java


import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class MyObjectOutputStream extends ObjectOutputStream {

	public MyObjectOutputStream(OutputStream out) throws IOException, SecurityException {
		super(out);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void writeStreamHeader() throws IOException {
		// 重置指针
		this.reset();
	}

}

五、主方法 app.java


import view.MainView;

public class App {
	public static void main(String[] args) {
		MainView mv = new MainView();
		mv.bankOper();
	} 
}

程序截图
在这里插入图片描述
在这里插入图片描述


今天的分享内容就到此结束了!期待明天的精彩内容吧!


发布了17 篇原创文章 · 获赞 3 · 访问量 512

猜你喜欢

转载自blog.csdn.net/qq_41986840/article/details/104449608