提交 57809ca5 编写于 作者: 朽木冰天's avatar 朽木冰天

init

上级
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/jxl.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ATM柜员机线上系统</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
# Java大作业-ATM柜员机线上系统
## 简介
此程序是本人大一时期的Java大作业,初学Java一学期后所编写的一个Java图形界面程序。作业题目是要求做一个ATM柜员机模拟程序,但本人考虑到这仅仅是做一个软件程序无需搭配硬件等,故将其做成一个ATM柜员机线上系统,使用账号及密码登录,亦可进行注册等。
本程序使用Java编写,用到了Java Swing,拥有图形界面,点击首页的“更换外观”按钮可切换不同风格的外观,程序分为两个模式,用户模式和系统管理模式。用户可进行注册和登录,进入用户模式,功能包括:查看个人信息、存款和取款、转账、贷款和还款、查看交易记录、修改密码等;管理员可输入指定的账号密码登录,进入系统管理模式,功能包括:用户管理、查看历史交易记录等。
本程序的数据存储使用文件存储,程序检错能力强,功能颇多(还可将交易记录导出为excel文件哦),界面工整好看(每个组件都经过细心调整)。但由于是大一时期初学Java后所写的程序,部分代码可能有些繁杂及不合理之处(纯从零开始写的,且为了界面美观,所以有大部分代码都在调整界面组件)。
程序源代码及程序设计说明书可在此下载,供各位需要的人学习参考。
文件已添加
文件已添加
文件已添加
此差异已折叠。
package atm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HomePage { //主页类
static JFrame mainJFrame=new JFrame("ATM柜员机线上系统");
static Container con=mainJFrame.getContentPane();
static boolean flag; //是否为管理员
static int appearance=0; //当前外观
static JLabel lb_appearance=new JLabel("<html>当前外观:<br>默认</html>"); //当前外观
public static void main(String[] args) {
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainJFrame.setResizable(false);
con.setLayout(null);
welcomePage();
}
public static void welcomePage() { //欢迎界面
mainJFrame.setSize(550,400);
mainJFrame.setLocationRelativeTo(null);
JPanel pn=new JPanel();
pn.setSize(550,400);
pn.setLayout(null);
JLabel lb1=new JLabel("欢迎使用"),lb2=new JLabel("ATM柜员机线上系统");
JButton bt1=new JButton("用户模式"),bt2=new JButton("系统管理模式"),bt_changeAppearance=new JButton("更换外观",new ImageIcon("image/更换外观.png"));
lb1.setFont(new Font("黑体",0,35));
lb1.setBounds(200,30,150,100);
lb2.setFont(new Font("黑体",0,35));
lb2.setBounds(125,100,300,80);
bt1.setFont(new Font("黑体",0,22));
bt1.setBounds(70,210,170,70);
bt1.setCursor(new Cursor(Cursor.HAND_CURSOR));
lb_appearance.setFont(new Font("黑体",0,17));
lb_appearance.setBounds(5,5,150,40);
bt2.setFont(new Font("黑体",0,22));
bt2.setBounds(310,210,170,70);
bt2.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt_changeAppearance.setFont(new Font("黑体",0,17));
bt_changeAppearance.setBounds(420,10,130,25);
bt_changeAppearance.setContentAreaFilled(false);
bt_changeAppearance.setBorderPainted(false);
bt_changeAppearance.setCursor(new Cursor(Cursor.HAND_CURSOR));
pn.add(lb1);
pn.add(lb2);
pn.add(lb_appearance);
pn.add(bt1);
pn.add(bt2);
pn.add(bt_changeAppearance);
con.add(pn);
mainJFrame.setVisible(true);
bt1.addActionListener(new ActionListener() { //进入用户登录界面
public void actionPerformed(ActionEvent e) {
flag=false;
con.remove(pn);
mainJFrame.repaint();
loginPage();
mainJFrame.validate();
}
});
bt2.addActionListener(new ActionListener() { //进入管理员登录界面
public void actionPerformed(ActionEvent e) {
flag=true;
con.remove(pn);
mainJFrame.repaint();
loginPage();
mainJFrame.validate();
}
});
bt_changeAppearance.addMouseListener(new MouseListener() { //更换整体界面外观
public void mouseEntered(MouseEvent arg0) {
bt_changeAppearance.setForeground(Color.blue);
}
public void mouseExited(MouseEvent arg0) {
bt_changeAppearance.setForeground(null);
}
public void mouseClicked(MouseEvent arg0) {
try {
appearance++;
String lookAndFeel=null;
switch(appearance) {
case 1: //Windows风格
lookAndFeel="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
lb_appearance.setText("<html>当前外观:<br>Windows</html>");
break;
case 2: //Nimbus风格
lookAndFeel="com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
lb_appearance.setText("<html>当前外观:<br>Nimbus</html>");
break;
case 3: //Windows Classic风格
lookAndFeel="com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
lb_appearance.setText("<html>当前外观:<br>Windows Classic</html>");
break;
case 4://Motif风格
lookAndFeel="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
lb_appearance.setText("<html>当前外观:<br>Motif</html>");
break;
case 5: //默认风格
lookAndFeel="javax.swing.plaf.metal.MetalLookAndFeel";
lb_appearance.setText("<html>当前外观:<br>默认</html>");
break;
}
UIManager.setLookAndFeel(lookAndFeel);
SwingUtilities.updateComponentTreeUI(mainJFrame);
if(appearance==5)
appearance=0;
} catch(Exception ex) {
System.out.println(ex);
}
}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
});
}
public static void loginPage() { //登录界面
mainJFrame.setSize(550,400);
mainJFrame.setLocationRelativeTo(null);
JPanel pn=new JPanel();
pn.setSize(550,400);
pn.setLayout(null);
JButton bt1=new JButton("返回",new ImageIcon("image/返回.png")),bt2=new JButton("注册账号"),bt3=new JButton("登 录");
JTextField tf=new JTextField();
JPasswordField pf=new JPasswordField();
JLabel lb1,lb2,lb3=new JLabel("密码:");
if(flag) {
lb1=new JLabel("管理员登录");
lb2=new JLabel("账号:");
lb1.setFont(new Font("黑体",0,35));
lb1.setBounds(185,30,180,100);
}
else {
lb1=new JLabel("用户登录");
lb2=new JLabel("卡号:");
lb1.setFont(new Font("黑体",0,35));
lb1.setBounds(200,30,150,100);
}
lb2.setFont(new Font("黑体",0,25));
lb2.setBounds(100,100,80,100);
lb3.setFont(new Font("黑体",0,25));
lb3.setBounds(100,150,80,100);
tf.setFont(new Font("黑体",0,25));
tf.setBounds(170,130,230,40);
pf.setFont(new Font(null,0,25));
pf.setBounds(170,180,230,40);
bt3.setFont(new Font("黑体",0,25));
bt3.setBounds(200,250,150,60);
bt3.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt1.setFont(new Font("黑体",0,17));
bt1.setBounds(1,10,92,25);
bt1.setContentAreaFilled(false);
bt1.setBorderPainted(false);
bt1.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt2.setFont(new Font("黑体",0,17));
bt2.setBounds(440,330,105,25);
bt2.setContentAreaFilled(false);
bt2.setBorderPainted(false);
bt2.setCursor(new Cursor(Cursor.HAND_CURSOR));
pn.add(lb1);
pn.add(lb2);
pn.add(lb3);
pn.add(tf);
pn.add(pf);
pn.add(bt1);
pn.add(bt3);
if(!flag)
pn.add(bt2);
con.add(pn);
bt1.addMouseListener(new MouseListener() { //返回到欢迎界面
public void mouseEntered(MouseEvent arg0) {
bt1.setForeground(Color.blue);
}
public void mouseExited(MouseEvent arg0) {
bt1.setForeground(null);
}
public void mouseClicked(MouseEvent arg0) {
con.remove(pn);
mainJFrame.repaint();
welcomePage();
mainJFrame.validate();
}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
});
bt2.addMouseListener(new MouseListener() { //进入注册界面
public void mouseEntered(MouseEvent arg0) {
bt2.setForeground(Color.blue);
}
public void mouseExited(MouseEvent arg0) {
bt2.setForeground(null);
}
public void mouseClicked(MouseEvent arg0) {
con.remove(pn);
mainJFrame.repaint();
con.add(new Register().registerPage());
mainJFrame.validate();
}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
});
bt3.addActionListener(new ActionListener() { //登录,获取并检验账号密码,正确则进入功能界面
public void actionPerformed(ActionEvent e) {
if(flag) //进行管理员账号密码检验
new Login().adminLogin(tf.getText(),String.valueOf(pf.getPassword()));
else //进行用户账号密码检验
new Login().userLogin(tf.getText(),String.valueOf(pf.getPassword()));
}
});
}
}
\ No newline at end of file
package atm;
import java.io.*;
import javax.swing.*;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class JTableToExcel { //用于将交易记录导出成Excel文件
public static void export(File file, JTable table) { //导出方法
try {
WritableWorkbook workbook=null; //创建工作薄
if (file.exists()) { //文件已经存在
workbook=Workbook.createWorkbook(file, Workbook.getWorkbook(file));
} else { //文件还不存在
workbook=Workbook.createWorkbook(file);
}
// 创建工作表
WritableSheet sheet=workbook.createSheet("交易记录", workbook.getNumberOfSheets());
// 取得table的行数(rowNum), 列数(colNum)
int rowNum=table.getRowCount();
int colNum=table.getColumnCount();
// 填写列名
fillColumnName(sheet, table, colNum);
// 填写数据
fillCell(sheet, table, rowNum, colNum);
// 写入工作表
workbook.write();
workbook.close();
// 导出成功提示框
int dialog=JOptionPane.showConfirmDialog(null, "交易记录导出成功!是否现在打开?", "提示", JOptionPane.YES_NO_OPTION);
if (dialog==JOptionPane.YES_OPTION) { //打开Excel文件
Runtime.getRuntime().exec("cmd /c start \"\" \"" + file + "\"");
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "导出数据前请关闭工作表!");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "导出失败!");
}
}
private static void fillColumnName(WritableSheet sheet, JTable table, int colNum) throws WriteException { //填写列名
WritableFont font=new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //定义字体
WritableCellFormat format=new WritableCellFormat(font); //定义格式化对象
format.setAlignment(Alignment.CENTRE); //水平居中显示
for(int i=0;i<colNum;i++) { //设置列宽
sheet.setColumnView(i, 20);
}
for(int col=0; col<colNum; col++) {
Label colName=new Label(col, 0, table.getModel().getColumnName(col), format);
sheet.addCell(colName);
}
}
private static void fillCell(WritableSheet sheet, JTable table, int rowNum, int colNum ) throws WriteException { //填写数据
WritableFont font=new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //定义字体
WritableCellFormat format=new WritableCellFormat(font); //定义格式化对象
format.setAlignment(Alignment.CENTRE); //水平居中显示
for(int i=0; i<colNum; i++) { //列
for(int j=1; j<=rowNum; j++) { //行
String str=table.getValueAt(j-1, i).toString();
Label labelN=new Label(i, j, str);
try {
sheet.addCell(labelN);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
\ No newline at end of file
package atm;
import java.io.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Login { //登录类
static ArrayList<User> allUsers=null; //所有用户
static User user=null; //进行登录/操作的用户
static int userNum=-1; //进行登录/操作的用户在文件中的集合中的位置
JLabel lb_tips=new JLabel(); //提示窗口的内容
public void adminLogin(String account,String password) { //管理员登录
if(account.equals("Admin") && password.equals("Admin")) { //检验账号密码,若正确,则进入管理员功能界面
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("用户信息.txt"));
allUsers=(ArrayList<User>)ois.readObject(); //从文件中读取所有用户
ois.close();
for(int i=0;i<allUsers.size();i++) { //更新用户欠款金额及信用分
user=allUsers.get(i);
userNum=i;
//若欠款,更新欠款金额及信用分
if(Double.parseDouble(user.debt)!=0) {
SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy-MM-dd");
Date date1=timeFormat.parse(timeFormat.format(System.currentTimeMillis()));
Date date2=timeFormat.parse(user.loanTime.get(user.loanTime.size()-1));
int difference=(int)((date1.getTime()-date2.getTime())/(1000*3600*24)); //计算贷款天数
if(difference>=30) { //达到30天,计算信用分、利息及欠款金额
//获取贷款时信用分和欠款
int credit=Integer.parseInt(user.dai.get(user.dai.size()-2));
String debt=user.dai.get(user.dai.size()-1);
if(credit>difference%30) //信用分足够扣取
user.credit=credit-(int)(difference%30); //计算信用分,每30天-1;
else //信用分不足扣取,置0
user.credit=0;
user.debt=String.valueOf(Double.parseDouble(debt)+Double.parseDouble(debt)*0.015*(difference%30)); //计算欠款,每30天收取月利息
allUsers.remove(userNum);
allUsers.add(userNum,user);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("用户信息.txt"));
oos.writeObject(allUsers);
oos.close();
}
}
}
} catch (ParseException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
lb_tips.setText("登录成功!");
loginTips();
new AdminFunction();
HomePage.mainJFrame.dispose();
}
else { //若错误,则进行提示
lb_tips.setText("账号或密码错误!");
loginTips();
}
}
public void userLogin(String account,String password) { //用户登录
if(account.equals("")) { //未输入卡号,进行提示
lb_tips.setText("请输入卡号!");
loginTips();
}
else if(password.equals("")) { //已输入卡号,但未输入密码,进行提示
lb_tips.setText("请输入密码!");
loginTips();
}
else { //已输入卡号和密码
int i=0; //临时循环变量,代表元素在集合中的位置
ArrayList<User> users=null;
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("用户信息.txt"));
users=(ArrayList<User>)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
for(i=0;i<users.size();i++) { //查询相应卡号是否存在
if(account.equals(users.get(i).account)) {
user=users.get(i);
userNum=i;
break;
}
}
if(i==users.size()) { //已输入卡号和密码,但输入的卡号错误,进行提示
lb_tips.setText("该卡号不存在!");
loginTips();
}
else { //已输入卡号和密码,且输入的卡号正确,密码随意
if(user.lock) { //账户已永久锁定,进行提示
lb_tips.setText("账户已永久锁定,请联系管理员解锁!");
loginTips();
}
else { //账户未锁定,进行当日密码出错次数计算及判断
try {
int n=0; //今日密码出错次数
SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy-MM-dd");
for(i=user.errorTime.size()-1;i>=user.errorTime.size()-3;i--) { //进行当日密码出错次数计算
if(timeFormat.parse(timeFormat.format(System.currentTimeMillis())).equals(timeFormat.parse(user.errorTime.get(i)))) {
n++;
}
}
user.errorNum=n;
if(user.errorNum==3) { //若当日密码出错次数达3次,继续进行登录,将进行提示
lb_tips.setText("今日密码出错达3次,请明日再登录!");
loginTips();
}
else { //若当日密码出错次数未达3次,可继续尝试登录
if(password.equals(user.password)) { //密码正确,登录成功,进入用户功能界面
//若今日与最后转账日不同,今日转账限额初始化10000
if(!(timeFormat.parse(timeFormat.format(System.currentTimeMillis())).equals(timeFormat.parse(user.transferTime.get(user.transferTime.size()-1))))) {
user.transferNum=10000;
users.remove(userNum);
users.add(userNum,user);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("用户信息.txt"));
oos.writeObject(users);
oos.close();
}
//若欠款,计算贷款天数和欠款金额及信用分
if(Double.parseDouble(user.debt)!=0) {
Date date1=timeFormat.parse(timeFormat.format(System.currentTimeMillis()));
Date date2=timeFormat.parse(user.loanTime.get(user.loanTime.size()-1));
int difference=(int)((date1.getTime()-date2.getTime())/(1000*3600*24)); //计算贷款天数
if(difference>=30) { //达到30天,计算信用分、利息及欠款金额
//获取贷款时信用分和欠款
int credit=Integer.parseInt(user.dai.get(user.dai.size()-2));
String debt=user.dai.get(user.dai.size()-1);
if(credit>difference%30) //信用分足够扣取
user.credit=credit-(int)(difference%30); //计算信用分,每30天-1;
else //信用分不足扣取,置0
user.credit=0;
user.debt=String.valueOf(Double.parseDouble(debt)+Double.parseDouble(debt)*0.015*(difference%30)); //计算欠款,每30天收取月利息
users.remove(userNum);
users.add(userNum,user);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("用户信息.txt"));
oos.writeObject(users);
oos.close();
}
}
lb_tips.setText("登录成功!");
loginTips();
new UserFunction();
HomePage.mainJFrame.dispose();
}
else { //密码错误
user.errorTime.add(timeFormat.format(System.currentTimeMillis())); //记录密码错误时间
user.errorNum=n+1; //当日密码出错次数+1
//若当日密码出错次数达3次,记录锁定时间,锁定天数+1
if(user.errorNum==3&&!(timeFormat.parse(timeFormat.format(System.currentTimeMillis())).equals(timeFormat.parse(user.lockTime.get(user.lockTime.size()-1))))) {
user.lockTime.add(timeFormat.format(System.currentTimeMillis())); //记录锁定时间
user.lockDays++; //锁定天数+1
if(user.lockDays==3) //若锁定天数达3天,将永久锁定账户
user.lock=true;
}
users.remove(userNum);
users.add(userNum,user);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("用户信息.txt"));
oos.writeObject(users);
oos.close();
lb_tips.setText("密码错误,今日剩余次数:"+(3-user.errorNum));
loginTips();
}
}
} catch (ParseException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void loginTips() { //登录时弹出的提示信息窗口
JDialog tips=new JDialog(HomePage.mainJFrame," 提示",true);
JPanel pn_tips=new JPanel();
JButton bt_tips=new JButton("确 定");
tips.setSize(450,200);
tips.setLocationRelativeTo(null);
tips.setResizable(false);
tips.setLayout(null);
pn_tips.setBounds(0,30,450,70);
lb_tips.setFont(new Font("黑体",0,25));
bt_tips.setFont(new Font("黑体",0,20));
bt_tips.setBounds(175,100,100,50);
bt_tips.setCursor(new Cursor(Cursor.HAND_CURSOR));
pn_tips.add(lb_tips);
tips.add(pn_tips);
tips.add(bt_tips);
bt_tips.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tips.dispose();
}
});
tips.setVisible(true);
}
}
\ No newline at end of file
package atm;
import javax.swing.*;
public class MyButton extends JButton { //自定义的按钮类,按钮存放了其所在的行列数
private int row;
private int column;
public int getRow() {
return row;
}
public void setRow(int row) {
this.row=row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column=column;
}
public MyButton() {
}
public MyButton(String name) {
super(name);
}
}
\ No newline at end of file
package atm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyButtonEditor extends DefaultCellEditor { //自定义的表格编辑器
private MyButton bt;
private MyEvent event;
public MyButtonEditor() {
super(new JTextField());
bt=new MyButton("查看");
bt.setFont(new Font("黑体",0,20));
bt.setForeground(Color.red.darker());
bt.setContentAreaFilled(false);
bt.setBorderPainted(false);
bt.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //调用自定义的事件处理方法
event.invoke(e);
}
});
}
public MyButtonEditor(MyEvent e) {
this();
this.event=e;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { //重写编辑器方法,返回一个按钮给JTable
setClickCountToStart(0); //点击1次触发事件
//将这个被点击的按钮所在的行和列放进button里面
bt.setRow(row);
bt.setColumn(column);
return bt;
}
}
\ No newline at end of file
package atm;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class MyButtonRender implements TableCellRenderer { //自定义的表格渲染器
private JButton bt;
public MyButtonRender() {
bt=new JButton("查看");
bt.setFont(new Font("黑体",0,20));
bt.setForeground(Color.blue);
bt.setContentAreaFilled(false);
bt.setBorderPainted(false);
bt.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return bt;
}
}
\ No newline at end of file
package atm;
import java.awt.event.ActionEvent;
public abstract class MyEvent {
public abstract void invoke(ActionEvent e);
}
\ No newline at end of file
package atm;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class MyTableModel extends AbstractTableModel { //交易记录表格、用户表格
private Object[] columnNames={"账户","明细","时间","余额","操作人"};
private Object[][] rowData;
public boolean flag=false; //是否为用户表格
public MyTableModel(Object[][] rowData) { //交易记录表格
this.rowData=rowData;
}
public MyTableModel(Object[] columnNames,Object[][] rowData) { //用户表格
flag=true;
this.columnNames=columnNames;
this.rowData=rowData;
}
public int getRowCount() {
return rowData.length;
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int column) {
return columnNames[column].toString();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return rowData[rowIndex][columnIndex];
}
public boolean isCellEditable(int row,int column) { //用户表格第4列可操作
if (flag&&column==4)
return true;
else
return false;
}
}
\ No newline at end of file
package atm;
import javax.swing.text.*;
public class NumLimit extends PlainDocument { //限制文本框只能输入数字
public NumLimit() {
super();
}
public void insertString(int offset,String str,AttributeSet a) throws BadLocationException {
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(offset,new String(s,0,length),a);
}
}
}
\ No newline at end of file
package atm;
import java.io.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Register { //注册类
User user=null;
JPanel pn=new JPanel();
JLabel lb=new JLabel("用户注册"),
lb_name=new JLabel("姓名:"),
lb_sex=new JLabel("性别:"),
lb_idCard=new JLabel("身份证:"),
lb_phone=new JLabel("手机:"),
lb_password=new JLabel("(输入6位数字密码)"),
lb_password1=new JLabel("密码:"),
lb_password2=new JLabel("确认密码:");
JTextField tf_name=new JTextField(),tf_idCard=new JTextField(),tf_phone=new JTextField();
JPasswordField pf_password1=new JPasswordField(),pf_password2=new JPasswordField();
JButton bt1=new JButton("返回",new ImageIcon("image/返回.png")),bt2=new JButton("注 册");
String[] sexx= {"------请选择------"," 男"," 女"};
JComboBox<String> cb_sex=new JComboBox<String>(sexx);
String password1,password2,name,sex,idCard,phone;
JLabel lb_tips=new JLabel();
public JPanel registerPage() { //注册界面
HomePage.mainJFrame.setSize(550,450);
HomePage.mainJFrame.setLocationRelativeTo(null);
pn.setSize(550,450);
pn.setLayout(null);
lb.setFont(new Font("黑体",0,35));
lb.setBounds(200,5,150,70);
lb_name.setFont(new Font("黑体",0,20));
lb_name.setBounds(140,80,70,30);
lb_sex.setFont(new Font("黑体",0,20));
lb_sex.setBounds(140,120,70,30);
lb_idCard.setFont(new Font("黑体",0,20));
lb_idCard.setBounds(120,160,80,30);
lb_phone.setFont(new Font("黑体",0,20));
lb_phone.setBounds(140,200,70,30);
lb_password.setFont(new Font("黑体",0,16));
lb_password.setBounds(400,240,150,30);
lb_password1.setFont(new Font("黑体",0,20));
lb_password1.setBounds(140,240,70,30);
lb_password2.setFont(new Font("黑体",0,20));
lb_password2.setBounds(100,280,100,30);
tf_name.setFont(new Font("黑体",0,20));
tf_name.setBounds(195,80,205,30);
cb_sex.setFont(new Font("黑体",0,20));
cb_sex.setBounds(195,120,205,30);
tf_idCard.setFont(new Font("黑体",0,20));
tf_idCard.setBounds(195,160,205,30);
tf_phone.setFont(new Font("黑体",0,20));
tf_phone.setBounds(195,200,205,30);
tf_phone.setDocument(new NumLimit());
pf_password1.setFont(new Font(null,0,20));
pf_password1.setBounds(195,240,205,30);
pf_password2.setFont(new Font(null,0,20));
pf_password2.setBounds(195,280,205,30);
bt1.setFont(new Font("黑体",0,17));
bt1.setBounds(1,10,92,25);
bt1.setContentAreaFilled(false);
bt1.setBorderPainted(false);
bt1.setCursor(new Cursor(Cursor.HAND_CURSOR));
bt2.setFont(new Font("黑体",0,25));
bt2.setBounds(200,330,150,60);
bt2.setCursor(new Cursor(Cursor.HAND_CURSOR));
pn.add(lb);
pn.add(lb_name);
pn.add(lb_sex);
pn.add(lb_idCard);
pn.add(lb_phone);
pn.add(lb_password);
pn.add(lb_password1);
pn.add(lb_password2);
pn.add(tf_name);
pn.add(cb_sex);
pn.add(tf_idCard);
pn.add(tf_phone);
pn.add(pf_password1);
pn.add(pf_password2);
pn.add(bt1);
pn.add(bt2);
bt1.addMouseListener(new MouseListener() { //返回到用户登录界面
public void mouseEntered(MouseEvent arg0) {
bt1.setForeground(Color.blue);
}
public void mouseExited(MouseEvent arg0) {
bt1.setForeground(null);
}
public void mouseClicked(MouseEvent arg0) {
HomePage.con.remove(pn);
HomePage.mainJFrame.repaint();
HomePage.loginPage();
HomePage.mainJFrame.validate();
}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
});
bt2.addActionListener(new ActionListener() { //注册账号
public void actionPerformed(ActionEvent e) {
getInfo();
}
});
return pn;
}
public void getInfo() { //获取用户信息
boolean y1,y2=false,y3,y4,y5,y6=false;
//y1表示是否全部填写,y2表示密码格式是否正确,y3表示两次密码是否相同,y4表示密码是否为数字,y5表示密码是否为6位,y6表示六位密码是否不完全相同
y1=(!tf_name.getText().trim().equals("")) //判断信息是否全部填写
&&(!tf_idCard.getText().trim().equals(""))
&&(!tf_phone.getText().trim().equals(""))
&&(!String.valueOf(pf_password1.getPassword()).equals(""))
&&(!String.valueOf(pf_password2.getPassword()).equals(""))
&&(cb_sex.getSelectedIndex()!=0);
if(!y1) { //若未完整填写信息,将进行提示
lb_tips.setText("请完善信息!");
registerTips();
}
else { //若完整填写信息,将进行密码格式判断
password1=String.valueOf(pf_password1.getPassword());
password2=String.valueOf(pf_password2.getPassword());
if(password1.equals(password2)) { //判断两次密码是否相同
y3=true;
if(password1.length()==6) { //判断密码是否为6位
y5=true;
y4=true;
int[] chr=new int[6]; //用于储存各位密码的值
for(int i=0;i<password1.length();i++) { //判断密码是否为数字
chr[i]=password1.charAt(i);
if(chr[i]<48||chr[i]>57) { //若密码不为数字,将进行提示
y4=false;
lb_tips.setText("请设置6位数字密码!");
registerTips();
break;
}
if(i==5) { //若密码为6位数字密码,则将进行6位密码是否完全相同的判断
int x=1;
for(x=1;x<6;x++) {
if(chr[x-1]==chr[x])
continue;
else
break;
}
if(x==6) { //若6位密码完全相同,将进行提示
y6=false;
lb_tips.setText("请勿设置6位完全相同的密码!");
registerTips();
}
else
y6=true;
}
}
y2=y3&&y4&&y5&&y6; //若密码为非完全相同的6位数字密码,则密码格式正确y2为true,否则y2为false
}
else { //若密码不为6位,将进行提示
y5=false;
lb_tips.setText("请设置6位数字密码!");
registerTips();
}
}
else { //若两次密码不相同,将进行提示
y3=false;
lb_tips.setText("两次密码不同!");
registerTips();
}
if(y2) { //符合注册条件,则进行注册
lb_tips.setText("注册成功!");
registerTips();
password1=String.valueOf(pf_password1.getPassword());
name=tf_name.getText();
if((String)cb_sex.getSelectedItem()==" 男")
sex="男";
else if((String)cb_sex.getSelectedItem()==" 女")
sex="女";
idCard=tf_idCard.getText();
phone=tf_phone.getText();
register();
completePage();
}
}
}
public void registerTips() { //注册时弹出的提示信息窗口
JDialog tips=new JDialog(HomePage.mainJFrame," 提示",true);
JPanel pn_tips=new JPanel();
JButton bt_tips=new JButton("确 定");
tips.setSize(450,200);
tips.setLocationRelativeTo(null);
tips.setResizable(false);
tips.setLayout(null);
pn_tips.setBounds(0,30,450,70);
lb_tips.setFont(new Font("黑体",0,25));
bt_tips.setFont(new Font("黑体",0,20));
bt_tips.setBounds(175,100,100,50);
bt_tips.setCursor(new Cursor(Cursor.HAND_CURSOR));
pn_tips.add(lb_tips);
tips.add(pn_tips);
tips.add(bt_tips);
bt_tips.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tips.dispose();
}
});
tips.setVisible(true);
}
public void register() { //注册账号,储存账号信息至文件中
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("用户信息.txt"));
ArrayList<User> users=(ArrayList<User>)ois.readObject();
ois.close();
user=new User(password1,name,sex,idCard,phone);
users.add(user);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("用户信息.txt"));
oos.writeObject(users);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void completePage() { //注册完成界面
JButton bt=new JButton("返回登录",new ImageIcon("image/返回.png"));
JLabel lb0=new JLabel("注册完成"),lb1=new JLabel("请牢记您的卡号和密码"),lb2=new JLabel("卡号:"+user.account),lb3=new JLabel("密码:"+user.password);
pn.removeAll();
HomePage.mainJFrame.repaint();
HomePage.mainJFrame.setSize(550,400);
HomePage.mainJFrame.setLocationRelativeTo(null);
pn.setSize(550,400);
pn.setLayout(null);
lb0.setFont(new Font("黑体",0,35));
lb0.setBounds(200,30,150,100);
bt.setFont(new Font("黑体",0,17));
bt.setBounds(1,10,135,25);
bt.setContentAreaFilled(false);
bt.setBorderPainted(false);
bt.setCursor(new Cursor(Cursor.HAND_CURSOR));
lb1.setFont(new Font("黑体",0,20));
lb1.setForeground(Color.red);
lb1.setBounds(170,70,200,100);
lb2.setFont(new Font("黑体",0,25));
lb2.setBounds(125,140,300,100);
lb3.setFont(new Font("黑体",0,25));
lb3.setBounds(125,190,300,100);
pn.add(lb0);
pn.add(lb1);
pn.add(lb2);
pn.add(lb3);
pn.add(bt);
bt.addMouseListener(new MouseListener() { //返回到用户登录界面
public void mouseEntered(MouseEvent arg0) {
bt.setForeground(Color.blue);
}
public void mouseExited(MouseEvent arg0) {
bt.setForeground(null);
}
public void mouseClicked(MouseEvent arg0) {
HomePage.con.remove(pn);
HomePage.mainJFrame.repaint();
HomePage.loginPage();
HomePage.mainJFrame.validate();
}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
});
HomePage.mainJFrame.validate();
}
}
\ No newline at end of file
package atm;
import java.io.*;
import java.util.ArrayList;
public class SaveInitialUser { //用于私下存储初始用户,并测试是否成功,亦可查看所有用户信息
static ArrayList<User> users=new ArrayList<User>(); //用于储存所有用户
public static void main(String[] args) {
init(); //仅查看用户信息,请注释此
write(); //仅查看用户信息,请注释此
print();
}
public static void init() { //用于初始化或自定义用户(想修改初始用户信息,请修改此)
boolean lock=false; //账户状态
int lockDays=0,errorNum=0,transferNum=10000,credit=100; //锁定天数,当日密码出错次数,当日转账限额,信用分
String account="2020202012345678", //卡号
password="123456", //密码
name="小明", //姓名
sex="男", //性别
idCard="445221200003310216", //身份证
phone="13602161617", //手机
money="100000.0", //余额
debt="0", //欠款
time="2020-08-25"; //建卡日期
User user=new User(lock,lockDays,errorNum,transferNum,credit,account,password,name,sex, idCard,phone,money,debt,time);
users.add(user);
User user0=new User(false,0,0,10000,80,"2020202011223344","112233","小红","女", "445221200101010216","13600021116","100.0","0","2020-08-25");
users.add(user0);
// users.add(new User(password,name,sex,idCard,phone)); //系统赋其他初值
}
public static void write() { //用于将用户信息写入至文件
ObjectOutputStream oos;
try {
oos=new ObjectOutputStream(new FileOutputStream("用户信息.txt",false));
oos.writeObject(users);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void print() { //用于读取文件中的用户信息,并输出至控制台
ArrayList<User> test_users=null; //用于储存从文件中读取的所有用户
ObjectInputStream ois;
try {
ois=new ObjectInputStream(new FileInputStream("用户信息.txt"));
test_users=(ArrayList<User>)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
for(int i=0;i<test_users.size();i++) {
User test_user=test_users.get(i); //用于储存单个用户
System.out.println(i+"号 ======================================================");
System.out.println("账户状态:"+test_user.lock);
System.out.println("锁定天数:"+test_user.lockDays);
System.out.println("当日密码出错次数:"+test_user.errorNum);
System.out.println("当日转账限额:"+test_user.transferNum);
System.out.println("信用分:"+test_user.credit);
System.out.println("密码锁定时间:"+test_user.lockTime);
System.out.println("密码出错时间:"+test_user.errorTime);
System.out.println("转账时间:"+test_user.transferTime);
System.out.println("贷款时间:"+test_user.loanTime);
System.out.println("贷款时信用分和欠款记录:"+test_user.dai);
System.out.println("卡号:"+test_user.account);
System.out.println("密码:"+test_user.password);
System.out.println("姓名:"+test_user.name);
System.out.println("性别:"+test_user.sex);
System.out.println("身份证:"+test_user.idCard);
System.out.println("手机:"+test_user.phone);
System.out.println("余额:"+test_user.money);
System.out.println("欠款:"+test_user.debt);
System.out.println("建卡日期:"+test_user.time);
System.out.println("交易记录:"+test_user.record);
}
}
}
\ No newline at end of file
package atm;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class User implements Serializable { //用户类
boolean lock ; //账户状态,是否永久锁定
int lockDays,errorNum,transferNum,credit; //锁定天数,当日密码出错次数(指密码最后出错当日而非今日),当日转账限额(指最后登录当日),信用分
ArrayList<String> lockTime=new ArrayList<String>(); //锁定时间
ArrayList<String> errorTime=new ArrayList<String>(); //密码出错时间
ArrayList<String> transferTime=new ArrayList<String>(); //转账时间
ArrayList<String> loanTime=new ArrayList<String>(); //贷款时间
ArrayList<String> dai=new ArrayList<String>(); //贷款时信用分和欠款记录,每两个元素为一组
String account, //卡号
password, //密码
name, //姓名
sex, //性别
idCard, //身份证
phone, //手机
money, //余额
debt, //欠款
time; //建卡日期
ArrayList<String> record=new ArrayList<String>(); //交易记录,每三个元素组成一条交易记录
public User(String password, String name, String sex, String idCard, String phone) { //为账户各信息赋值
ArrayList<User> users=null;
ObjectInputStream ois;
try {
ois=new ObjectInputStream(new FileInputStream("用户信息.txt"));
users=(ArrayList<User>)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
lock=false; //账户状态初始化正常非永久锁定
lockDays=0; //锁定天数初始化0
errorNum=0; //当日密码出错次数初始化0
transferNum=10000; //当日转账限额初始化10000
credit=90; //信用分初始化90
lockTime.add("0000-00-00"); //锁定时间初始化
errorTime.add("0000-00-00");
errorTime.add("0000-00-00");
errorTime.add("0000-00-00"); //密码出错时间初始化
transferTime.add("0000-00-00"); //转账时间初始化
loanTime.add("0000-00-00"); //贷款时间初始化
this.password=password; //密码赋值
this.name=name; //姓名赋值
this.sex=sex; //性别赋值
this.idCard=idCard; //身份证赋值
this.phone=phone; //手机赋值
money="0.0"; //余额初始化0
debt="0"; //欠款初始化0
SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy-MM-dd");
time=timeFormat.format(System.currentTimeMillis()); //建卡日期赋值
Random r=new Random();
boolean y=false; //是否重复
do { //卡号赋值,并查重
account="20202020";
for(int i=0;i<8;i++) //赋值
account=account+r.nextInt(10);
for(int i=0;i<users.size();i++) { //查重
if(account.equals(users.get(i).account)) {
y=true;
break;
}
else
y=false;
}
} while(y);
}
public User(boolean lock,int lockDays,int errorNum,int transferNum,int credit,String account,String password,String name,String sex,String idCard,String phone,String money,String debt,String time) {
//用于私下存储初始用户
this.lock=lock;
this.lockDays=lockDays;
this.errorNum=errorNum;
this.transferNum=transferNum;
this.credit=credit;
lockTime.add("0000-00-00");
errorTime.add("0000-00-00");
errorTime.add("0000-00-00");
errorTime.add("0000-00-00");
transferTime.add("0000-00-00");
loanTime.add("0000-00-00");
this.account=account;
this.password=password;
this.name=name;
this.sex=sex;
this.idCard=idCard;
this.phone=phone;
this.money=money;
this.debt=debt;
this.time=time;
}
}
\ No newline at end of file
此差异已折叠。
文件已添加
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册