📄 withdraw.java
字号:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//取款功能
public class withDraw extends JFrame implements ActionListener{
private Statement st=null;
private Connection con=null;
private ResultSet rs=null;
private String command=null;
private boolean flag=true;
private String getCardNum=null;
private String getpswd=null;
private String getMoney=null;
private String money; //
private JLabel cardNumLabel=new JLabel("卡 号:");
private JTextField cardNum=new JTextField(18);
private JLabel cardpswdLabel=new JLabel("口 令:");
private JPasswordField cardpswd=new JPasswordField(8);
private JLabel inputMoneyLabel=new JLabel("请输入要取款金额(50~2000)");
private JTextField inputMoney=new JTextField(6);
private JButton withDrawButton=new JButton("取款");
private JButton close=new JButton("关闭");
private JLabel resultLabel=new JLabel("操作结果");
private JTextArea resultArea=new JTextArea(8,40);
//无参构造方法
public withDraw(){
setup(); //初始化面板
}
//有参构造方法
public withDraw(String card,String pswd){
setup(); //初始化面板
cardNum.setText(card);
cardpswd.setText(pswd);
}
//初始化面板方法
public void setup(){
setTitle("取 款");
setSize(350,400);
setLocation(300,400);
setResizable(false);
resultArea.setEditable(false);
resultArea.setLineWrap(true);
Container c=getContentPane();
c.setLayout(null);
c.add(cardNumLabel);
cardNumLabel.setBounds(20,20,75,30);
c.add(cardNum);
cardNum.setBounds(70,20,240,30);
c.add(cardpswdLabel);
cardpswdLabel.setBounds(20,60,75,30);
c.add(cardpswd);
cardpswd.setBounds(70,60,240,30);
c.add(inputMoneyLabel);
inputMoneyLabel.setBounds(90,95,250,30);
c.add(inputMoney);
inputMoney.setBounds(70,130,240,30);
c.add(withDrawButton);
withDrawButton.setBounds(100,180,60,40);
c.add(close);
close.setBounds(180,180,60,40);
c.add(resultLabel);
resultLabel.setBounds(120,210,60,50);
JScrollPane scrollPane=new JScrollPane(resultArea);
c.add(scrollPane);
scrollPane.setBounds(20,250,300,80);
withDrawButton.addActionListener(this);
close.addActionListener(this);
show();
}
//事件监听
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==close){ //监听关闭按钮
confirmDialog endwithDraw=new confirmDialog(this,"结束操作","真的要结束取款吗?");
if(endwithDraw.isOkay){
hide();
}
}
else if(ae.getSource()==withDrawButton){ //监听取款按钮
flag=true;
//获取输入内容
getCardNum=cardNum.getText().trim();
getpswd=cardpswd.getText().trim();
getMoney=inputMoney.getText().trim();
//判断输入卡号和密码是否为空
if(getCardNum.equals("")==true&&getpswd.equals("")==true){
JOptionPane.showMessageDialog(null,"请输入卡号和密码!");
}
//判断输入要取款金额是否为空
else if(getMoney.equals("")==true){
JOptionPane.showMessageDialog(null,"请输入要取款的金额!");
}
//取款
else{
command="select * from cardTable where userCardNum='"+getCardNum+"'";
try{
st=connectDB.conDB();
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=BSMS","sa","5201314");
rs=st.executeQuery(command);
if(rs.next()){ //判断卡号是否存在
String pswdValue=rs.getString("code").trim();
String moneyValue=rs.getString("balance").trim();
int moneyInCardValue=Integer.parseInt(moneyValue);
int getMoneyValue=Integer.parseInt(getMoney);
int sum=moneyInCardValue-getMoneyValue;
if(sum<0){
JOptionPane.showMessageDialog(null,"卡上余额不足!");//判断余额足否
flag=false;
}
if(pswdValue.equals(getpswd)==true){//判断卡号密码是否一致
if(flag){
money=Integer.toString(sum);
command="update cardTable set balance='"+money+"' where userCardNum='"+getCardNum+"'";
try{//取款成功
st.executeUpdate(command);
JOptionPane.showMessageDialog(null,"交易成功!");
//输出操作结果
resultArea.append("卡号:"+getCardNum+"\n");
resultArea.append("取出:"+getMoney+"\n"+"系统提示:操作成功完成!\n");
resultArea.append("当前余额:"+money+"\n");
}catch(SQLException ex){
while(ex!=null){
JOptionPane.showMessageDialog(null,ex);
ex.getNextException();
return;
}
}
}
}
else{//卡号密码不一致
JOptionPane.showMessageDialog(null,"卡号和密码不一致!");
flag=false;
confirmDialog reEnter=new confirmDialog(this,"重新输入","重新输入卡号和密码?");
if(reEnter.isOkay){
flag=true;
}
else{
withDraw.this.hide();
}
}
}
else{//卡号不存在
JOptionPane.showMessageDialog(null,"卡号不存在!");
flag=false;
confirmDialog reEnter=new confirmDialog(this,"重新输入","重新输入卡号和密码?");
if(reEnter.isOkay){
flag=true;
}
else{
withDraw.this.hide();
}
}
}catch(SQLException ex){
while(ex!=null){
JOptionPane.showMessageDialog(null,ex);
resultArea.append(ex+"\n");
ex.getNextException();
return;
}
}
}
closeDB.closeDB(st,con);
}
}
public static void main(String args[]){
withDraw withdraw=new withDraw();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -