package view;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import dao.UserDao;
import dao.impl.UserDaoImpl;
public class ATMView extends JFrame {
private static int USERID;
private static JFrame loginView;
private static JPanel welcomeView, welcome_btn, operationView, inquireView, depositView, withdrawView, transferView,
changePasswordView;
private static UserDao userDao = new UserDaoImpl();
private static JButton btn_login, btn_Wexit, btn_inquire, btn_deposit, btn_withdraw, btn_transfer, btn_Oexit,
btn_inquireMoney, btn_withdrawJournal, btn_depositJournal, btn_transferJournal, btn_Jexit,
btn_changePassword;
private static JTextField tLoginUser, jt_withdrawmoney, jt_accountId, jt_transfermoney ;
private static JPasswordField tLoginPassword,jt_password1, jt_password2;
private static JLabel jl_welcom;
private static String DATE;
/**
* 欢迎界面
*/
public void welcomeView() {
this.setTitle("ATM自助系统");
this.setResizable(false);
this.setDefaultCloseOperation(3);
welcomeView = new JPanel();
welcomeView.setLayout(null);
jl_welcom = new JLabel("欢迎");
jl_welcom.setBounds(225, 60, 230, 100);
jl_welcom.setFont(new Font("宋体", Font.ITALIC, 100));
jl_welcom.setForeground(Color.YELLOW);
btn_login = new JButton("登录");
btn_Wexit = new JButton("退出");
;
welcome_btn = new JPanel(new FlowLayout());
welcome_btn.add(btn_login);
welcome_btn.add(btn_Wexit);
welcome_btn.setBounds(225, 200, 250, 100);
welcomeView.add(jl_welcom);
welcomeView.add(welcome_btn);
welcome_btn.setOpaque(false);
// 登录按钮的点击事件
btn_login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginView();
// operationView();
// USERID = 11111111;
}
});
// 退出
btn_Wexit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setBackGround(welcomeView);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/**
* 设置背景
*
* @param jpanel
*/
public void setBackGround(JPanel jpanel) {
// 加载图片
ImageIcon icon = new ImageIcon("image/ATMWelcome.png");
JLabel label = new JLabel(icon);
// 设置label的大小
label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
// 获取窗口的第二层,将label放入
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
// 获取frame的顶层容器,并设置为透明
JPanel j = (JPanel) this.getContentPane();
j.setOpaque(false);
jpanel.setOpaque(false);
this.setSize(icon.getIconWidth(), icon.getIconHeight());
this.add(jpanel);
this.setVisible(true);
}
/**
* 登录界面
*/
public void loginView() {
loginView = new JFrame();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
loginView.setBounds(((int) dimension.getWidth() - 200) / 2, ((int) dimension.getHeight() - 300) / 2, 200, 150);
loginView.setResizable(false);
loginView.setLayout(null);
JLabel label1 = new JLabel("卡号:");
label1.setBounds(10, 10, 100, 30);
loginView.add(label1);
JLabel label2 = new JLabel("密码:");
label2.setBounds(10, 40, 100, 30);
loginView.add(label2);
tLoginUser = new JTextField();
tLoginUser.setBounds(50, 15, 130, 20);
loginView.add(tLoginUser);
tLoginPassword = new JPasswordField();
tLoginPassword.setBounds(50, 45, 130, 20);
loginView.add(tLoginPassword);
btn_login = new JButton("Login");
btn_login.setBounds(10, 75, 170, 40);
btn_login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int userId = Integer.valueOf(tLoginUser.getText());
int password = Integer.valueOf(new String(tLoginPassword.getPassword()));
if (userDao.haveCustomer(userId)) {
if (userDao.isPassword(userId, password)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
USERID = userId;
DATE = sdf.format(new Date());
userDao.journal(USERID, "登录:成功!");
JOptionPane.showMessageDialog(null, "登陆成功", "提示", JOptionPane.INFORMATION_MESSAGE);
loginView.setVisible(false);
operationView();
} else {
JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.ERROR_MESSAGE);
tLoginPassword.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "用户不存在", "提示", JOptionPane.ERROR_MESSAGE);
tLoginPassword.setText("");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入用户名和密码", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});
loginView.add(btn_login);
loginView.setVisible(true);
}
/**
* 业务界面
*/
public void operationView() {
operationView = new JPanel();
operationView.setLayout(null);
btn_inquire = new JButton("查询");
btn_inquire.addActionListener(new BL());
btn_changePassword = new JButton("修改密码");
btn_changePassword.addActionListener(new BL());
btn_deposit = new JButton("存款");
btn_deposit.addActionListener(new BL());
btn_withdraw = new JButton("取款");
btn_withdraw.addActionListener(new BL());
btn_transfer = new JButton("转账");
btn_transfer.addActionListener(new BL());
btn_Oexit = new JButton("退出");
btn_Oexit.addActionListener(new BL());
operationView.add(btn_inquire);
operationView.add(btn_deposit);
operationView.add(btn_withdraw);
operationView.add(btn_transfer);
operationView.add(btn_Oexit);
operationView.add(btn_changePassword);
btn_inquire.setBounds(2, 50, 100, 50);
btn_transfer.setBounds(2, 175, 100, 50);
btn_changePassword.setBounds(2, 300, 100, 50);
btn_deposit.setBounds(550, 50, 100, 50);
btn_withdraw.setBounds(550, 175, 100, 50);
btn_Oexit.setBounds(550, 300, 100, 50);
this.remove(welcomeView);
setBackGround(operationView);
}
/**
* operation内部类
*
* @author Administrator
*
*/
class BL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o == btn_inquire) {
System.out.println("查询");
inquireView();
}
if (o == btn_deposit) {
System.out.println("取款");
depositView();
}
if (o == btn_withdraw) {
System.out.println("存款");
withdrawView();
}
if (o == btn_transfer) {
System.out.println("转账");
transferView();
}
if (o == btn_changePassword) {
System.out.println("修改密码");
changePasswordView();
}
if (o == btn_Oexit) {
JFrame jf = new JFrame("凭条");
Container contentpane = jf.getContentPane();
jf.setLayout(new GridLayout(1, 1));
try {
ListModel mode;
mode = new ListModel() {
List list = userDao.dayJournal(DATE, USERID);
@Override
public int getSize() {
return list.size();
}
@Override
public String getElementAt(int index) {
return (index + 1) + "." + list.get(index++);
}
@Override
public void addListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
@Override
public void removeListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
};
JList list = new JList<>(mode);
contentpane.add(new JScrollPane(list));
} catch (SQLException e1) {
e1.printStackTrace();
}
jf.pack();
jf.setLocationRelativeTo(null);
jf.setResizable(false);
ATMView.this.remove(operationView);
welcomeView();
jf.setVisible(true);
}
}
}
/**
* 查询界面
*/
public void inquireView() {
inquireView = new JPanel();
inquireView.setLayout(null);
btn_inquireMoney = new JButton("查询账户余额");
btn_inquireMoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
JOptionPane.showMessageDialog(null, "余额:" + userDao.money(USERID) + "元", "余额",
JOptionPane.INFORMATION_MESSAGE);
System.out.println(userDao.money(USERID));
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btn_withdrawJournal = new JButton("查询存款记录");
btn_withdrawJournal.addActionListener(new IBJ());
btn_depositJournal = new JButton("查询取款记录");
btn_depositJournal.addActionListener(new IBJ());
btn_transferJournal = new JButton("查询转账记录");
btn_transferJournal.addActionListener(new IBJ());
btn_Jexit = new JButton("退出");
btn_Jexit.addActionListener(new IBJ());
inquireView.add(btn_inquireMoney);
inquireView.add(btn_withdrawJournal);
inquireView.add(btn_depositJournal);
inquireView.add(btn_transferJournal);
inquireView.add(btn_Jexit);
btn_inquireMoney.setBounds(2, 100, 150, 50);
btn_withdrawJournal.setBounds(2, 250, 150, 50);
btn_depositJournal.setBounds(500, 50, 150, 50);
btn_transferJournal.setBounds(500, 175, 150, 50);
btn_Jexit.setBounds(550, 300, 100, 50);
this.remove(operationView);
setBackGround(inquireView);
}
class IBJ implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
try {
if (o == btn_withdrawJournal) {
journalView("存款");
}
if (o == btn_depositJournal) {
journalView("取款");
}
if (o == btn_transferJournal) {
journalView("转账");
}
} catch (Exception e1) {
e1.printStackTrace();
}
if (o == btn_Jexit) {
ATMView.this.remove(inquireView);
setBackGround(operationView);
}
}
}
/**
* 记录界面
*
* @throws Exception
*/
public void journalView(String genre) throws Exception {
JFrame jf = new JFrame(genre);
Container contentpane = jf.getContentPane();
jf.setLayout(new GridLayout(1, 1));
ListModel mode = new ListModel() {
List list = userDao.checkJournal(USERID, genre);
@Override
public int getSize() {
return list.size();
}
@Override
public String getElementAt(int index) {
return (index + 1) + "." + list.get(index++);
}
@Override
public void addListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
@Override
public void removeListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
};
JList list = new JList<>(mode);
contentpane.add(new JScrollPane(list));
jf.pack();
jf.setLocationRelativeTo(null);
jf.setResizable(false);
jf.setVisible(true);
}
/**
* 取款界面
*/
public void withdrawView() {
withdrawView = new JPanel();
withdrawView.setLayout(null);
JPanel jp1 = new JPanel(new GridLayout(2, 1));
JLabel jl_label = new JLabel(" 请输入取款金额");
jl_label.setFont(new Font("宋体", Font.ITALIC, 20));
jl_label.setForeground(Color.orange);
jt_withdrawmoney = new JTextField();
jp1.add(jl_label);
jp1.add(jt_withdrawmoney);
jp1.setOpaque(false);
JPanel jp2 = new JPanel(new FlowLayout());
JButton jb1 = new JButton("确认");
JButton jb2 = new JButton("退出");
jp2.add(jb1);
jp2.add(jb2);
jp2.setOpaque(false);
withdrawView.add(jp1);
withdrawView.add(jp2);
jp1.setBounds(225, 60, 230, 100);
jp2.setBounds(250, 200, 150, 100);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double money = Double.valueOf(jt_withdrawmoney.getText());
if (money <= 0) {
JOptionPane.showMessageDialog(null, "请输入正确数值", "提示", JOptionPane.ERROR_MESSAGE);
jt_withdrawmoney.setText("");
return;
}
int userPassword = Integer.valueOf(JOptionPane.showInputDialog("请输入密码"));
userDao.withdraw(USERID, money, userPassword);
ATMView.this.remove(withdrawView);
operationView();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "金额或密码为空!", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(withdrawView);
operationView();
}
});
this.remove(operationView);
setBackGround(withdrawView);
}
/**
* 存款界面
*/
public void depositView() {
depositView = new JPanel();
depositView.setLayout(null);
JButton jb1 = new JButton("开始存款");
JButton jb2 = new JButton("退出存款");
depositView.add(jb1);
depositView.add(jb2);
jb1.setBounds(225, 60, 230, 100);
jb2.setBounds(550, 300, 100, 50);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String src = JOptionPane.showInputDialog("输入存放的金额");
double money = Double.valueOf(src);
if (money > 0) {
try {
userDao.deposit(USERID, money);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "请输入正确数值", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(depositView);
operationView();
}
});
this.remove(operationView);
setBackGround(depositView);
}
/**
* 转账界面
*/
public void transferView() {
transferView = new JPanel();
transferView.setLayout(null);
JPanel jp1 = new JPanel(new GridLayout(2, 1));
JLabel jl1 = new JLabel(" 对方账户");
jp1.add(jl1);
jt_accountId = new JTextField();
jp1.add(jt_accountId);
jl1.setFont(new Font("宋体", Font.ITALIC, 20));
jl1.setForeground(Color.YELLOW);
jp1.setOpaque(false);
JPanel jp2 = new JPanel(new GridLayout(2, 1));
JLabel jl2 = new JLabel(" 转账金额");
jp2.add(jl2);
jt_transfermoney = new JTextField();
jp2.add(jt_transfermoney);
jl2.setFont(new Font("宋体", Font.ITALIC, 20));
jl2.setForeground(Color.YELLOW);
jp2.setOpaque(false);
JPanel jp3 = new JPanel(new FlowLayout());
JButton jb1 = new JButton("确认");
JButton jb2 = new JButton("退出");
jp3.setOpaque(false);
jp3.add(jb1);
jp3.add(jb2);
jp1.setBounds(200, 30, 200, 100);
jp2.setBounds(200, 140, 200, 100);
jp3.setBounds(200, 250, 200, 100);
transferView.add(jp1);
transferView.add(jp2);
transferView.add(jp3);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int accountId = 0;
double money = 0;
try {
accountId = Integer.valueOf(jt_accountId.getText());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入对方卡号!", "提示", JOptionPane.ERROR_MESSAGE);
}
try {
money = Double.valueOf(jt_transfermoney.getText());
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入密码!", "提示", JOptionPane.ERROR_MESSAGE);
}
if (accountId == USERID) {
JOptionPane.showMessageDialog(null, "不能转账给自己", "提示", JOptionPane.ERROR_MESSAGE);
jt_accountId.setText("");
return;
}
if (money <= 0) {
JOptionPane.showMessageDialog(null, "请输入正确金额", "提示", JOptionPane.ERROR_MESSAGE);
jt_transfermoney.setText("");
return;
}
try {
if (!userDao.haveCustomer(accountId)) {
JOptionPane.showMessageDialog(null, "账户不存在", "提示", JOptionPane.ERROR_MESSAGE);
jt_accountId.setText("");
return;
}
String accountName = JOptionPane.showInputDialog("请确认对方身份");
if (accountName.equals(userDao.userName(accountId))) {
int userPassword = Integer.valueOf(JOptionPane.showInputDialog("请输入密码"));
userDao.transfer_accounts(USERID, accountId, money, userPassword);
} else {
JOptionPane.showMessageDialog(null, "对方身份不符", "提示", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(transferView);
operationView();
}
});
this.remove(operationView);
setBackGround(transferView);
}
/**
* 修改密码界面
*/
public void changePasswordView() {
try {
int userPassword = Integer.valueOf(JOptionPane.showInputDialog("请输入密码"));
if (userDao.isPassword(USERID, userPassword)) {
changePasswordView = new JPanel();
changePasswordView.setLayout(null);
JPanel jp1 = new JPanel(new GridLayout(2, 1));
JLabel jl1 = new JLabel(" 请输入新密码");
jp1.add(jl1);
jt_password1 = new JPasswordField();
jp1.add(jt_password1);
jl1.setFont(new Font("宋体", Font.ITALIC, 20));
jl1.setForeground(Color.YELLOW);
jp1.setOpaque(false);
JPanel jp2 = new JPanel(new GridLayout(2, 1));
JLabel jl2 = new JLabel(" 请确认新密码");
jp2.add(jl2);
jt_password2 = new JPasswordField();
jp2.add(jt_password2);
jl2.setFont(new Font("宋体", Font.ITALIC, 20));
jl2.setForeground(Color.YELLOW);
jp2.setOpaque(false);
JPanel jp3 = new JPanel(new FlowLayout());
JButton jb1 = new JButton("确认");
JButton jb2 = new JButton("退出");
jp3.setOpaque(false);
jp3.add(jb1);
jp3.add(jb2);
jp1.setBounds(200, 30, 200, 100);
jp2.setBounds(200, 140, 200, 100);
jp3.setBounds(200, 250, 200, 100);
changePasswordView.add(jp1);
changePasswordView.add(jp2);
changePasswordView.add(jp3);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int userPassword1 = Integer.valueOf(new String(jt_password1.getPassword()));
int userPassword2 = Integer.valueOf(new String(jt_password2.getPassword()));
if (userPassword1 == userPassword2) {
userDao.changePassword(USERID, userPassword1);
ATMView.this.remove(changePasswordView);
welcomeView();
} else {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致!", "提示", JOptionPane.ERROR_MESSAGE);
jt_password1.setText("");
jt_password2.setText("");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "请输入新密码!", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ATMView.this.remove(changePasswordView);
operationView();
}
});
this.remove(operationView);
setBackGround(changePasswordView);
} else {
JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
System.out.println("取消输入");
}
}
public static void main(String[] args) {
ATMView atm = new ATMView();
atm.welcomeView();
}
}