📄 goodsoutpane.java
字号:
package file2;
import java.sql.*;
import java.text.*;
import java.util.StringTokenizer;
import java.util.Calendar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GoodsOutPane extends JPanel implements ActionListener{
//声明数据库连接组件
private DBConnection con=null;
private SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
private JLabel tip=null;
private JLabel name=null;
private JLabel goods_In_Quantity=null;
private JLabel goods_In_Time=null;
private JComboBox comboBox=null;
private JTextField goods_In_Quantity_TF=null;
private JTextField goods_In_Time_TF=null;
private JButton insert=null;
private JButton reset=null;
private JPanel tipPane=null;
private JPanel namePane=null;
//private JScrollPane listPane=null;
private JPanel inAndOutPane=null;
private JPanel buttonsPane=null;
public GoodsOutPane(){
try{
con=new DBConnection();
tip=new JLabel("请在下面录入出货信息:");
name=new JLabel("名称:");
goods_In_Quantity=new JLabel("出货数量:");
goods_In_Time=new JLabel("出货时间(请按给出的格式输入):");
goods_In_Quantity_TF=new JTextField(10);
goods_In_Time_TF=new JTextField(10);
insert=new JButton("插入");
reset=new JButton("重置");
Calendar now=Calendar.getInstance();
String date=formatter.format(now.getTime());
goods_In_Time_TF.setText(date);
//声明面版
tipPane=new JPanel();
tipPane.setLayout(new FlowLayout(FlowLayout.LEFT));
tipPane.add(tip);
namePane=new JPanel();
namePane.setLayout(new FlowLayout(FlowLayout.LEFT));
namePane.add(name);
//声明具体商品名称下拉列表
String[] dataOfList=new String[300];
dataOfList[0]="请选择具体商品名称:";
int count=1;
String querySql="select* from Third_Catalog";
ResultSet set=con.executeSelect(querySql);
while(set.next()){
String name=set.getString("Third_name");
dataOfList[count++]=name;
}
comboBox=new JComboBox(dataOfList);
comboBox.setSelectedIndex(0);
namePane.add(comboBox);
//listPane=new JScrollPane(list);
inAndOutPane=new JPanel();
inAndOutPane.setLayout(new GridLayout(2,2));
inAndOutPane.add(goods_In_Quantity);
inAndOutPane.add(goods_In_Quantity_TF);
inAndOutPane.add(goods_In_Time);
inAndOutPane.add(goods_In_Time_TF);
buttonsPane=new JPanel();
buttonsPane.setLayout(new GridLayout(1,2));
buttonsPane.add(insert);
buttonsPane.add(reset);
//在当前面版中加入各个面版
this.setLayout(new GridLayout(4,1));
this.add(tipPane);
this.add(namePane);
//this.add(listPane);
this.add(inAndOutPane);
this.add(buttonsPane);
insert.addActionListener(this);
reset.addActionListener(this);
}catch(SQLException sql){
JOptionPane.showMessageDialog(null, "发生SQL错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
comboBox.setSelectedIndex(0);
goods_In_Quantity_TF.setText("");
goods_In_Time_TF.setText(formatter.format(Calendar.getInstance().getTime()));
return;
}
if(e.getSource()==insert){
try{
String belongsTo=(String)comboBox.getSelectedItem();
if(belongsTo.equals("请选择具体商品名称:")){
JOptionPane.showMessageDialog(null, "您未选择商品名称!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}
String queryID="select* from Third_Catalog where Third_name='"+belongsTo+"'";
ResultSet rs=con.executeSelect(queryID);
int Third_ID=0;
int store_IN=0;
if(rs.next()){
Third_ID=rs.getInt("Third_ID");
store_IN=rs.getInt("store_in");
}
rs=null;
int quantity=(new Integer(goods_In_Quantity_TF.getText().trim())).intValue();
if(store_IN<quantity){
JOptionPane.showMessageDialog(null, "你的数据录入有误,库存不足!只有 "+store_IN, "警告", JOptionPane.ERROR_MESSAGE);
return;
}
String date=goods_In_Time_TF.getText().trim();
int dateValue[]=new int[3];
int start=0;
StringTokenizer token=new StringTokenizer(date,"-");
while(token.hasMoreTokens()){
dateValue[start++]=(new Integer(token.nextToken())).intValue();
}
if(start!=3){
JOptionPane.showMessageDialog(null, "日期格式输入有误!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}
Calendar now=Calendar.getInstance();
now.clear();
now.set(dateValue[0], dateValue[1]-1, dateValue[2]);
java.util.Date dateGet=now.getTime();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
String dateStr=formatter.format(dateGet);
String query="select* from operationRecord where To_Third_name='"+belongsTo+
"' and quantity="+quantity+" and operateTime='"+dateStr+"' and operationState=1";
rs=con.executeSelect(query);
if(rs.next()){
int choice=JOptionPane.showConfirmDialog(null,"数据库中已经存在该记录,"+
"您确认在同一天又发出同样数量的货吗?" ,"确认",JOptionPane.YES_NO_OPTION);
if(choice==JOptionPane.YES_OPTION){
String insertSql="insert into operationRecord values('"+belongsTo+"',"+quantity+",'"+dateStr+"',1)";
con.executeDML(insertSql);
String updateSql="update Third_Catalog set store_in=store_in-"+quantity
+" where Third_name='"+belongsTo+"'";
con.executeDML(updateSql);
JOptionPane.showMessageDialog(null, "记录录入成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
return;
}
String insertSql="insert into operationRecord values('"+belongsTo+"',"+quantity+",'"+dateStr+"',1)";
con.executeDML(insertSql);
String updateSql="update Third_Catalog set store_in=store_in-"+quantity
+" where Third_name='"+belongsTo+"'";
con.executeDML(updateSql);
JOptionPane.showMessageDialog(null, "记录录入成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
}catch(Exception excep){
JOptionPane.showMessageDialog(null, "您的输入有误!", "警告", JOptionPane.ERROR_MESSAGE);
//excep.printStackTrace();
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -