package customerFinance;
import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date; //import java.util.regex.Matcher;
//import java.util.regex.Pattern;
import javax.swing.*;
import DataMethod.DataBase;
import DataMethod.Regex;
;
/**
*
* @author Bondy Zhou
*
*/
public class Transfer extends JPanel implements ActionListener {
private JLabel[] jlArray = { new JLabel("输入您的卡号"), new JLabel("输入您的密码"),
new JLabel("输入对方的卡号"), new JLabel("再次输入对方卡号"), new JLabel("转账数额"),
new JLabel("转账日期") };
private JLabel[] jlArray2 = { new JLabel("卡号是0-9之间10为的数字"),
new JLabel("密码是0-9之间的6位数字 "), new JLabel("卡号是0-9之间10为的数字"),
new JLabel("卡号是0-9之间10为的数字"), new JLabel("金额是0-9之间的数字") };
private JTextField[] jtfArray = { new JTextField(), new JTextField(),
new JTextField(), new JTextField(), new JTextField() };
private JPasswordField jpf = new JPasswordField();
private JButton[] jbArray = { new JButton("验 证"), new JButton("提 交"),
new JButton("重 置") };
private DataBase db = null;
public Transfer() {
this.initialFrame();
this.addListener();
}
public void initialFrame() {
this.setLayout(null);
for (int i = 0; i < jlArray.length; i++) {
jlArray[i].setBounds(30, 20 + 50 * i, 180, 35);
jlArray[i].setFont(new Font("华文行楷", Font.ITALIC + Font.BOLD, 16));
this.add(jlArray[i]);
}
for (int i = 0; i < jlArray2.length; i++) {
jlArray2[i].setBounds(400, 20 + 50 * i, 200, 35);
jlArray2[i].setForeground(Color.RED);
jlArray2[i].setFont(new Font("华文行楷", Font.BOLD, 16));
this.add(jlArray2[i]);
}
jtfArray[0].setBounds(170, 20, 200, 35);
jtfArray[0].setFont(new Font("宋体", Font.BOLD, 16));
this.add(jtfArray[0]);
jpf.setBounds(170, 70, 200, 35);
jpf.setFont(new Font("宋体", Font.BOLD, 16));
this.add(jpf);
for (int i = 1; i < jtfArray.length; i++) {
jtfArray[i].setBounds(170, 20 + 50 * (i + 1), 200, 35);
jtfArray[i].setFont(new Font("宋体", Font.BOLD, 16));
this.add(jtfArray[i]);
jtfArray[i].setEditable(false);
jtfArray[i].addActionListener(this);
}
// 构造注册日期
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
jtfArray[4].setText(sdf.format(date));
jtfArray[4].setEditable(false);
for (int i = 0; i < jbArray.length; i++) {
jbArray[i].setBounds(40 + i * 120, 320, 100, 40);
jbArray[i].setFont(new Font("宋体", Font.BOLD, 16));
this.add(jbArray[i]);
jbArray[i].addActionListener(this);
}
jbArray[1].setEnabled(false);
this.setBackground(Color.CYAN); //设置背景为青色
}
public void addListener() {
jtfArray[0].addActionListener(this);
jpf.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
db = new DataBase();
String sql = null;
String message = null;
String yourCard = jtfArray[0].getText().trim();
String pwd = new String(jpf.getPassword()).trim();
String hisCard = jtfArray[1].getText().trim();
String confirmCard = jtfArray[2].getText().trim();
String transferMoney = jtfArray[3].getText().trim();
String transferDate = jtfArray[4].getText().trim();
Regex r = new Regex();
ResultSet rs = null;
// 增强键盘的可用性,自动移动光标
if (e.getSource() == jtfArray[0]) {
jpf.requestFocus(true);
}
if (e.getSource() == jtfArray[1]) {
jtfArray[2].requestFocus(true);
} else if (e.getSource() == jtfArray[2]) {
jtfArray[3].requestFocus(true);
} else if (e.getSource() == jbArray[2]) {
for (int i = 0; i < jtfArray.length; i++) {
if (i == 0) {
jtfArray[i].setEditable(true);
jpf.setEditable(true);
jtfArray[i].requestFocus(true);
jtfArray[i].setText("");
} else if(i == 4) {
return;
} else {
jpf.setText("");
jtfArray[i].setText("");
jtfArray[i].setEditable(false);
}
}
jpf.setText("");
jpf.setEditable(true);
jbArray[0].setEnabled(true);
jbArray[1].setEnabled(false);
}
else if (e.getSource() == jpf || e.getSource() == jbArray[0]) {
if (yourCard.equals("")) {
jtfArray[0].requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "请输入卡号", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else if (!r.checkCusId(yourCard)) {
jtfArray[0].requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "卡号是0-9的10位数字", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else if (pwd.equals("")) {
jpf.requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "请输入密码", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else if (!r.checkCusPwd(pwd)) {
jpf.requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "密码只能是6位0-9的整数", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
// 得到顾客表中你的的余额信息
sql = "select cusLeftMoney from customer where cusCard = '"
+ yourCard + "'and cusPassword = '" + pwd + "'";
rs = db.selectDb(sql);
try {
if (rs.next()) {
String str = rs.getString(1); // 得到customer中的余额
double yourMoney = Double.parseDouble(str.trim());
if (yourMoney < 10.0) {
jtfArray[0].setText("");
jpf.setText("");
jtfArray[0].requestFocus(true);
JOptionPane.showMessageDialog(this, "您的已余额不足,转账失败",
"错误", JOptionPane.ERROR_MESSAGE);
return;
} else {
int i = JOptionPane.showConfirmDialog(this, "你的余额为" + str
+"元\n请输入转账金额","询问",JOptionPane.YES_NO_OPTION);
//判断是否为该用户,然后进行相应的操作
if(i == JOptionPane.YES_OPTION) {
jtfArray[1].requestFocus(true);
jpf.setEditable(false);
for(int j=0; j<jtfArray.length; j++) {
if(j==0 || j==4) {
jtfArray[j].setEditable(false);
} else {
jtfArray[j].setEditable(true);
}
}
jbArray[0].setEnabled(false);
jbArray[1].setEnabled(true);
} else {
jtfArray[0].requestFocus(true);
}
}
} else {
JOptionPane.showMessageDialog(this, "请检查卡号或密码是否正确", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (HeadlessException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
db.dbClose();
}
}
else if (e.getSource() == jtfArray[3] || e.getSource() == jbArray[1]) {
int i = 0;
int j = 0;
int k = 0;
int a = 0;
int b = 0;
// 判断输入的情况是否符合实际情况
if (hisCard.equals("")) {
jtfArray[1].requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "请输入对方的卡号", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else if (!r.checkCusId(hisCard)) {
jtfArray[1].requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "卡号是0-9的10位数字", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else if (confirmCard.equals("")) {
jtfArray[2].requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "确认卡号不能为空", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else if (!r.checkCusId(confirmCard)) {
jtfArray[2].requestFocus(true); // 将光标转到该文本框
JOptionPane.showMessageDialog(this, "卡号是0-9的10位数字", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else if (!confirmCard.equals(hisCard
评论13
最新资源