简介
此程序是本人大一时期的Java大作业,初学Java一学期后所编写的一个Java图形界面程序。作业题目是要求做一个ATM柜员机模拟程序,但本人考虑到这仅仅是做一个软件程序无需搭配硬件等,故将其做成一个ATM柜员机线上系统,使用账号及密码登录,亦可进行注册等。
本程序使用Java编写,用到了Java Swing,拥有图形界面,点击首页的“更换外观”按钮可切换不同风格的外观,程序分为两个模式,用户模式和系统管理模式。用户可进行注册和登录,进入用户模式,功能包括:查看个人信息、存款和取款、转账、贷款和还款、查看交易记录、修改密码等;管理员可输入指定的账号密码登录,进入系统管理模式,功能包括:用户管理、查看历史交易记录等。
本程序的数据存储使用文件存储,程序检错能力强,功能颇多(还可将交易记录导出为excel文件哦),界面工整好看(每个组件都经过细心调整)。但由于是大一时期初学Java后所写的程序,部分代码可能有些繁杂及不合理之处(纯从零开始写的,且为了界面美观,所以有大部分代码都在调整界面组件)。
程序源代码及程序设计说明书可在下方GitHub链接处进行下载,供各位需要的人学习参考。
GitHub链接:ATM柜员机线上系统
程序流程
程序大概流程如下图:
部分功能展示
本程序功能及细节颇多,在此仅展示部分功能,完整功能请至文章开头处GitHub链接下载程序,进行体验。
欢迎界面-更换外观
打开本程序,进入欢迎界面,在此可选择用户模式和系统管理模式,点击右上角“更换外观”可更改系统整体外观,共有5种不同风格的外观。
登录
用户模式下,输入系统中存有的卡号和密码进行登录,登录成功则进入用户功能菜单界面;系统管理模式下,输入系统中固定的账号和密码进行登录,登陆成功则进入系统管理功能菜单界面。两个模式下,登录失败都会有相应的提示或限制,如用户模式下,某卡号一天内输入密码出错超过3次,则该账户将被临时锁定,今日无法再登录,累计三日被锁定,则该账户将被永久锁定,需管理员解锁。
注册
用户注册,填写相应的信息进行注册,若信息填写格式错误,将进行提示,注册完成后,系统将赋予用户一个卡号,并显示卡号与密码,用户需在此时牢记自己的卡号和密码。
个人信息
个人信息功能,在此显示用户的个人信息。
存款/取款
存款/取款功能,可进行存款和取款。
转账
转账功能,可向指定账户进行转账。
贷款/还款
贷款/还款功能,可进行贷款和还款,需遵循贷款还款规则。
交易记录
交易记录功能,可查看该账户的所有交易记录,且可将交易记录导出为excel表格文件。
修改密码
修改密码功能,可进行密码的修改。
用户管理
用户管理功能,管理员可在此管理系统中的所有用户,点击右侧“查看”按钮进行指定用户的详细管理操作。
历史记录
历史记录功能,管理员可在此查看系统中所有用户的历史交易记录,还可查看指定用户的个人交易记录,且都可导出为excel表格文件。
部分代码展示
本程序代码量巨大,在此仅展示部分程序代码,完整代码请至文章开头处GitHub链接下载程序,进行了解。
为了系统界面的美观 强迫症患者 ,所以本程序的很多代码都在调整界面组件,请别介意。
User类
用户类,一个用户即为一个用户类对象。
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;
errorNum=0;
transferNum=10000;
credit=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";
debt="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);
}
}
HomePage类
主页类,main方法包含在此类中,是程序的入口,定义了系统的欢迎界面、登录界面和更换外观功能等。
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:
lookAndFeel="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
lb_appearance.setText("<html>当前外观:<br>Windows</html>");
break;
case 2:
lookAndFeel="com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
lb_appearance.setText("<html>当前外观:<br>Nimbus</html>");
break;
case 3:
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()));
}
});
}
}
Register类
注册类,定义了系统的注册界面,用于完成用户的注册功能。
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=(!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) {
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) {
int x=1;
for(x=1;x<6;x++) {
if(chr[x-1]==chr[x])
continue;
else
break;
}
if(x==6) {
y6=false;
lb_tips.setText("请勿设置6位完全相同的密码!");
registerTips();
}
else
y6=true;
}
}
y2=y3&&y4&&y5&&y6;
}
else {
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();
}
}
Login类
登录类,用于进行登录验证。
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) {
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);
else
user.credit=0;
user.debt=String.valueOf(Double.parseDouble(debt)+Double.parseDouble(debt)*0.015*(difference%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) {
lb_tips.setText("今日密码出错达3次,请明日再登录!");
loginTips();
}
else {
if(password.equals(user.password)) {
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) {
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);
else
user.credit=0;
user.debt=String.valueOf(Double.parseDouble(debt)+Double.parseDouble(debt)*0.015*(difference%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;
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++;
if(user.lockDays==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);
}
}
… … 其他代码请见程序源码!
后记
本程序仅供学习和参考,请勿抄袭或另作他用。 感谢观看,有什么问题可在下方评论区进行评论,若觉得本文章写得不错,还请点个赞呢。( ? ?ω?? )?求点赞、评论、收藏
|