JAVA课程设计:ATM用户界面实现
最近一段时间因课程要求做了java的课程设计,所做的是ATM用户界面实现,挺简单的一个小设计,做之前有参考网上其他人的 大体思路,于是做完之后就打算把它上传到网上,以方便之后有需要的人可以做个简单参考。
本ATM柜员机的主要功能如下所述:
要求使用图形用户界面。当输入给定的卡号和密码,初始卡号为286080822(52554917)和密码为654321(123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:
查询余额:初始余额为10000元
ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。
ATM存款:不能出现负存款。
修改密码:只有旧密码正确,新密码符合要求,密码只能为六位数且两次输入相同的情况下才可以成功修改密码。
程序主要使用的是文件的读取存储来储存账户和操作记录,swing组件实现界面,为了图简便没有使用数据库(菜鸡一个,第一次写博客)
以下是程序代码:
(简单的上传,菜鸡的源代码其中很多注释掉了的,也没去删除修改,相信都能看懂,虽然这样可能有点乱-.-)
1.Test类(测试类,用于启动程序,以及写入初始用户,已注释掉部分(这个是可以直接在相应文件写入的,开始打算的是在登录界面创建一个注册选项,后觉得既然是为了实现ATM,不需要这个功能,就放弃了))
import java.io.*;
import java.util.*;
public class Test {
static File userfile; //储存用户账户信息的文件
static FileReader fr;
static FileWriter fw;
static List<Account> user; //储存账户的集合
public static String[] str,strs,len;
//static String strs = new String();
public static void main(String args[]) {
user = new ArrayList<>();
System.out.println(user.isEmpty());
/*userfile = new File("userfile.txt");
if(!userfile.exists()) {
try {
userfile.createNewFile();
fw = new FileWriter("userfile.txt");
str = new String[5];
users();
fw.write(str[0]);
//fw.write("\n");
fw.write(str[1]);
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
login();
new Login();
}
public static void login() {
try {
fr = new FileReader("userfile.txt");
BufferedReader bfr = new BufferedReader(fr);
len = new String[20];
for(int i = 0;i<5;i++) {
while((len[i]=bfr.readLine())=="\n") {
break;
//String[] ms = new String[5];
//ms = len.split("\\s");
//System.out.println(ms[0]);
/*ms = len.split("\\s+");
Account a = new Account(ms[0], ms[1], ms[2]);
user.add(a);*/
}
}
System.out.println(len[1]);
strs = new String[20];
for(int i = 0;i<2;i++) {
strs = len[i].split("\\s");
Account a = new Account(strs[0],strs[1], strs[2]);
user.add(a);
System.out.println(user.get(i).passwd);
}
//System.out.println(user.size());
//System.out.println(user.get(0).money);type name = new type();
fr.close();
bfr.close();
//System.out.printf(strs[0]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*public static void users() {
str[0] = "286080822 1998 10000\n";
str[1] = "52554917 1999 10000\n";
}*/
}
2.Account类(账户类,包含了所有功能的实现方法)
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Account {
String id;
String passwd,money;
static String user;
//SimpleDateFormat sdate;
//Date currentTime;
public Account(String id,String passwd,String money) {
this.id = id;
this.passwd = passwd;
this.money = money;
}
//实现取钱的方法,会在取钱界面事件监听中被调用,后面几个方法与之类似
public static void outmoney(int mymoney)throws Exception{
int lmoney = Integer.parseInt(Login.mymoney);
String uu = Login.myid+" "+Login.mypasswd+" "+Login.mymoney;
//System.out.println(uu);
lmoney-=mymoney;
Login.mymoney =String.valueOf(lmoney);
for(int i = 0;i<2;i++) {
if(uu.equals(Test.len[i])) {
user = Login.myid+" "+Login.mypasswd+" "+Login.mymoney;
System.out.println(user);
Test.len[i] = user;
Writer fw = new FileWriter("userfile.txt");
//Reader fr = new FileReader("userfile.txt");
for(int j = 0;j<2;j++) {
fw.write(Test.len[i]+"\n");
}
fw.flush();
fw.close();
//Test.user.get(i).money = Login.mymoney;
//throw new Exception("取钱成功");
}
}
}
public static void inmoney(int money)throws Exception{
int lmoney = Integer.parseInt(Login.mymoney);
String uu = Login.myid+" "+Login.mypasswd+" "+Login.mymoney;
//System.out.println(uu);
lmoney+=money;
Login.mymoney =String.valueOf(lmoney);
for(int i = 0;i<2;i++) {
if(uu.equals(Test.len[i])) {
user = Login.myid+" "+Login.mypasswd+" "+Login.mymoney;
System.out.println(user);
Test.len[i] = user;
break;
}
}
Writer fw = new FileWriter("userfile.txt");
for(int i = 0;i<2;i++) {
fw.write(Test.len[i]+"\n");
}
fw.flush();
fw.close();
}
public static String cha() throws Exception{
FileReader fr = new FileReader("record"+Login.myid+".txt");
BufferedReader bfr = new BufferedReader(fr);
String record,fb;
StringBuffer sb = new StringBuffer();
while((record=bfr.readLine())!=null) {
System.out.println(record);
sb.append(record+"\n");
}
fb = sb.toString();
return fb;
}
public static boolean cpasswd(String pwd,String npwd,String spwd) throws Exception {
boolean flag = false;
if(pwd.equals(Login.mypasswd)) {
if(npwd.length()==6) {
if(npwd.equals(spwd)) {
String uu = Login.myid+" "+Login.mypasswd+" "+Login.mymoney;
Login.mypasswd = npwd;
System.out.println(Login.mypasswd);
for(int i = 0;i<2;i++) {
if(uu.equals(Test.len[i])) {
user = Login.myid+" "+Login.mypasswd+" "+Login.mymoney;
System.out.println(user);
Test.len[i] = user;
System.out.println(Test.len[i]);
break;
}
}
Writer fw = new FileWriter("userfile.txt");
for(int i = 0;i<2;i++) {
fw.write(Test.len[i]+"\n");
}
fw.flush();
fw.close();
flag = true;
}else {
flag = false;
}
}else {
flag = false;
}
}else {
flag = false;
}
return flag;
}
}
3.Login类(登录界面的实现,以及实现登录的方法)
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login extends JFrame implements ActionListener{
private JPanel pan1,pan2,pan3,pan4;
private JTextField username,password;
private JButton login;
static String myid,mypasswd,mymoney;
//static int mymoney;
//public static Reader fw;
public Login() {
this.setTitle("登录ATM");
this.setSize(500, 300);
this.setLocation(500, 200);
this.setResizable(false);
//this.setVisible(true);
this.setLayout(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pan1 = new JPanel();
pan1.setBackground(new Color(255,239,213));
pan1.setBounds(150,30,200,30);
this.add(pan1);
pan1.add(new JLabel("袁氏高级储蓄银行"));
pan2 = new JPanel();
pan2.setBounds(80, 70, 300, 30);
this.add(pan2);
pan2.add(new JLabel("账户:"));
username = new JTextField(20);
pan2.add(username);
pan3 = new JPanel();
pan3.setBounds(80, 100, 300, 30);
this.add(pan3);
pan3.add(new JLabel("密码:"));
password = new JPasswordField(20);
pan3.add(password);
login = new JButton("登录");
//zhuce = new JButton("注册");
login.addActionListener(this);
pan4 = new JPanel();
pan4.setBounds(100, 140, 300, 36);
this.add(pan4);
pan4.add(login);
//pan4.add(zhuce);
//setVisible(false);
setVisible(true);
}
public static String getid() {
return myid;
}
public static String getmoney() {
return mymoney;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if(source==login) {
int size =Test.user.size()-1;
System.out.println(size);
for(int i = 0;i<Test.user.size();i++) {
if(username.getText().toString().equals(Test.user.get(i).id)) {
if(password.getText().toString().equals(Test.user.get(i).passwd)) {
myid = Test.user.get(i).id;
mypasswd = Test.user.get(i).passwd;
mymoney = Test.user.get(i).money;
System.out.println(mypasswd);
this.dispose();
new Menu();
break;
}else if(password.getText().toString().equals("")){
JOptionPane.showMessageDialog(this, "密码不能为空");
break;
}else {
JOptionPane.showMessageDialog(this, "密码错误");
break;
}
}else if(username.getText().toString().equals("")&&i==size){
JOptionPane.showMessageDialog(this, "账号不能为空");
break;
}else if(password.getText().toString().equals("")&&i==size) {
JOptionPane.showMessageDialog(this, "密码不能为空");
break;
}else if(i==size) {
JOptionPane.showMessageDialog(this, "账号或密码错误");
break;
}
}
}
}
/*public static void main(String args[]) {
new Login();
}*/
}
4.Menu类(主菜单界面)
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class Menu extends JFrame implements ActionListener{
private JPanel menu1,menu2;
private JButton cun,qu,cha,zhuan,cpasswd,exit,back;
private JDialog tip;
private JLabel tips;
public Menu() {
this.setTitle("袁氏高级储蓄银行");
this.setSize(500, 300);
this.setLocation(500, 200);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
menu1 = new JPanel();
menu1.setBounds(0, 0, 500, 30);
menu1.setBackground(new Color(255,222,173));
menu1.add(new JLabel("请选择需要进行的业务"));
this.add(menu1);
menu2 = new JPanel();
menu2.setBounds(0, 30, 500, 250);
menu2.setBackground(new Color(255,239,213));
menu2.setLayout(null);
this.add(menu2);
cun = new JButton("存款");
qu = new JButton("取款");
cha = new JButton("查询");
zhuan = new JButton("转账");
cpasswd = new JButton("更改密码");
exit = new JButton("退出");
back = new JButton("返回");
menu2.add(cun);
menu2.add(qu);
menu2.add(cha);
menu2.add(zhuan);
menu2.add(cpasswd);
menu2.add(exit);
cun.setBounds(25,30,100,30);
qu.setBounds(360,30,100,30);
cha.setBounds(25,90,100,30);
zhuan.setBounds(360,90,100,30);
cpasswd.setBounds(25,150,100,30);
exit.setBounds(360,150,100,30);
cun.addActionListener(this);
qu.addActionListener(this);
cha.addActionListener(this);
zhuan.addActionListener(this);
cpasswd.addActionListener(this);
exit.addActionListener(this);
back.addActionListener(this);
tips = new JLabel("私人银行,无法转账");
tips.setBounds(90, 20, 150, 30);
back.setBounds(105, 60, 80, 30);
tip = new JDialog(this, "转账", true);
tip.setSize(300,150);
tip.setLayout(null);
tip.setLocation(600,250);
tip.add(tips);
tip.add(back);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if(source==cun) {
this.dispose();
new Cun();
}else if(source==qu) {
this.dispose();
new Qu();
}else if(source==cha) {
this.dispose();
try {
new Cha();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(source==zhuan) {
tip.setVisible(true);
}else if(source==cpasswd) {
this.dispose();
new Cpasswd();
}else if(source==exit) {
Test.login();
this.dispose();
new Login();
JOptionPane.showMessageDialog(null,"请记得取走你的银行卡");
}else if(source==back) {
tip.dispose();
}
}
/*public static void main(String args[]) {
new Menu();
}*/
}
5.Cha类(查询界面)
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
/*
* 查询类,实现查询界面,当被主界面按钮调用时即显示存取操作记录
*/
public class Cha extends JFrame implements ActionListener{
private JPanel cha1,cha2,cha4;
private JScrollPane cha3;
private JTextArea cha;
private JButton back;
//static String reccord;
public Cha() throws Exception{
this.setTitle("查询");
this.setSize(500, 300);
this.setLocation(500, 200);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
cha1 = new JPanel();
cha1.setBounds(0, 20, 500, 30);
cha1.setLayout(new FlowLayout(FlowLayout.LEFT,20,0));
cha1.add(new JLabel("账户:"+Login.myid));
this.add(cha1);
cha2 = new JPanel();
cha2.setBounds(0, 50, 500, 30);
cha2.setLayout(new FlowLayout(FlowLayout.LEFT,20,0));
cha2.add(new JLabel("余额:"+Login.mymoney));
this.add(cha2);
cha = new JTextArea("用户操作记录如下:\n"+Account.cha());
cha3 = new JScrollPane(cha);
cha3.setBounds(0, 80, 500, 130);
cha.setBackground(new Color(255,250,250));
cha.setEditable(false);
this.add(cha3);
back = new JButton("返回");
back.addActionListener(this);
cha4 = new JPanel();
cha4.setBounds(0,215,500,50);
cha4.add(back);
this.add(cha4);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==back) {
this.dispose();
new Menu();
}
}
/*public static void main(String args[]) {
new Cha();
}*/
}
6.Cun类(存钱界面)
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import javax.swing.*;
public class Cun extends JFrame implements ActionListener{
private JPanel cun1,cun2,cun3,cun4;
private JTextField cun;
private JButton sure,back;
public Cun() {
this.setTitle("存款");
this.setSize(500, 300);
this.setLocation(500, 200);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
cun1 = new JPanel();
cun1.setBounds(0, 20, 500, 30);
cun1.setLayout(new FlowLayout(FlowLayout.LEFT,20,0));
cun1.add(new JLabel("账户:"+Login.myid));
this.add(cun1);
cun2 = new JPanel();
cun2.setBounds(0, 50, 500, 30);
cun2.setLayout(new FlowLayout(FlowLayout.LEFT,20,0));
cun2.add(new JLabel("余额:"+Login.mymoney));
this.add(cun2);
cun3 = new JPanel();
cun3.setBounds(0, 80, 500, 30);
cun3.add(new JLabel("存款金额:"));
cun = new JTextField(20);
cun.setDocument(new NumLimit());
cun3.add(cun);
this.add(cun3);
sure = new JButton("确定");
back = new JButton("返回");
sure.addActionListener(this);
back.addActionListener(this);
cun4 = new JPanel();
cun4.setBounds(0,130,500,50);
cun4.add(sure);
cun4.add(back);
this.add(cun4);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if(source==back){
this.dispose();
new Menu();
}else if(source==sure) {
String c = cun.getText().toString();
int money = Integer.parseInt(c);
System.out.println(c+money);
if(money%100==0) {
try {
Account.inmoney(money);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(this, "存钱成功");
try {
Date currenttime = new Date();//获取当前时间
//将此次操作记录写入记录文件
Writer fw = new FileWriter("record"+Login.myid+".txt",true);
fw.write("用户在"+currenttime+"存入"+money+"元"+"\n");
fw.flush();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
cun.removeAll();
this.dispose();
new Menu();
}else {
JOptionPane.showMessageDialog(this, "存入金额必须是整百数");
}
}
}
/*public static void main(String args[]) {
new Cun();
}*/
}
7.Qu类(取钱界面)
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Qu extends JFrame implements ActionListener{
private JPanel qu1,qu2,qu3,qu4;
private JTextField qu;
private JButton sure,back;
//private Date currentdate;
//int money;
public Qu() {
this.setTitle("取款");
this.setSize(500, 300);
this.setLocation(500, 200);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
qu1 = new JPanel();
qu1.setBounds(0, 20, 500, 30);
qu1.setLayout(new FlowLayout(FlowLayout.LEFT,20,0));
qu1.add(new JLabel("账户:"+Login.getid()));//erro 从这里开始
this.add(qu1);
qu2 = new JPanel();
qu2.setBounds(0, 50, 500, 30);
qu2.setLayout(new FlowLayout(FlowLayout.LEFT,20,0));
qu2.add(new JLabel("余额:"+Login.getmoney()));
this.add(qu2);
qu3 = new JPanel();
qu3.setBounds(0, 80, 500, 30);
qu3.add(new JLabel("取款金额:"));
qu = new JTextField(20);
qu.setDocument(new NumLimit());
qu3.add(qu);
this.add(qu3);
sure = new JButton("确定");
back = new JButton("返回");
back.addActionListener(this);
sure.addActionListener(this);
qu4 = new JPanel();
qu4.setBounds(0,130,500,50);
qu4.add(sure);
qu4.add(back);
this.add(qu4);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if(source==back) {
this.dispose();
new Menu();
}else if(source==sure) {
String m=qu.getText().toString();
int money=Integer.parseInt(m);
int lmoney = Integer.parseInt(Login.mymoney);
if(money<100) {
JOptionPane.showMessageDialog(this, "取款金额不能小于100");
}else if(money>5000) {
JOptionPane.showMessageDialog(this, "取款金额不能超过5000");
}else if(money>lmoney) {
JOptionPane.showMessageDialog(this, "余额不足");
}else if(money%100==0){
try {
Account.outmoney(money);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(this, "取款成功");
try {
Date currenttime = new Date();
Writer fw = new FileWriter("record"+Login.myid+".txt",true);
fw.write("用户在"+currenttime+"取出"+money+"元"+"\n");
fw.flush();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
qu.removeAll();
this.dispose();
new Menu();
}else {
JOptionPane.showMessageDialog(this, "取款金额必须是整百数");
}
}
}
/*public static void main(String args[]) {
new Qu();
}*/
}
8.Cpasswd类(改密界面)
import java.awt.Color;
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.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Cpasswd extends JFrame implements ActionListener{
private JPanel pan1,pan2,pan3,pan4,pan5;
private JTextField pwd1,pwd2,pwd3;
private JButton sure,back;
String pwd,npwd,spwd;
public Cpasswd() {
this.setTitle("密码更改");
this.setSize(500, 300);
this.setLocation(500, 200);
this.setResizable(false);
//this.setVisible(true);
this.setLayout(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pan1 = new JPanel();
pan1.setBounds(80, 20, 300, 30);
this.add(pan1);
pan1.add(new JLabel("原密码:"));
pwd1 = new JPasswordField(20);
pan1.add(pwd1);
pan2 = new JPanel();
pan2.setBounds(80, 60, 300, 30);
this.add(pan2);
pan2.add(new JLabel("新密码:"));
pwd2 = new JPasswordField(20);
pan2.add(pwd2);
pan3 = new JPanel();
pan3.setBounds(75, 100, 300, 30);
this.add(pan3);
pan3.add(new JLabel("确认密码:"));
pwd3 = new JPasswordField(20);
pan3.add(pwd3);
sure = new JButton("确认");
back = new JButton("返回");
sure.addActionListener(this);
back.addActionListener(this);
pan4 = new JPanel();
pan4.setBounds(100, 160, 300, 36);
this.add(pan4);
pan4.add(sure);
pan4.add(back);
pan5 = new JPanel();
pan5.setBounds(100,200,300,36);
this.add(pan5);
pan5.add(new JLabel("(提示:密码只能为六位数)"));
//setVisible(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
if(source==back) {
this.dispose();
new Menu();
}else if(source==sure) {
pwd = pwd1.getText().toString();
npwd = pwd2.getText().toString();
spwd = pwd3.getText().toString();
try {
boolean flag = Account.cpasswd(pwd, npwd, spwd);
if(flag) {
JOptionPane.showMessageDialog(this, "密码修改成功,请重新登录");
Test.user.clear();
this.dispose();
Test.login();
new Login();
}else {
JOptionPane.showMessageDialog(this, "密码修改失败");
this.dispose();
new Menu();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/*public static void main(String args[]) {
new Cpasswd();
}*/
}
9.NumLimit类(限制输入只能为数字,无需知道如何实现,copy下来即可)
import javax.swing.text.*;
public class NumLimit extends PlainDocument {
public NumLimit() {
super();
}
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
// TODO Auto-generated method stub
if(str==null) {
return;
}
char[] s = str.toCharArray();
int length = 0;
//过滤非数字
for (int i = 0; i < s.length; i++) {
if ((s[i] >= '0') && (s[i] <= '9')) {
s[length++] = s[i];
}
super.insertString(offs, new String(s, 0, length), a);
}
}
}
程序会涉及到两个文件:
userfile.txt(用户账户信息文件)
record286080822.txt(用户操作记录文件(文件名为record+账号))
可以自行添加到项目根目录中,以防因为文件发生错误
实现后的界面(会看到转账这个选项,实际上并未实现,楼主强迫症,为了保持对称美观留下来了-.-):