基于JavaSwing ATM取款机系统的设计和实现

这是我参与 8 月更文挑战的第 5 天,活动详情查看: [8月更文挑战]

)

前言:

本项目是使用Java swing开发,可实现ATM系统/银行系统的基本登陆、转账、查询余额、存取款业务。界面设计比较简介、适合作为Java课设设计以及学习技术使用。

需求分析:

随着生活水平的提高,消费量的增大,开销也越来越大,自然离不开的就是钱。人们有的要取钱,有的要存钱,可是只能去银行,而银行的遍布并不是很广,它可能在人流密集度比较大的地方会设立,或者稍大范围内设立一个,但是对于比较偏远地区的人们,无疑造成了非常大的困难。那么,如何来解决这个问题那?研发ATM柜员机即为广大用户提供了便捷,改善了生活。您无需再到银行排队办理一些简单的业务, ATM柜员机为您提供取款,存款,余额查询,修改密码等功能操作。而且ATM的遍及范围远远大于银行,主要是ATM的自身功能容易实现日容易布局,不需要耗费大量的空间,人力及物力,可以在很多点来设立。也正是在这种情况下, ATM柜员机得到了人们的喜爱并得到了大量的普及,可以说对银行和人们都非常有益的。本系统通过设计与开发Windows系统,主要完成了余额查询功能,取款功能,存款功能,转账功能,退出系统功能,目的在于通过 ATM自动存取款 机实现一些简单的动能。

主要模块:

用户登录、注册、重置、存款、查询余额、取款、转账、更改密码、退卡等具体功能

功能截图:

登录注册:

运行程序启动mian方法进入登录页面

)

首页

)

存款

存入输入的金额点击确认完成存款、存款的时候输入的必须是整数

)

查询余额

查询自己的余额以及操作记录信息

)

取款

取款金额不能大于账户余额

)

转账

转账的时候必须正确输入用户id信息、否则转款失败

)

更改密码

输入原密码进行校验后、输入2次相同的新密码完成修改密码功能

)

数据库设计:

这个ATM暂时没用数据库、是以文本txt的形式进行存储数据、更方便快捷简单话

部分关键代码:

主启动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ini复制代码  public static void main(String[] args)throws Exception {  
usersList = new ArrayList<Account>();
//System.out.println(usersList);
/**********************用户文本**********************/
File users = new File("users.txt");
if (!users.exists()) {
try {
users.createNewFile();
Writer fw = new FileWriter("users.txt");
fw.write("123456 123456 10000");
fw.flush();
fw.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "创建用户文档失败");
}

}
usersFile = users;//创建用户文档,存储用户账户,密码,余额信息;
usersListRead();
usersListUpdate();
/*****************************Login****************************/

LoginGui loginGui = new LoginGui();
}

账号相关:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
java复制代码package atm;
//import com.sun.deploy.util.SyncFileAccess;
//import com.sun.org.apache.regexp.internal.RE;

import javax.swing.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class Account {
int money;
String id;//账号名

String password;
Date now=new Date();
Date currentTime;
SimpleDateFormat formatter;
Reader fr;
;
public Account(String id, String password, String money) {//构造方法
this.id = id;

this.password = password;
this.money=Integer.parseInt(money);
}

public void outMoney (int money)throws Exception {//抛出异常,由相关的界面类弹窗处理异常,下面几个方法同理
//如在取钱界面取钱,则会调用此函数,进行try/catch处理,获得这个函数的异常,弹窗说明异常
if (money > this.money) {
throw new Exception("余额不足");
}
if(money<0)
{
throw new Exception("不能取出负数");
}
formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//时间格式
currentTime = new Date();//当前时间
String dateString = formatter.format(currentTime);//处理当前时间格式
Writer fw = new FileWriter(Test.file);
fw.write(Test.recordString.append(dateString + "\t" + Test.currentAccount.id + "\t取出" + money + "元\r\n").toString());//将这次的取钱行为添加到记录文件中
fw.flush();//写进文件
fw.close();
this.money -= money;
Test.usersListUpdate();//更新用户文档(信息)
}

public void inMoney(int money)throws Exception
{
try {
Writer fw = new FileWriter(Test.file);
// System.out.println(Test.file);
formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
currentTime=new Date();
String dateString=formatter.format(currentTime);
fw.write(Test.recordString.append(dateString+"\t"+Test.currentAccount.id+"\t存入" + money + "元\r\n").toString());
fw.flush();//写进文件
fw.close();

this.money+=money;

Test.usersListUpdate();//更新当前用户信息

}
catch (Exception e1)
{
throw new Exception("写入记录失败");
}

}

public void transfer(int money,String id)throws Exception//转账
{
if(id.equals(Test.currentAccount.id))
{
throw new Exception("不能转给自己");
}
if(money>this.money)
{
throw new Exception("余额不足");
}
if(money<0) {
throw new Exception("不能转入负数");
}


for(int i=0;i<Test.usersList.size();i++)
{
if(Test.usersList.get(i).id.equals(id))//找到要转帐的用户
{
Test.usersList.get(i).money+=money;//转入
this.money-=money;//扣钱

FileWriter fw=new FileWriter(Test.file);
formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//声明时间格式
currentTime=new Date();//获取当前时间
String dateString=formatter.format(currentTime);//转换时间格式
fw.write(Test.recordString.append(dateString+"\t向"+id+"\t转出"+money+"元\r\n").toString());//Test类中的静态字符串拼接上这个字符串覆盖写入当前用户文档
fw.close();

/********************向转入目标写入转账信息*************************/
try {
fr = new FileReader(id+".txt");//字符流
}
catch (Exception e)
{
System.out.println("字符流创建失败");
}

BufferedReader bfr = new BufferedReader(fr);

String temp="";
String temp1;

while ((temp1 = bfr.readLine()) != null)
{
temp+=temp1;
}
temp=temp.replace("元","元\n\r")+dateString+"\t由"+Test.currentAccount.id+"\t转进"+money+"元\r\n";
System.out.println(temp);
fw=new FileWriter(id+".txt");
fw.write(temp);
fw.close();


JOptionPane.showMessageDialog(null,"转账成功");
Test.usersListUpdate();//更新用户文档

return;
}
}
throw new Exception("目标用户不存在");
}

public void ChangePassword(String newPassword)throws Exception
{
if(newPassword.equals(this.password))
{
throw new Exception("原密码和新密码不能一样");
}

else
{
if(newPassword.equals(""))
{
throw new Exception("密码不能为空");
}

}
password=newPassword;
Test.usersListUpdate();


}



}

修改密码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
scss复制代码package atm;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChangePassword implements ActionListener{
public JPasswordField oldPassword,newPassword,checkPassword;
public JFrame iframe;
public JPanel ip0,ip1,ip2,ip3,ip4;
public JButton confirm,cancel,exit;
public ChangePassword() {
iframe=new JFrame("更改密码");
iframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

ip2=new JPanel();
ip2.add(new JLabel("原密码:"));
oldPassword=new JPasswordField(20);
ip2.add(new JLabel("<html><br/><html>"));//换行
ip2.add(oldPassword);

ip0=new JPanel();
ip0.add(new JLabel("新密码:"));
newPassword=new JPasswordField(20);
ip0.add(new JLabel("<html><br/><html>"));//换行
ip0.add(newPassword);

ip4=new JPanel();
ip4.add(new JLabel("再次输入新密码:"));
checkPassword=new JPasswordField(20);
ip4.add(new JLabel("<html><br/><html>"));//换行
ip4.add(checkPassword);

ip3=new JPanel();
confirm=new JButton("确定");
ip3.add(confirm);
cancel=new JButton("返回");
ip3.add(cancel);

iframe.add(ip2);
iframe.add(ip0);
iframe.add(ip4);
iframe.add(ip3);
iframe.add(confirm);
iframe.add(cancel);
iframe.setLayout(new FlowLayout());
iframe.setVisible(true);
iframe.setTitle("密码更改");//窗体标签
iframe.setSize(400, 200);//窗体大小
iframe.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
confirm.addActionListener(this);

cancel.addActionListener(this);

}
public void pw_clean() {
newPassword.setText("");
oldPassword.setText("");
checkPassword.setText("");
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("确定")) {
if (Test.currentAccount.password.equals(oldPassword.getText())) {
try {
if(newPassword.getText().equals(checkPassword.getText())) {
if(newPassword.getText().length()>=6) {
Test.currentAccount.ChangePassword(newPassword.getText());
JOptionPane.showMessageDialog(null, "更改密码成功");
iframe.setVisible(false);
Test.menu.mframe.setVisible(false);//关闭菜单界面
new LoginGui();
}else {
JOptionPane.showMessageDialog(null,"密码不能少于6位!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
pw_clean();
}
}
else
{
JOptionPane.showMessageDialog(null, "两次输入的密码不一致");
pw_clean();
}
}
catch (Exception e1) {//捕获账户类中更改密码函数的异常并弹窗显示
JOptionPane.showMessageDialog(null, e1.getMessage());
pw_clean();
}
} else {

JOptionPane.showMessageDialog(null, "原密码错误");
pw_clean();
}
}
else//如果点击cancel
{
iframe.setVisible(false);
}
}
}

文档结构图:

)

总结:

在本次课程设计中我主要负责登陆界面部分和界面优化。通过这次课程设计。我学到了许多令我受益匪浅的知识。感觉java的界面设计和 mfc差不多。只是java没有可视化的界面做起来太累了。其他主要是类和对象的问题。实现起来还是挺简单的。

大家可以点赞、收藏、关注、评论我啦

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%