ATM
ATM柜员机模拟项目
项目开发基本要求:
(1)功能要求:
- 登陆
- 查询剩余金额
- 存入现金
- 取款
- 转帐
- 修改密码
- 查询明细
- 等等
(2)系统界面要求:
- 要求系统具有友好的用户界面
- 界面简洁
- 操作方便
使用语言Java,工具IDEA
1.首先完成登录界面
package ATM_Pakege;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
//登录界面
public class LoginGui implements ActionListener {
//窗口 并给窗口定义一个名字
private JFrame frame = new JFrame("ATM");
//面板
private JPanel panel4;
//输入框
private JTextField userName;
private JTextField passWord;
private JTextField passwordCheck;
//按钮
private JButton login;
private JButton register;
private Reader fw;
//用来检查是否能注册账户
Boolean regirsterable = true;
//登录界面
public LoginGui() {
//当你点击×除键时,它会仅仅关闭本窗口,而不会对应用程序下创建的其他窗口产生影响。
frame.setDefaultCloseOperation(3);
//在panel0面板上放名称
JPanel panel0 = new JPanel();
panel0.add(new JLabel("中国储蓄银行ATM"));
//在panel1面板上放用户名及输入框
JPanel panel1 = new JPanel();
panel1.add(new JLabel("\t用户名:"));
userName = new JTextField(20);
panel1.add(userName);
//在panel2面板上放密码及输入框
JPanel panel2 = new JPanel();
panel2.add(new JLabel("\t 密码: "));
passWord = new JTextField(20);
panel2.add(passWord);
//在panel3面板上放登录按钮以及注册按钮
JPanel panel3 = new JPanel();
login = new JButton(" 登录 ");
register = new JButton(" 注册 ");
panel3.add(login);
panel3.add(register);
//在panel4面板上放确认密码以及输入框
panel4 = new JPanel();
panel4.add(new JLabel("请再次输入密码:"));
passwordCheck = new JTextField(20);
panel4.add(passwordCheck);
//将这些面板统一放到窗口上
frame.add(panel0);
frame.add(panel1);
frame.add(panel2);
frame.add(panel4);
frame.add(panel3);
//展示界面
show();
}
//展示界面
public void show() {
//根据窗口里面的布局及组件的preferredSize来确定frame的最佳大小。
frame.pack();
//展示窗口
frame.setVisible(true);
//隐藏panel4面板
panel4.setVisible(false);
//给login这个按钮添加事件监听接口
login.addActionListener(this);
//给register这个按钮添加事件监听接口
register.addActionListener(this);
//设置窗口的位置 大小
frame.setBounds(500, 500, 350, 250);
//设置布局
frame.setLayout(new FlowLayout());
//居中
frame.setLocationRelativeTo(null);
}
public void ClearInputBox()
{
userName.setText("");
passWord.setText("");
passwordCheck.setText("");
}
//事件触发后,监视器调用接口中的方法
public void actionPerformed(ActionEvent e)
{
//getActionCommand()获取事件发生是一个与事件相关的'命令'字符串
if (e.getActionCommand().equals(" 注册 "))
{
if (!panel4.isVisible())
{
panel4.setVisible(true);
login.setText(" 取消 ");
regirsterable = true;
return;
}
if (regirsterable)
{
if (userName.getText().equals(""))
{
JOptionPane.showMessageDialog(frame, "用户名不能为空");
ClearInputBox();
return;
}
for(int i = 0; i < Test.usersList.size(); i++)
{
if ((Test.usersList.get(i)).id.equals(userName.getText()))
{
JOptionPane.showMessageDialog(frame, "该用户已被注册");
ClearInputBox();
return;
}
}
if (passWord.getText().equals(""))
{
JOptionPane.showMessageDialog(frame, "密码不能为空,请重新输入");
return;
}
if (passwordCheck.getText().equals(passWord.getText()))
{
Account registeraccount = new Account(userName.getText(), passWord.getText(), "0");
JOptionPane.showMessageDialog(frame, "注册成功,请登录");
Test.usersList.add(registeraccount);
Test.usersListUpdate();
return;
}
JOptionPane.showMessageDialog(frame, "两次输入的密码不一致,请重新输入");
passWord.setText("");
passwordCheck.setText("");
return;
}
}
if (e.getActionCommand().equals(" 登录 "))
{
for(int i = 0; i < Test.usersList.size(); i++)
{
if ((Test.usersList.get(i)).id.equals(userName.getText()))
{
if (!passWord.getText().equals((Test.usersList.get(i)).password))
{
JOptionPane.showMessageDialog(frame, "密码错误");
ClearInputBox();
return;
}
Test.currentAccount = Test.usersList.get(i);
Test.file = new File(Test.currentAccount + ".txt");
Test.recordString = new StringBuilder();
Test.menu = new Menu();
frame.setVisible(false);
File records = new File(Test.currentAccount.id + ".txt");
if (!records.exists())
{
try
{
records.createNewFile();
}
catch (Exception var)
{
JOptionPane.showMessageDialog(null, "创建该用户的记录失败");
}
}
Test.file = records;
try
{
fw = new FileReader(Test.file);
}
catch (Exception var)
{
JOptionPane.showMessageDialog(null, "读取记录失败");
}
BufferedReader bfr = new BufferedReader(fw);
String temp;
try
{
while((temp = bfr.readLine()) != null)
{
Test.recordString.append(temp);
}
fw.close();
}
catch (Exception var)
{
System.out.println("读取记录过程中出现错误");
}
return;
}
}
JOptionPane.showMessageDialog(frame, "该用户不存在");
ClearInputBox();
}
if (e.getActionCommand().equals(" 取消 "))
{
ClearInputBox();
panel4.setVisible(false);
login.setText(" 登录 ");
regirsterable = false;
}
}
}
2.实现菜单界面
package ATM_Pakege;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Menu implements ActionListener
{
public JFrame mframe = new JFrame();
public Menu()
{
mframe.setDefaultCloseOperation(3);
JButton inqury = new JButton("查询");
JButton outmoney = new JButton("取款");
JButton transfer = new JButton("转账");
JButton inmoney = new JButton("存款");
JButton changepassword = new JButton("更改密码");
JButton exit = new JButton("退卡");
JPanel mp0 = new JPanel();
mp0.add(new JLabel("选择项目"));
mframe.add(mp0);
JPanel mp1 = new JPanel();
mp1.add(inmoney);
mp1.add(inqury);
mp1.add(outmoney);
mp1.add(transfer);
mp1.add(changepassword);
mp1.add(exit);
mp1.setLayout(new GridLayout(3, 2, 20, 20));
mframe.add(mp1);
mframe.pack();
mframe.setVisible(true);
mframe.setLayout(new FlowLayout());
mframe.setBounds(500, 500, 270, 210);
//居中
mframe.setLocationRelativeTo(null);
inqury.addActionListener(this);
inmoney.addActionListener(this);
outmoney.addActionListener(this);
transfer.addActionListener(this);
changepassword.addActionListener(this);
exit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if (cmd.equals("查询"))
{
new Inquire();
}
else if (cmd.equals("取款"))
{
new OutMoney();
}
else if (cmd.equals("存款"))
{
new InMoney();
}
else if (cmd.equals("转账"))
{
new Transfer();
}
else if (cmd.equals("更改密码"))
{
new ChangePassword();
}
else if (cmd.equals("退卡"))
{
mframe.setVisible(false);
new LoginGui();
JOptionPane.showMessageDialog(null, "请记得取走银行卡");
}
}
}
3.实现菜单中存款功能
package ATM_Pakege;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class InMoney implements ActionListener
{
public JTextField money;
public JFrame iframe = new JFrame("存款");
public JPanel ip0,ip1,ip2,ip3;
public JButton confirm,cancel;
public JLabel yue;
public InMoney()
{
ip0=new JPanel();
ip0.add(new JLabel("账户id:" + Test.currentAccount.id));
ip1 = new JPanel();
yue = new JLabel("账户余额:" + Test.currentAccount.money);
ip1.add(yue);
ip2 = new JPanel();
ip2.add(new JLabel("存款金额:"));
money = new JTextField(20);
ip2.add(money);
ip3 = new JPanel();
confirm = new JButton("确定");
ip3.add(confirm);
cancel = new JButton("返回");
ip3.add(cancel);
iframe.add(ip0);
iframe.add(ip1);
iframe.add(ip2);
iframe.add(confirm);
iframe.add(cancel);
iframe.setLayout(new FlowLayout());
iframe.setVisible(true);
iframe.setBounds(500, 500, 333, 212);
//居中
iframe.setLocationRelativeTo(null);
confirm.addActionListener(this);
cancel.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("确定"))
{
try
{
Test.currentAccount.inMoney(Integer.parseInt(money.getText()));
JOptionPane.showMessageDialog(null, "存款成功");
money.setText("");
yue.setText("账户余额:" + Test.currentAccount.money);
}
catch (ClassCastException e1)
{
JOptionPane.showMessageDialog(null, "输入数据类型错误,请输入整数");
}
catch (Exception e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
else
{
iframe.setVisible(false);
}
}
}
4.实现菜单中查询功能
package ATM_Pakege;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Inquire implements ActionListener
{
public JFrame iframe = new JFrame("查询");
public JTextArea InquireResult;
public JLabel balance;
public Inquire()
{
JPanel panel0 = new JPanel();
panel0.add(new JLabel("账户id:" + Test.currentAccount.id));
JPanel panel1 = new JPanel();
balance = new JLabel("账户余额:" + Test.currentAccount.money);
panel1.add(balance);
JPanel panel2 = new JPanel();
InquireResult = new JTextArea(10, 30);
panel2.add(InquireResult);
JButton confirm = new JButton("查询记录");
confirm.addActionListener(this);
iframe.add(panel0);
iframe.add(panel1);
iframe.add(panel2);
iframe.add(confirm);
iframe.setLayout(new FlowLayout());
iframe.setVisible(true);
iframe.setBounds(500, 500, 350, 300);
//居中
iframe.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e)
{
InquireResult.setText(Test.recordString.toString().replace("元", "元\n").replace("null", ""));
balance.setText("账户余额:" + Test.currentAccount.money);
}
}
5.实现菜单中取款功能
package ATM_Pakege;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class OutMoney implements ActionListener
{
public JTextField money;
public JFrame iframe = new JFrame("取款");
public JPanel ip0 = new JPanel();
public JPanel ip1;
public JPanel ip2;
public JPanel ip3;
public JButton confirm;
public JButton cancel;
public JLabel yue;
public OutMoney()
{
ip0.add(new JLabel("账户id:" + Test.currentAccount.id));
ip1 = new JPanel();
yue = new JLabel("账户余额:" + Test.currentAccount.money);
ip1.add(yue);
ip2 = new JPanel();
ip2.add(new JLabel("取款金额:"));
money = new JTextField(20);
ip2.add(money);
ip3 = new JPanel();
confirm = new JButton("确定");
ip3.add(confirm);
cancel = new JButton("返回");
ip3.add(cancel);
iframe.add(ip0);
iframe.add(ip1);
iframe.add(ip2);
iframe.add(confirm);
iframe.add(cancel);
iframe.setLayout(new FlowLayout());
iframe.setVisible(true);
iframe.setBounds(500, 500, 353, 209);
//居中
iframe.setLocationRelativeTo(null);
confirm.addActionListener(this);
cancel.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("确定"))
{
try
{
Test.currentAccount.outMoney(Integer.parseInt(money.getText()));
JOptionPane.showMessageDialog(null, "取款成功");
money.setText("");
yue.setText("账户余额:" + Test.currentAccount.money);
}
catch (ClassCastException var3)
{
JOptionPane.showMessageDialog(null, "输入数据类型错误,请输入整数");
}
catch (Exception var4)
{
JOptionPane.showMessageDialog(null, var4.getMessage());
}
}
else
{
iframe.setVisible(false);
}
}
}
6.实现菜单中转账功能
package ATM_Pakege;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Transfer implements ActionListener
{
public JTextField money;
public JTextField other;
public JFrame iframe = new JFrame("转账");
public JPanel ip0 = new JPanel();
public JPanel ip1;
public JPanel ip2;
public JPanel ip3;
public JPanel ip4;
public JButton confirm;
public JButton cancel;
public JLabel yue;
public Transfer()
{
ip0.add(new JLabel("账户id:" + Test.currentAccount.id));
ip1 = new JPanel();
yue = new JLabel("账户余额:" + Test.currentAccount.money);
ip1.add(yue);
ip2 = new JPanel();
ip2.add(new JLabel("转账账户id:"));
other = new JTextField(10);
ip2.add(other);
ip4 = new JPanel();
ip4.add(new JLabel("转账金额:"));
money = new JTextField(10);
ip4.add(new JLabel("<html><br/><html>"));//输入空行
ip4.add(money);
ip3 = new JPanel();
confirm = new JButton("确定");
ip3.add(confirm);
cancel = new JButton("返回");
ip3.add(cancel);
iframe.add(ip0);
iframe.add(ip1);
iframe.add(ip2);
iframe.add(ip4);
iframe.add(ip3);
iframe.setLayout(new FlowLayout());
iframe.setVisible(true);
iframe.setBounds(500, 500, 325, 218);
//居中
iframe.setLocationRelativeTo(null);
confirm.addActionListener(this);
cancel.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("确定"))
{
try
{
Test.currentAccount.transfer(Integer.parseInt(money.getText()), other.getText());
yue.setText("账户余额:" + Test.currentAccount.money);
}
catch (Exception var3)
{
JOptionPane.showMessageDialog(null, var3.getMessage());
}
}
else
{
iframe.setVisible(false);
}
}
}
7.实现菜单中更改密码功能
package ATM_Pakege;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChangePassword implements ActionListener {
public JTextField oldPassword;
public JTextField newPassword;
public JTextField checkPassword;
public JFrame iframe = new JFrame("更改密码");
public JPanel ip1;
public JPanel ip2;
public JPanel ip3;
public JPanel ip4;
public JButton confirm;
public JButton cancel;
public ChangePassword()
{
iframe.setDefaultCloseOperation(3);
ip2 = new JPanel();
ip2.add(new JLabel("原密码:"));
oldPassword = new JTextField(20);
ip2.add(new JLabel("<html><br/><html>"));
ip2.add(oldPassword);
ip1 = new JPanel();
ip1.add(new JLabel("新密码:"));
newPassword = new JTextField(20);
ip1.add(new JLabel("<html><br/><html>"));
ip1.add(newPassword);
ip4 = new JPanel();
ip4.add(new JLabel("再次输入新密码:"));
checkPassword = new JTextField(20);
ip4.add(new JLabel("<html><br/><html>"));
ip4.add(checkPassword);
ip3 = new JPanel();
confirm = new JButton("确定");
ip3.add(this.confirm);
cancel = new JButton("返回");
ip3.add(cancel);
iframe.add(ip2);
iframe.add(ip1);
iframe.add(ip4);
iframe.add(ip3);
iframe.add(confirm);
iframe.add(cancel);
iframe.setLayout(new FlowLayout());
iframe.setVisible(true);
iframe.setBounds(500, 500, 417, 211);
//居中
iframe.setLocationRelativeTo(null);
confirm.addActionListener(this);
cancel.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("确定"))
{
if (Test.currentAccount.password.equals(oldPassword.getText()))
{
try
{
if (newPassword.getText().equals(checkPassword.getText()))
{
Test.currentAccount.ChangePassword(newPassword.getText());
JOptionPane.showMessageDialog(null, "更改密码成功");
iframe.setVisible(false);
Test.menu.mframe.setVisible(false);
new LoginGui();
}
else
{
JOptionPane.showMessageDialog(null, "两次输入的密码不一致");
}
}
catch (Exception var3)
{
JOptionPane.showMessageDialog(null, var3.getMessage());
}
}
else
{
JOptionPane.showMessageDialog(null, "原密码错误");
}
}
else
{
iframe.setVisible(false);
}
}
}
7.实现菜单中退卡功能
在菜单界面,当你点到退卡是执行以下代码即可完成退卡操作
if (cmd.equals("退卡"))
{
mframe.setVisible(false);
new LoginGui();
JOptionPane.showMessageDialog(null, "请记得取走银行卡");
}
8.账户管理
package ATM_Pakege;
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
{
if (money > this.money)
{
throw new Exception("余额不足");
}
else if (money < 0)
{
throw new Exception("不能取出负数");
}
else
{
this.formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
this.currentTime = new Date();
String dateString = this.formatter.format(this.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);
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 var4)
{
throw new Exception("写入记录失败");
}
}
public void transfer(int money, String id) throws Exception
{
if (id.equals(Test.currentAccount.id))
{
throw new Exception("不能转给自己");
}
else if (money > this.money)
{
throw new Exception("余额不足");
}
else if (money < 0)
{
throw new Exception("不能转入负数");
}
else
{
for(int i = 0; i < Test.usersList.size(); i++)
{
if ((Test.usersList.get(i)).id.equals(id))
{
Account var10000 = Test.usersList.get(i);
var10000.money += money;
this.money -= money;
FileWriter fw = new FileWriter(Test.file);
this.formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
this.currentTime = new Date();
String dateString = this.formatter.format(this.currentTime);
fw.write(Test.recordString.append(dateString + "\t向" + id + "\t转出" + money + "元\r\n").toString());
fw.close();
try
{
this.fr = new FileReader(id + ".txt");
}
catch (Exception var9)
{
System.out.println("字符流创建失败");
}
BufferedReader bfr = new BufferedReader(this.fr);
String temp;
String temp1;
for(temp = ""; (temp1 = bfr.readLine()) != null; temp = 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("密码不能为空");
}
else
{
this.password = newPassword;
Test.usersListUpdate();
}
}
}
9.测试
package ATM_Pakege;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Test
{
public static List<Account> usersList;
public static Account currentAccount;
public static File file;
public static StringBuilder recordString = new StringBuilder();
public static Menu menu;
public static File usersFile;
static Reader fw;
public static void main(String[] args) throws Exception
{
usersList = new ArrayList();
File users = new File("users.txt");
if (!users.exists())
{
try
{
users.createNewFile();
Writer fw = new FileWriter("users.txt");
fw.write("admin 12345 88888");
fw.flush();
fw.close();
}
catch (Exception e1)
{
JOptionPane.showMessageDialog(null, "创建用户文档失败");
}
}
usersFile = users;
usersListRead();
usersListUpdate();
new LoginGui();
}
public static void usersListRead()
{
try
{
fw = new FileReader("users.txt");
}
catch (Exception e1)
{
System.out.println("字符流创建失败");
}
BufferedReader bfr = new BufferedReader(fw);
String temp;
try
{
System.out.println("开始写入list");
while((temp = bfr.readLine()) != null)
{
String[] tmpstr = new String[5];
tmpstr = temp.split("\\s+");
System.out.println("余额:" + tmpstr[2]);
Account a = new Account(tmpstr[0], tmpstr[1], tmpstr[2]);
usersList.add(a);
System.out.println("读取到" + a.id + ",实例化用户" + a.id);
}
bfr.close();
fw.close();
System.out.println("用户list:" + usersList);
}
catch (Exception e1)
{
System.out.println("读取用户文档失败");
}
}
public static void usersListUpdate()
{
try
{
Writer fw = new FileWriter("users.txt");
StringBuilder tmpstr = new StringBuilder();
for(int i = 0; i < usersList.size(); i++)
{
tmpstr.append((usersList.get(i)).id + " " + (usersList.get(i)).password + " " + (usersList.get(i)).money + "\r\n");
}
fw.write(tmpstr.toString());
fw.flush();
fw.close();
}
catch (Exception e1)
{
e1.printStackTrace();
System.out.println("更新用户失败");
}
}
}
10.效果图(部分)演示