Test 界面:

import java.awt.Menu;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List; import javax.swing.JOptionPane; // JOptionPane 类 方便的弹出要求用户提供值或向其发出消息通知的对话框 import javafx.scene.layout.Pane; // 支持用户界面布局,面板
import jdk.incubator.http.WebSocket.Builder; public class Test{ // 通过静态变量来调用全局
public static List<zhanghu> usersList; // 具体账户(所有) // userslist类型 zhanghu为参数(类) List<zhanghu> 一个容器类泛型
public static zhanghu d_zhanghu; // 登陆的账户
public static File file; // 当前用户的记录文件
public static StringBuilder r_string = new StringBuilder(); // 对缓冲区修改字符串,而不创建新的对象
public static ATM_she_ji.Menu menu; // 静态的菜单界面
public static File usersfile;
public static StringBuilder u_String = new StringBuilder(); static Reader fw; // 用于同步 fw 的操作 public static void main(String[] args){ // throws 不在本方法中进行异常处理
usersList = new ArrayList<zhanghu>(); // 账户表,用数组的形式 File users = new File("users.txt"); //用户文本 if(!users.exists()) { // 若文件存在则为true, isfile 文件存在为false
try {
users.createNewFile(); //创建新的文本
Writer fw = new FileWriter("users.txt"); // 文本写入
fw.write("vera 123456 88888"); // 默认账户
fw.flush(); // 清空缓冲区并完成文件写入操作
fw.close(); }catch(Exception e1) {
JOptionPane.showMessageDialog(null,"创建文档失败"); // 弹出消息对话框
}
}
usersfile = users; // 创建用户文档,存储用户账户,密码,余额信息
usersLRead(); // 读取文档
usersUpdate(); // 文本存储 deng_lu Deng_lu = new deng_lu(); // 账户登陆注册页面 } public static void usersLRead() { // 读取users文档
try {
fw = new FileReader("users.txt"); // 创建字符流
}catch(Exception e) {
System.out.print("字符流创建失败");
}
BufferedReader bfr = new BufferedReader(fw); // 缓冲字符输入流,继承于Reader String temp = "";
try {
//System.out.print("开始写入list");
while((temp = bfr.readLine())!= null) { // 如果取出字符不为null
String[] tmpstr = new String[5];
tmpstr = temp.split("\\s+"); // 分割空格
//System.out.print("余额:"+tmpstr[2]);
zhanghu a = new zhanghu(tmpstr[0],tmpstr[1],tmpstr[2]);
usersList.add(a);
// System.out.print("读取到"+a.id+",实例化用户"+a.id); }
bfr.close(); // 关闭字符输出流
fw.close(); // 关闭文本写入 }catch(Exception e) {
System.out.println("读取文档失败");
} } public static void usersUpdate() { // 用户信息存储
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).mi_ma+" "+usersList.get(i).money+"\r\n");
}
fw.write(tmpstr.toString());
fw.flush();
fw.close();
}catch(Exception e) {
e.printStackTrace();
System.out.println("更新用户失败");
} } }

存钱:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField; public class cun_qian implements ActionListener{
public JTextField money;
public JFrame jfra;
public JPanel i0,i1,i2,i3;
public JButton que_ding,cancel,exit;
public JLabel yu_e;
public cun_qian() { jfra = new JFrame("存款");
i0 = new JPanel();
i0.add(new JLabel("账户id:"+Test.d_zhanghu.id)); i1 = new JPanel();
yu_e = new JLabel("账户余额"+Test.d_zhanghu.money);
i1.add(yu_e); i2 = new JPanel();
i2.add(new JLabel("存款金额:"));
money = new JTextField(20);
i2.add(money); i3 = new JPanel();
que_ding = new JButton("确定");
i3.add(que_ding);
cancel = new JButton("返回");
i3.add(cancel); jfra.add(i0);
jfra.add(i1);
jfra.add(i2);
jfra.add(i3); jfra.add(que_ding);
jfra.add(cancel);
jfra.setLayout(new FlowLayout()); // 流式布局
jfra.setVisible(true); // 窗口可见 jfra.setBounds(200,200,350,150); que_ding.addActionListener(this); // 绑定监听器 cancel.addActionListener(this); } @Override
public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("确定")) {
try {
if(Integer.parseInt(money.getText())% 100 == 0 && Integer.parseInt(money.getText())>=0 ) {
// 调用当前用户的存钱函数
Test.d_zhanghu.cun_qian(Integer.parseInt(money.getText()));
yu_e.setText("账户余额:"+Test.d_zhanghu.money);
JOptionPane.showMessageDialog(null, "存款成功"); // 弹窗
}else {
JOptionPane.showMessageDialog(null, "存款金额须为100的整数倍");
} }catch(ClassCastException e1) {
JOptionPane.showMessageDialog(null, "请输入数字整数"); // frame 窗口
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "输入值必须为数字整数"); // e1.getMessage()获取异常详细消息字符串 }
money.setText(""); }else {
jfra.setVisible(false); // 隐藏窗口
}
} }

取钱:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField; public class qu_qian implements ActionListener{
public JTextField money;
public JFrame jfra;
public JPanel i0,i1,i2,i3;
public JButton que_ding,cancel,exit;
public JLabel yu_e; public qu_qian() { jfra = new JFrame("取款");
i0 = new JPanel();
i0.add(new JLabel("账户id:"+Test.d_zhanghu.id)); i1 = new JPanel();
yu_e = new JLabel("账户余额"+Test.d_zhanghu.money);
i1.add(yu_e); i2 = new JPanel();
i2.add(new JLabel("取款金额:"));
money = new JTextField(20);
i2.add(money); i3 = new JPanel();
que_ding = new JButton("确定");
i3.add(que_ding);
cancel = new JButton("返回");
i3.add(cancel); jfra.add(i0);
jfra.add(i1);
jfra.add(i2);
jfra.add(i3); jfra.add(que_ding);
jfra.add(cancel);
jfra.setLayout(new FlowLayout()); // 流式布局
jfra.setVisible(true); // 窗口可见 jfra.setBounds(200,200,350,150); que_ding.addActionListener(this); // 绑定监听器 cancel.addActionListener(this); } @Override
public void actionPerformed(ActionEvent e) { // 按下键后。。
String i = e.getActionCommand(); // i 赋值为点击的按键的值
if(e.getActionCommand().equals("确定")) {
try {
if(Integer.parseInt(money.getText())% 100 == 0 && Integer.parseInt(money.getText())>=0 ) {
if(Integer.parseInt(money.getText()) > Test.d_zhanghu.money) { JOptionPane.showMessageDialog(null, "余额不足"); }else {
// 调用当前用户的存钱函数
Test.d_zhanghu.qu_qian(Integer.parseInt(money.getText()));
yu_e.setText("账户余额:"+Test.d_zhanghu.money);
JOptionPane.showMessageDialog(null, "取款成功"); // 弹窗
}
}else{
JOptionPane.showMessageDialog(null, "取款金额须为100的正整数倍");
} }catch(ClassCastException e1) { // 捕获 数据类型错误
JOptionPane.showMessageDialog(null, "请输入数字整数"); // frame 窗口
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "输入值必须为数字整数"); // e1.getMessage()获取异常详细消息字符串
// qu_qian 里抛出的
}
money.setText("");
}else {
jfra.setVisible(false); // 隐藏窗口
} } }

账户:

public class zhanghu {

    int money;
String id;
String mi_ma; public zhanghu(String id,String mi_ma, String money) { // 注册和读取用户时用
this.id = id;
this.mi_ma = mi_ma;
this.money = Integer.parseInt(money); // money 返回int值
} // 抛出异常由相关的界面类弹窗处理异常
// 取钱
public void qu_qian(int money){ this.money -=money;
Test.usersUpdate(); // 更新用户文档
} // 存钱
public void cun_qian(int money){ this.money += money; Test.usersUpdate(); // 更新用户信息 } }

菜单:

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;// 动作监听器 import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Menu implements ActionListener{ public JFrame fra; // 窗口
private JPanel m; // 布局
private JTextField mi_ma,cha_mi_ma;
private JButton qu_qian; // 操作按键
private JButton cun_qian; public Menu() { fra = new JFrame("操作菜单");
fra.setResizable(false); // 设置不可放大
fra.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 静态常量,退出窗体
JButton cao_zuo = new JButton("请选择操作项目 ");
JButton cun_qian = new JButton("存 款");
JButton qu_qian = new JButton("取 款");
JButton exit = new JButton("退 卡");
//m = new JPanel();
//cao_zuo = new JLabel("操作项目:")
//m0.add(cao_zuo);
//fra.add(m0); m = new JPanel();
m.add(cao_zuo);
m.add(qu_qian);
m.add(cun_qian);
m.add(exit);
m.setLayout(new GridLayout(4,1,30,30)); // 网格布局管理器,组件的行列分布,20为组件间的横纵间距 fra.add(m);
fra.pack(); // 自动适应大小
fra.setVisible(true); // 图形界面可见
fra.setLayout(new FlowLayout()); // 布局管理器,流式布局
fra.setBounds(200,200,350,280);
qu_qian.addActionListener(this); // 增加监听(绑定监听器)
cun_qian.addActionListener(this);
exit.addActionListener(this); } @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String i = e.getActionCommand(); // i 赋值为点击的按键的值
if(i.equals("取 款")) {
qu_qian qu = new qu_qian();
}else if(i.equals("存 款")){
cun_qian cun = new cun_qian(); }else if(i.equals("退 卡")) {
fra.setVisible(false); // 隐藏页面 deng_lu Deng_lu = new deng_lu();
JOptionPane.showMessageDialog(null, "请记得取走您的银行卡");
}
}
}

登陆/ 注册:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
//import java.io.Reader; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
//import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants; public class deng_lu implements ActionListener{ private JFrame frame;
private JPanel p0,p1,p2,p3,p4; // 布局
private JTextField userName;
private JTextField mi_ma,cha_mi_ma;
private JButton deng;
private JButton zhu_ce;
// private Reader fw; //
Boolean Zhu_ce = true; // 控制是否注册的变量 public deng_lu() {
frame = new JFrame("登陆ATM");
frame.setResizable(false); // 设置为不能放大
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 右上角x 退出
p0 = new JPanel();
p0.add(new JLabel("银行储蓄ATM")); // 标签
frame.add(p0); p1 = new JPanel();
p1.add(new JLabel("\t用 户 名:"));
userName = new JTextField(20);
p1.add(userName); p2 = new JPanel();
p2.add(new JLabel("\t 密 码:"));
mi_ma = new JTextField(20);
//JPasswordField mi_ma = new JPasswordField(20) ;
//mi_ma.setEchoChar('*') ;// 设置回显
//String mi = String.valueOf(mi_ma.getPassword());
p2.add(mi_ma); p3 = new JPanel(); deng = new JButton(" 登陆 ");
zhu_ce = new JButton(" 注册 ");
p3.add(deng);
p3.add(zhu_ce); p4 = new JPanel();
p4.add(new JLabel("确认密码:"));
cha_mi_ma = new JTextField(20);
p4.add(cha_mi_ma); frame.add(p1);
frame.add(p2); frame.add(p4); // 确认密码框 ,放在注册键的前面
frame.add(p3); frame.pack();
frame.setVisible(true);
p4.setVisible(false); deng.addActionListener(this); // 添加监听
zhu_ce.addActionListener(this);
frame.setBounds(200,200,350,250); // 设置大小
frame.setLayout(new FlowLayout()); // 流式布局 } @Override
public void actionPerformed(ActionEvent e) { // 动作
// TODO Auto-generated method stub
if(e.getActionCommand().equals(" 注册 ")) { // 如果点击注册
if(p4.isVisible() == false) { // 判断部件是否可见
p4.setVisible(true);
deng.setText(" 取消 "); // 将登陆改为取消,同时触发取消监听器
Zhu_ce = true;
return;
}
if(Zhu_ce == true) {
if(userName.getText().equals("")) { // 如果userName文本为空
JOptionPane.showMessageDialog(frame, "用户名不能为空"); // 弹窗
return;
}else {
for (int i = 0;i<Test.usersList.size();i++) {
if(Test.usersList.get(i).id.equals(userName.getText())) {
JOptionPane.showMessageDialog(frame, "该用户已被注册");
userName.setText(""); // 清空输入框
mi_ma.setText("");
cha_mi_ma.setText("");
return; // 若用户名同名,结束方法
}
}
}
if(mi_ma.getText().length()!=6) { //&&mi_ma.getText().equals("")
JOptionPane.showMessageDialog(frame, "请输入六位密码"); // 弹窗
return;
}else {
if(cha_mi_ma.getText().equals(mi_ma.getText())) {
// 注册账户,账户实例化
zhanghu zhang_zhu_ce = new zhanghu(userName.getText(),mi_ma.getText(),"0");
JOptionPane.showMessageDialog(frame,"注册成功,请登陆");
Test.usersList.add(zhang_zhu_ce); // 加入Test类的静态用户list
Test.usersUpdate();
return; }else {
JOptionPane.showMessageDialog(frame, "两次输入的密码不一致,请重新输入");
return;
}
}
}
}
if(e.getActionCommand().equals(" 登陆 ")) {
for(int i = 0;i<Test.usersList.size();i++) {
if(Test.usersList.get(i).id.equals(userName.getText())) { if(mi_ma.getText().equals(Test.usersList.get(i).mi_ma)) {
JOptionPane.showMessageDialog(frame, "登陆成功");
Test.d_zhanghu = Test.usersList.get(i);
// 将list中符合登陆输入密码的账户类设为当前Test类中的静态的“当前类”
Test.file = new File(Test.d_zhanghu+".text"); // 在文件中添加新的txt Menu menu = new Menu(); // 实例化菜单窗口 Test.menu = menu;
frame.setVisible(false); // 隐藏登陆窗口
return; }else {
JOptionPane.showMessageDialog(frame, "密码错误");
cha_mi_ma.setText(""); // 清空
return;
}
}
}
JOptionPane.showMessageDialog(frame, "该用户不存在"); }
if(e.getActionCommand().equals(" 取消 ")) {
p4.setVisible(false); // 隐藏窗口
deng.setText(" 登陆 ");
Zhu_ce = false; // 不可注册
}
} }

参考于:https://blog.csdn.net/qq_37724011/article/details/78682028

java_ATM_GUI的更多相关文章

随机推荐

  1. Java拓展接口-default关键词

    Java接口在使用过程中有两点规定: 1.接口中只能有定义方法名.方法返回类型,不能有方法的实现. 2.实现接口的类,必须实现接口中所有的方法. 例如下面的例子: //定义接口 public inte ...

  2. 不同系统下的字长------typedef的意义

    int的字节长度是由CPU和操作系统编译器共同决定的, 一般情况下,主要是由操作系统决定,比如,你在64位AMD的机器上安装的是32位操作系统,那么,int默认是32位的:如果是64位操作系统,64位 ...

  3. spring boot简单的小demo(适合于初学者)

    import com.example.demo2.com.example.dao.ShopDao; import com.example.demo2.com.example.entity.Shops; ...

  4. MT【281】最大值函数

    已知正系数二次函数$ax^2+bx+c=0$有实数根,证明:$\max\{a,b,c\}\ge\dfrac{4}{9}(a+b+c)$ 证明:$\max\{a,b,c\}=\dfrac{a+c+|a- ...

  5. 当使用makemigrations时报错No changes detected

    在修改了models.py后,有些用户会喜欢用python manage.py makemigrations生成对应的py代码. 但有时执行python manage.py makemigration ...

  6. 使用kqueue的str_cli函数

    void str_cli(FILE *fp, int sockfd) { , isfile; char buf[MAXLINE]; ]; struct timespec ts; struct stat ...

  7. Linux-Centos 虚拟机安装

    Centos安装方法 第一步:一般只有第一项和第三项有用 其余的没啥卵用 第二步:提示检查镜像完整性,这里我们不要检查  选 skip 继续(之前尝试选择OK,最后安装失败了,也不想找原因了) 第三步 ...

  8. UE4材质特别属生记录

    tangent space normal 切线空间法线 材质默认使用的切线空间法线,可切换为对象空间法线(彩色贴图) Separate Translucency 单独半透明 允许半透明被渲染到一个单独 ...

  9. Stm32 GPIO复习

    地点:中图四楼. 1.七个寄存器 配置寄存器:GPIOx_CPL.GPIOx_CPH; 数据寄存器:GPIOx_IDR.GPIOx_ODR: 置位/复位寄存器:GPIOx_BSRR 复位寄存器:GPI ...

  10. Django之form表单

    https://www.cnblogs.com/liwenzhou/p/8747872.html 1. form组件的介绍 之前web开发的模式,以注册为例: 1. 要有一个注册的页面,页面里面要有f ...