📄 perform.java
字号:
package com.smmis;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
//import com.app.MyInternalFrame;
class Perform extends JFrame implements ActionListener {
public JDesktopPane desktop;
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
}
}
class PurchasePerform extends Perform {//实现进药功能
//商品编码,商品名称,采购价,采购数量,采购日期,供应商,生产厂家,规格,零售价
private JLabel lab_goodsId,lab_goodsName,lab_purPrice,lab_purQuantity,lab_purDate,lab_dateForm,lab_supplier,lab_manufacturer,lab_standard,lab_retailPrice;
private JTextField txt_goodsId,txt_goodsName,txt_purPrice,txt_purQuantity,txt_purDate,txt_supplier,txt_manufacturer,txt_standard,txt_retailPrice;
private MyInternalFrame purManage = new MyInternalFrame();
private JScrollPane jScrollPane = null;
private JTable jTable = null;
//private JPanel jPanel = null;
private String []columnNames={"商品编码","商品名称","采购价格","采购数量","采购日期","供应商","生产厂家","规格","零售价格"};
private Object[][]p ={};
private DefaultTableModel defaultTable = new DefaultTableModel(p,columnNames);
public PurchasePerform(JDesktopPane desktop) {
this.desktop = desktop;
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
lab_goodsId = new JLabel("商品编码");
lab_goodsId.setBounds(20,10,100,20);
lab_goodsName = new JLabel("商品名称");
lab_goodsName.setBounds(20,40,100,20);
lab_purPrice = new JLabel("采购价格");
lab_purPrice.setBounds(20,70,100,20);
lab_purQuantity = new JLabel("采购数量");
lab_purQuantity.setBounds(20,100,100,20);
lab_purDate = new JLabel("采购日期");
lab_purDate.setBounds(20,130,100,20);
lab_dateForm = new JLabel("(格式:YYYY-MM-DD)");
lab_dateForm.setBounds(220,130,150,20);
lab_supplier = new JLabel("供应商");
lab_supplier.setBounds(20,160,100,20);
lab_manufacturer = new JLabel("生产厂家");
lab_manufacturer.setBounds(20,190,100,20);
lab_standard = new JLabel("规 格");
lab_standard.setBounds(20,220,100,20);
lab_retailPrice = new JLabel("零售价格");
lab_retailPrice.setBounds(20,250,100,20);
txt_goodsId = new JTextField();
txt_goodsId.setBounds(120,10,100,20);
txt_goodsName = new JTextField();
txt_goodsName.setBounds(120,40,100,20);
txt_purPrice = new JTextField();
txt_purPrice.setBounds(120,70,100,20);
txt_purQuantity = new JTextField();
txt_purQuantity.setBounds(120,100,100,20);
txt_purDate = new JTextField();
txt_purDate.setBounds(120,130,100,20);
txt_supplier = new JTextField();
txt_supplier.setBounds(120,160,100,20);
txt_manufacturer = new JTextField();
txt_manufacturer.setBounds(120,190,100,20);
txt_standard = new JTextField();
txt_standard.setBounds(120,220,100,20);
txt_retailPrice = new JTextField();
txt_retailPrice.setBounds(120,250,100,20);
jTable = new JTable();
jTable.setModel(defaultTable);
jScrollPane = new JScrollPane(jTable);
jScrollPane.setViewportView(jTable);
jScrollPane.setBounds(20,310,860,180);
JButton btn_bePurchased = new JButton("检查是否进过此商品");
btn_bePurchased.setBounds(400,10,200,20);
btn_bePurchased.addActionListener(new CheckPerform());
JButton btn_addPurList = new JButton("添加到进货列表");
btn_addPurList.setBounds(400,250,200,20);
btn_addPurList.addActionListener(new AddToListPerform());
JButton btn_addToStock = new JButton("入库");
btn_addToStock.setBounds(400,510,100,20);
btn_addToStock.addActionListener(new AddToStockPerform());
//JInternalFrame purManage = new JInternalFrame("进药管理" );
purManage.setTitle("进货管理");
purManage.setLayout(null);
purManage.add(lab_goodsId); purManage.add(txt_goodsId);
purManage.add(lab_goodsName); purManage.add(txt_goodsName);
purManage.add(lab_purPrice); purManage.add(txt_purPrice);
purManage.add(lab_purQuantity);purManage.add(txt_purQuantity);
purManage.add(lab_purDate); purManage.add(txt_purDate);
purManage.add(lab_supplier); purManage.add(txt_supplier);
purManage.add(lab_manufacturer); purManage.add(txt_manufacturer);
purManage.add(lab_standard); purManage.add(txt_standard);
purManage.add(lab_retailPrice); purManage.add(txt_retailPrice);
purManage.add(btn_bePurchased); purManage.add(btn_addPurList);
purManage.add(lab_dateForm);
//purManage.add(jPanel);
purManage.add(jScrollPane);
purManage.add(btn_addToStock);
purManage.setVisible(true);
//purManage.setResizable(false);
desktop.add(purManage,4);
try {
purManage.setSelected(true);
} catch (java.beans.PropertyVetoException e1) {}
}
class CheckPerform implements ActionListener {
// 检查是否是第一次购进此种药物
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
try {
DBConnection dbc = new DBConnection();
Connection conn = dbc.getDBConnection();
Statement stmt = conn.createStatement();
String sql;
ResultSet rst;
if(txt_goodsId.getText().equals("")) {
JOptionPane.showMessageDialog(desktop,"商品编码为空!","警告",2);
return;
}
sql = "select * from stock where goodsId = '" + txt_goodsId.getText() + "'" ;
rst = stmt.executeQuery(sql);
if(rst.next()) {
txt_goodsName.setText(rst.getString(2));
txt_manufacturer.setText(rst.getString(4));
txt_standard.setText(rst.getString(5));
txt_retailPrice.setText(rst.getString(6));
}
else {
JOptionPane.showMessageDialog(desktop,"没有进过此商品,请输入商品详细信息!","提示",1);
return;
}
}
catch (Exception e1) {
JOptionPane.showMessageDialog(desktop,"请检查数据库连接!","错误",0);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
//e1.printStackTrace();
return;
}
}
}
class AddToListPerform implements ActionListener {//内部类实现添加进药信息到进药列表
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
Vector vector = new Vector();
DefaultTableModel defTableModel = (DefaultTableModel)jTable.getModel();
// int columNum = defTableModel.getRowCount();
// txt_goodsId,txt_goodsName,txt_purPrice,txt_purQuantity,txt_purDate,txt_supplier,txt_manufacturer,txt_standard,txt_retailPrice
if((txt_goodsId.getText().equals(""))||(txt_goodsName.getText().equals(""))
||(txt_purPrice.getText().equals(""))||(txt_purQuantity.getText().equals(""))
||(txt_purDate.getText().equals(""))||(txt_supplier.getText().equals(""))
||(txt_manufacturer.getText().equals(""))||(txt_standard.getText().equals(""))
||(txt_retailPrice.getText().equals(""))) {
JOptionPane.showMessageDialog(desktop,"信息项填写不完全,不能添加到列表!","警告",2);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
//System.out.println("存在某个信息没有填写!");
return ;
}
else {
vector.add(txt_goodsId.getText().trim());
vector.add(txt_goodsName.getText().trim());
vector.add(txt_purPrice.getText().trim());
vector.add(txt_purQuantity.getText().trim());
vector.add(txt_purDate.getText().trim());
vector.add(txt_supplier.getText().trim());
vector.add(txt_manufacturer.getText().trim());
vector.add(txt_standard.getText().trim());
vector.add(txt_retailPrice.getText().trim());
defTableModel.getDataVector().add(vector);
txt_goodsId.setText("");
txt_goodsName.setText("");
txt_purPrice.setText("");
txt_purQuantity.setText("");
txt_purDate.setText("");
txt_supplier.setText("");
txt_manufacturer.setText("");
txt_standard.setText("");
txt_retailPrice.setText("");
((DefaultTableModel)jTable.getModel()).fireTableDataChanged();
}
}
}
class AddToStockPerform implements ActionListener {//将表中信息添加到数据库中
//int numRows = jTable.getRowCount();
//int numCols = jTable.getColumnCount();
String goodsId,goodsName,purDate,supplier,manufacturer,standard;
float purPrice,retailPrice;
int purQuantity,stocks,receiptId;//采购数量,库存量,单据号
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
//注意:对于行和列的定义需要在响应函数之内,否则对于行的定义总会是table表初始化定义时的值;
int numRows = jTable.getRowCount();
// int numCols = jTable.getColumnCount();
javax.swing.table.TableModel model = jTable.getModel();
try{
DBConnection dbc = new DBConnection();
Connection conn = dbc.getDBConnection();
Statement stmt = conn.createStatement();
String sql;
ResultSet rst;
int i;
//System.out.println("numRows = " + numRows + " numCols" + numCols);
sql = "select max(receiptId)from purchase";
rst = stmt.executeQuery(sql);
if(rst.next()) {
receiptId = rst.getInt(1) + 1;
}
else {
receiptId = 1;
}
//List list;
for(i = 0; i <numRows; i++) {
goodsId =(String) model.getValueAt(i,0);
goodsName = (String)model.getValueAt(i,1);
purPrice = Float.valueOf((String)model.getValueAt(i,2));
purQuantity = Integer.valueOf((String)model.getValueAt(i,3));
purDate = (String)model.getValueAt(i,4);
supplier = (String)model.getValueAt(i,5);
manufacturer = (String)model.getValueAt(i,6);
standard = (String)model.getValueAt(i,7);
retailPrice = Float.valueOf((String)model.getValueAt(i,8));
//System.out.println("retailPrice = " + retailPrice);
//写入到stock表
sql = "select stocks from stock where goodsId = '" + goodsId + "'";
rst = stmt.executeQuery(sql);
if(rst.next()) {
stocks = rst.getInt("stocks") + purQuantity;
//System.out.println("stocks = " + stocks);
// 注意:下条sql语句中where前面一定要有空格,否则会出现mysql数据库的语法错误
sql = "update stock set stocks = " + stocks + " where goodsId = '" + goodsId + "'";
stmt.executeUpdate(sql);
//System.out.println("更新stock表");
}
else {
sql = "insert into stock values('" + goodsId + "','" + goodsName + "'," + purQuantity + ",'" + manufacturer + "','" + standard + "'," + retailPrice + ')';
stmt.execute(sql);
//System.out.println("in写入stock表");
}
//System.out.println("写入stock表");
//写入purchase表
sql = "insert into purchase values(" + receiptId + ",'"+ goodsId +"'," + purPrice + "," + purQuantity +",'" + purDate + "','" + supplier + "')";
stmt.execute(sql);
//System.out.println("写入purchase表");
//System.out.println("数据成功导入!");
//System.out.println("第" + i + "行数据:" +goodsId + goodsName + purPrice + purQuantity + purDate + supplier + manufacturer + standard + retailPrice +"\n");
}
JOptionPane.showMessageDialog(desktop,"商品成功入库!","提示",1);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
//清空table表中内容
((DefaultTableModel)jTable.getModel()).getDataVector().removeAllElements();
((DefaultTableModel)jTable.getModel()).fireTableDataChanged();
}
catch (Exception ce) {
JOptionPane.showMessageDialog(desktop,"添加到库存出现错误!","错误",0);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
ce.printStackTrace();
}
}
}
}
class SellPerform extends Perform {//实现售药功能
private JLabel lab_goodsId,lab_goodsName,lab_sellPrice,lab_sellQuantity,lab_stocks;
//应收现金,实收现金,找零
private JLabel lab_shouldCash,lab_actualCash,lab_change;
private JTextField txt_goodsId,txt_goodsName,txt_sellPrice,txt_sellQuantity,txt_stocks;
private JTextField txt_shouldCash,txt_actualCash,txt_change;
//按钮:添加到售药列表,结算,收款
private JButton btn_check,btn_addToSellList,btn_balance,btn_charge;
private MyInternalFrame sellManage = new MyInternalFrame();
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private String []columnNames={"商品编码","商品名称","单价","销售数量","销售日期"};
private Object[][]p = {};
private DefaultTableModel defaultTable = new DefaultTableModel(p,columnNames);
public SellPerform(JDesktopPane desktop) {
this.desktop = desktop;
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
lab_goodsId = new JLabel("商品编码");
lab_goodsId.setBounds(20,10,100,20);
lab_goodsName = new JLabel("商品名称");
lab_goodsName.setBounds(20,40,100,20);
lab_sellPrice = new JLabel("单 价");
lab_sellPrice.setBounds(280,40,100,20);
lab_stocks = new JLabel("库存量");
lab_stocks.setBounds(20,70,100,20);
lab_sellQuantity = new JLabel("销售数量");
lab_sellQuantity.setBounds(280,70,100,20);
txt_goodsId = new JTextField();
txt_goodsName = new JTextField();
txt_sellPrice = new JTextField();
txt_sellQuantity = new JTextField();
txt_stocks = new JTextField();
//txt_sellDate.setText(Integer.toString(date.getDate()));
txt_goodsId.setBounds(120,10,100,20);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -