一、实验目的

(1)全面检验面向对象编程思想,巩固Java面向对象、集合和常用API类等方面知识的应用;
(2)加强实践动手能力,能够将从书本上学习到的理论知识用到了实践上。

二、实验内容

模拟网上银行业务,当用户登录时需判断银行卡号和银行卡密码,当输入的卡号和密码都正确时,登录成功,提示当前登录的账户名,并进入下一步选择操作类型。操作类型包括四种(存款:1 取款:2 余额:3 修改个人密码:4 退出:0),输入数字1、2时,将进行存取款操作,此时需要输入存取的金额,并进行正确的金额加减计算;输入数字3时,显示当前账户的余额;输入数字4时,可修改当前账户的密码;输入数字0时将退出整个系统。提示:可利用HashMap集合存储模拟的账户信息,其中key值用于存储银行卡号,value值用于存储整个账户对象。

三、总体设计(设计原理、设计方案及流程等)

结构:
该实验共有四个包
1.image放置图片包,
2.DataBase模拟银行系统中的账户信息,相当于数据库的功能,
3.frame放置了登入界面实现类与银行操作界面的实现类,运用了简单的GUI,实现了逻辑结构,
4.module放置用户模型类
还有一个properties文件,可以把数据读取到文件中

设计原理:
1.把银行用户放入到一个hashMap中,每次进入和退出程序,都会把hashMap表中的数据读取到properties文件中,达到保留数据的功能。
2.用户登入必须要输入对的userName与passWord才可以登入进去,bank界面可以实现存款,取款,查看余额,修改账户密码等等,都可以通过修改hashMap表中的数据来达到效果,然后再存入到properties文件中保留数据,保证下次登入时可以重新恢复数据

四、代码实现

package BankManagementSystem.DataBase;import BankManagementSystem.model.User;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;/*** @author 汪杰* @date 2021/11/23 15:09* @details 模拟银行系统中的账户信息,相当于数据库的功能*/
public class DataBaseUtil {private static DataBaseUtil instance = null;private static HashMap<String, User> users = new HashMap<>();private DataBaseUtil(){Properties pro =new Properties();//充当简单的数据库try {pro.load(new FileReader("src/data.properties"));} catch (IOException e) {e.printStackTrace();}int allNum = Integer.parseInt(pro.getProperty("allNum"));for(int i=1;i <= allNum;i++){User user = new User();user.setCardId(pro.getProperty(i+"-id"));user.setCardPwd(pro.getProperty(i+"-pwd"));user.setAccount(Integer.parseInt(pro.getProperty(i+"-account")));user.setCallNum(pro.getProperty(i+"-callNum"));user.setUserName(pro.getProperty(i+"-name"));users.put(user.getCardId(),user);//加入到集合中}/*User user01 = new User();user01.setUserName("WJ");user01.setCardId("123456");user01.setCardPwd("123456");user01.setCallNum("000000");user01.setAccount(1000);users.put(user01.getCardId(),user01);User user02 = new User();user02.setUserName("XXY");user02.setCardId("1234567");user02.setCardPwd("1234567");user02.setCallNum("0000000");user02.setAccount(1000);users.put(user02.getCardId(),user02);User user03 = new User();user03.setUserName("gubao");user03.setCardId("12345678");user03.setCardPwd("12345678");user03.setCallNum("00000000");user03.setAccount(1000);users.put(user03.getCardId(),user03);*/}public static DataBaseUtil getInstance(){//懒汉式单例模式,使所有对象唯一了if(instance == null){//同步代码块p371synchronized (DataBaseUtil.class){instance = new DataBaseUtil();}}return instance;}//根据id获得用户个人信息public static User getUser(String userName){return users.get(userName);}//获得hashMappublic static HashMap<String,User> getUsers(){return users;}public static boolean CheckLogin(String userName,String passWord){Set set = users.keySet();Iterator it = set.iterator();while(it.hasNext()){String userid = (String) it.next();User user = users.get(userid);//判断id与密码是否一致if(userid.equals(userName) && user.getCardPwd().equals(passWord)){return true;}}return false;}}
package BankManagementSystem.frame;import BankManagementSystem.DataBase.DataBaseUtil;
import BankManagementSystem.model.User;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.Vector;/*** @author 汪杰* @date 2021/11/30 11:38* @details 简单实现银行面板,和一些基本操作(存钱,取钱,余额,修改密码)*/
public class BankFrame extends JFrame {private User user;Properties pro = new Properties();//充当简单的数据库int index;//数据库索引JPanel contentPane;JScrollPane scp = new JScrollPane();JTable tab = null;String text;//文本数据Vector CellsVector = new Vector();// 标题行Vector TitleVector = new Vector();// 数据行JButton btn_save = new JButton("存钱");JButton btn_withdraw = new JButton("取钱");JButton btn_remain = new JButton("余额");JButton btn_set_passWord = new JButton("修改密码");JTextField text_context = new JTextField();//输入内容JLabel context = new JLabel("输入相关数字: ");public BankFrame(String userName){try {//加载数据库pro.load(new FileReader("src/data.properties"));} catch (IOException e) {e.printStackTrace();}index = getUserIndex(userName);//获得user对象user = DataBaseUtil.getUser(userName);//JPanel面板的设置contentPane = (JPanel) getContentPane();contentPane.setLayout(null);this.setResizable(false);setSize(new Dimension(600, 340));setLocationRelativeTo(null);setTitle(user.getUserName() + "用户");//滑动面板的设置scp.setBounds(new Rectangle(46, 32, 497, 157));
//        JLabel的坐标建立context.setFont(new Font("宋体", Font.BOLD, 14));context.setBounds(136,200,126,20);//Text的坐标建立text_context.setBounds(255,200,130,20);text_context.setFont(new Font("宋体", Font.BOLD, 14));text_context.setDocument(new JTextFieldLimit(12));//限制密码长度为12//Button的坐标建立btn_save.setBounds(new Rectangle(85, 271, 70, 25));btn_save.setFont(new Font("宋体", Font.BOLD, 12));btn_save.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(checkInformation()){Integer num =Integer.valueOf(text);user.setAccount(user.getAccount()+num);//存入用户存款DataBaseUtil.getUsers().put(user.getCardId(),user);//存入数据库pro.setProperty(index+"-account",String.valueOf(user.getAccount()));try {pro.store(new FileWriter("src/data.properties"),"修改值");} catch (IOException ioException) {ioException.printStackTrace();}LocalDateTime now = LocalDateTime.now();DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");Vector vector = new Vector();vector.add("存款");vector.add(num);vector.add(now.format(dtf));CellsVector.add(vector);tab.updateUI();}}});btn_withdraw.setBounds(195, 271, 70, 25);btn_withdraw.setFont(new Font("宋体", Font.BOLD, 12));btn_withdraw.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(checkInformation()){Integer num =Integer.valueOf(text);if(num > user.getAccount()){//判断余额不足JOptionPane.showMessageDialog(contentPane, "您好!你的账户余额不足!", "提示", 1);text_context.setText("");return;}user.setAccount(user.getAccount()-num);//存入用户存款DataBaseUtil.getUsers().put(user.getCardId(),user);//存入数据库pro.setProperty(index+"-account",String.valueOf(user.getAccount()));try {//将properties加载到对应文件中pro.store(new FileWriter("src/data.properties"),"修改值");} catch (IOException ioException) {ioException.printStackTrace();}LocalDateTime now = LocalDateTime.now();DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");Vector vector = new Vector();vector.add("取款");vector.add(num);vector.add(now.format(dtf));CellsVector.add(vector);tab.updateUI();}}});btn_remain.setBounds(new Rectangle(295, 271, 70, 25));btn_remain.setFont(new Font("宋体", Font.BOLD, 12));btn_remain.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {LocalDateTime now = LocalDateTime.now();DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");Vector vector = new Vector();vector.add("账户余额");vector.add(user.getAccount());vector.add(now.format(dtf));CellsVector.add(vector);tab.updateUI();text_context.setText("");}});btn_set_passWord.setBounds(395, 271, 120, 25);btn_set_passWord.setFont(new Font("宋体", Font.BOLD, 12));btn_set_passWord.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//检查操作text = text_context.getText();if (text.equals("")) {//如果是空的输入,输入警告JOptionPane.showMessageDialog(contentPane, "您好!请输入密码!", "提示", 1);text_context.grabFocus();return;}user.setCardPwd(text);//数据库修改密码DataBaseUtil.getUsers().put(user.getCardId(),user);JOptionPane.showMessageDialog(contentPane, "修改成功! 账户名:"+ user.getUserName() + " 密码:" +user.getCardPwd(), "提示", 1);pro.setProperty(index+"-pwd",String.valueOf(user.getCardPwd()));try {pro.store(new FileWriter("src/data.properties"),"修改值");} catch (IOException ioException) {ioException.printStackTrace();}System.out.println(pro.getProperty("1-pwd"));LocalDateTime now = LocalDateTime.now();DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");Vector vector = new Vector();vector.add("修改密码");vector.add("修改成功");vector.add(now.format(dtf));CellsVector.add(vector);tab.updateUI();text_context.setText("");}});//将组件添加到JPanel中contentPane.add(scp);contentPane.add(btn_save);contentPane.add(btn_withdraw);contentPane.add(btn_remain);contentPane.add(btn_set_passWord);contentPane.add(context);contentPane.add(text_context);this.TitleVector.add("查询操作");this.TitleVector.add("操作数额(人民币元)");this.TitleVector.add("操作时间");tab = new JTable(CellsVector, TitleVector);scp.getViewport().add(tab);this.add(scp,BorderLayout.CENTER);this.setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}// 对输入框进行验证public boolean checkInformation() {text = text_context.getText();if (text.equals("")) {//如果是空的输入,输入警告JOptionPane.showMessageDialog(contentPane, "您好!请输入数额!", "提示", 1);text_context.grabFocus();return false;}char[] ans = text.toCharArray();for (int i = 0; i < ans.length; i++) {//遍历每个字符if (!Character.isDigit(ans[i])) {//如果不是数字输出,输出警告JOptionPane.showMessageDialog(contentPane, "您好!数额只可以是数字!", "提示", 1);text_context.setText("");text_context.grabFocus();return false;}if (text.length() > 5) {JOptionPane.showMessageDialog(contentPane, "您好! 存款不可以超过99999元", "提示", 1);text_context.setText("");text_context.grabFocus();return false;}}text_context.setText("");return true;}//从Properties中获取对应的userprivate int getUserIndex(String userId){int i = Integer.parseInt(pro.getProperty("allNum"));for(int j=1;j <= i ;j++){if(pro.getProperty(j+"-id").equals(userId)){return j;}}return -1;}
}
package BankManagementSystem.frame;import BankManagementSystem.DataBase.DataBaseUtil;import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;/*** @author 汪杰* @date 2021/11/30 11:38* @details 登入界面的实现类*/
public class LoginFrame extends JFrame {/*** 用户名*/private static final JLabel usernameLabel = new JLabel("用户名(小于12位)");private static final JTextField usernameInput = new JTextField();/*** 密码*/private static final JLabel passwordLabel = new JLabel("密码(小于12位)");private static final JTextField passwordInput = new JPasswordField();/*** 登录和退出按钮*/private static final JButton loginButton = new JButton(new ImageIcon("image/login.png"));//使用图片//    private static final JButton loginButton = new JButton("登录");private static final JButton exitButton = new JButton("注册   ");public LoginFrame() {setSize(new Dimension(380, 180));setLocationRelativeTo(null);// 设置窗体的标题setTitle("银行登入系统");setLayout(null);//初始化界面initUI();// 点击关闭退出程序setDefaultCloseOperation(EXIT_ON_CLOSE);// 设置窗体可见setVisible(true);//数据库的加载DataBaseUtil.getInstance();// 该处设计为登录按钮的监听loginButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 获取用户名和密码输入框的值String userName = usernameInput.getText().trim();String passWord = passwordInput.getText().trim();// 用户名为空时提示用户输入if ("".equals(userName)) {//JOptionPane可以轻松地弹出一个标准对话框,// 提示用户获取值或通知他们某些东西。JOptionPane.showMessageDialog(LoginFrame.this, "请输入用户名");return;}// 密码为空时提示用户输入if ("".equals(passWord)) {JOptionPane.showMessageDialog(LoginFrame.this, "请输入密码");return;}// 查询数据库if (!DataBaseUtil.CheckLogin(userName, passWord)) {JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误");return;}// 首先关闭当前界面dispose();// 打开用户管理界面new BankFrame(userName);}});// 该处设计为退出按钮的监听exitButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.exit(0);}});}/*** 初始化界面*/private void initUI() {usernameInput.setDocument(new JTextFieldLimit(12));passwordInput.setDocument(new JTextFieldLimit(12));usernameLabel.setBounds(10, 10, 100, 21);usernameInput.setBounds(150, 10, 200, 21);passwordLabel.setBounds(10, 40, 100, 21);passwordInput.setBounds(150, 40, 200, 21);loginButton.setBounds(60, 100, 120, 21);exitButton.setBounds(200, 100, 120, 21);add(usernameLabel);add(usernameInput);add(passwordLabel);add(passwordInput);add(loginButton);add(exitButton);}//启动入口public static void main(String[] args) {new LoginFrame();}}//限制输入长度的类
class   JTextFieldLimit extends PlainDocument {private int limit;  //限制的长度public JTextFieldLimit(int limit) {super(); //调用父类构造this.limit = limit;}public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {if(str == null) return;//下面的判断条件改为自己想要限制的条件即可,这里为限制输入的长度if((getLength() + str.length()) <= limit) {super.insertString(offset, str, attr);//调用父类方法}}}
package BankManagementSystem.model;/*** @author 汪杰* @date 2021/11/23 15:01* @details 用户信息类*/
public class User {private String cardId;//用户idprivate String cardPwd;//用户密码private String userName;//用户名private String callNum;//用户电话号码private int account;//用户账户余额public String getCardId() {return cardId;}public void setCardId(String cardId) {this.cardId = cardId;}public String getCardPwd() {return cardPwd;}public void setCardPwd(String cardPwd) {this.cardPwd = cardPwd;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getCallNum() {return callNum;}public void setCallNum(String callNum) {this.callNum = callNum;}public int getAccount() {return account;}public void setAccount(int account) {this.account = account;}
}

还有一个data.properties文件不要忘了创建在src包下并且加入allNum=0这一行数据即可!

五、结果分析与总结


模拟银行存取款业务(GUI版)相关推荐

  1. java模拟银行存取_JAVA基础案例 模拟银行存取款业务

    模拟银行存取款业务 编写一个Java应用程序,模拟网上银行登录及存取款业务.登录时需判断银行卡号和银行卡密码,当输入的卡号和密码都正确时,登录成功,提示当前登录的账户名,并进入下一步选择操作类型.操作 ...

  2. java实验报告之模拟银行存取款业务

    一个不知名大学生,江湖人称菜狗 original author: jacky Li Email : 3435673055@qq.com Time of completion:2022.12.20 La ...

  3. java模仿银行账务业务_Java基础案例 - 模拟银行存取款业务

    博学谷--让IT教学更简单,让IT学习更有效 模拟银行存取款业务 编写一个Java应用程序,模拟网上银行登录及存取款业务.登录时需判断银行卡号和银行卡密码,当输入的卡号和密码都正确时,登录成功,提示当 ...

  4. 模拟银行存款java,模拟银行存取款业务

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 public class Account { // 初始化存款 float count = 0f;// 当前账户余额 int money1;// 存款数, ...

  5. 模拟银行存取款业务 java

    实训题5:模拟银行存取款业务 实训目的:本项目的主要功能是模拟银行的存取款业务,当用户登录时需判断银行卡号和银行卡密码,当输入的卡号和密码都正确时,登录成功,提示当前登录的账户名,并进入下一步选择操作 ...

  6. 基于Java语言实现模拟银行存取款业务系统

    资源下载地址:https://download.csdn.net/download/sheziqiong/85820969 1.项目简介 本项目的主要功能是模拟银行的存取款业务,当用户登录时需判断银行 ...

  7. python代码设计测试用例_[CP_01] Python循环结构案例:模拟银行存取款业务,设计执行测试用例...

    目录结构 一.案例描述 二.需求分析 1. 业务流程图 2. 业务项&测试场景 三.代码实现 四.用例设计&执行(简化模式) 1. 登录业务 2. 存款业务 3. 取款业务 4. 退卡 ...

  8. 模拟银行存取款业务 简要代码

    本项目的主要功能是模拟银行的存取款业务,当用户登录时需判断银行卡号和银行卡密码,当输入的 卡号和密码都正确时,登录成功,提示当前登录的账户名,并进入下一步选择操作类型.操作类型包括四种(存款:1取款: ...

  9. java银行业务_java模拟银行存取款业务

    //创建基接口BankAccount,包含 存款方法playIn(),取款方法withdraw(),查询余额方法getBalance(). interfaceBankAccount{void play ...

最新文章

  1. 设计模式-组合+策略模式
  2. java之IO整理(中)
  3. php程序变量,PHP 变量
  4. ES中如何使用逗号来分词
  5. 关于创业:希望有人在N年前就告诉我的一些事儿
  6. ebnf范式_使用Scala基于词法单元的解析器定制EBNF范式文法解析
  7. php 加载慢,PHP版网站缓存加快打开速度的方法分享
  8. 84.负载均衡哈希算法:ip_hash与hash模块
  9. 易语言 html邮件,易语言邮件收发源码
  10. 数据、数据库、数据库管理系统,数据库系统的概念
  11. riot账号服务器互通吗,云顶之弈手游和PC数据互通吗账号数据同步分析
  12. 单细胞测序数据下载和预处理
  13. matlab44矩阵,如何在MATLAB中将2X2矩阵转换为4X4矩阵?
  14. 素描入门工具需要准备哪些
  15. 双粗虚线中间一条实线_道路中间一条白实线,一条白虚线平行,代表什么意思?...
  16. Oracle 11g存在密码过期问题
  17. JavaScript中string与number
  18. 白内障、散光、老花、眼压高,一个方法全部解决
  19. TimesTen 数据库复制学习:10. 定义classic复制
  20. JS二维码生成插件,一键生成二维码

热门文章

  1. 微信团队分享:极致优化,iOS版微信编译速度3倍提升的实践总结
  2. c语言 random安全函数,random函数-关于C语言的函数问题请问,randomize()random这两 爱问知识人...
  3. mysql删除一天前数据_MYSQL如何删除三天前的数据(两个时间比较函数)
  4. 思科路由器 一些配置
  5. 编写函数has(arr , 60) 判断数组中是否存在60这个元素,返回布尔类型
  6. Father-Child
  7. concurrent-5-AQS-Condition
  8. 一个计算机爱好者的不完整回忆(二十一)歪打正着
  9. 员工绩效考核计算机二级考试题解析,绩效管理考试试题及答案解析.doc
  10. jboss 下载xls 不提示下载框 直接打开文件了 解决办法