📄 perform.java
字号:
txt_goodsName.setBounds(120,40,100,20);
txt_sellPrice.setBounds(380,40,100,20);
txt_stocks.setBounds(120,70,100,20);
txt_sellQuantity.setBounds(380,70,100,20);
btn_check = new JButton("检查商品库存量");
btn_check.setBounds(280,10,200,20);
btn_check.addActionListener(new CheckPerform());
btn_addToSellList = new JButton("添加到售货列表");
btn_addToSellList.setBounds(520,65,150,30);
btn_addToSellList.addActionListener(new AddToSellListPerform());
jTable = new JTable();
jTable.setModel(defaultTable);
jScrollPane = new JScrollPane(jTable);
jScrollPane.setViewportView(jTable);
jScrollPane.setBounds(20,120,860,250);
btn_balance = new JButton("结 算");
btn_balance.addActionListener(new BalancePerform());
btn_charge = new JButton("收 款");
btn_charge.addActionListener(new ChargePerform());
lab_shouldCash = new JLabel("应收金额");
lab_actualCash = new JLabel("实收金额");
lab_change = new JLabel("找 零");
txt_shouldCash = new JTextField();
txt_actualCash = new JTextField();
txt_change = new JTextField();
btn_balance.setBounds(80,390,100,30);
lab_shouldCash.setBounds(80,430,100,20);
txt_shouldCash.setBounds(180,430,120,20);
lab_actualCash.setBounds(320,430,100,20);
txt_actualCash.setBounds(420,430,120,20);
btn_charge.setBounds(80,460,100,30);
lab_change.setBounds(320,460,100,20);
txt_change.setBounds(420,460,120,20);
sellManage.setTitle("售货管理");
sellManage.setLayout(null);
sellManage.add(lab_goodsId);
sellManage.add(btn_check);
sellManage.add(lab_goodsName);
sellManage.add(lab_sellPrice);
sellManage.add(lab_sellQuantity);
sellManage.add(lab_stocks);
sellManage.add(txt_goodsId);
sellManage.add(txt_goodsName);
sellManage.add(txt_sellPrice);
sellManage.add(txt_sellQuantity);
sellManage.add(txt_stocks);
sellManage.add(btn_addToSellList);
sellManage.add(jScrollPane);
sellManage.add(btn_balance);
sellManage.add(lab_shouldCash);
sellManage.add(txt_shouldCash);
sellManage.add(lab_actualCash);
sellManage.add(txt_actualCash);
sellManage.add(btn_charge);
sellManage.add(lab_change);
sellManage.add(txt_change);
sellManage.setVisible(true);
//sellManage.setResizable(false);
desktop.add(sellManage,3);
}
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);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
return;
}
sql = "select * from stock where goodsId = '" + txt_goodsId.getText() + "'" ;
rst = stmt.executeQuery(sql);
if(rst.next()) {
txt_goodsName.setText(rst.getString(2));
txt_sellPrice.setText(rst.getString(6));
txt_stocks.setText(rst.getString(3));
//txt_retailPrice.setText(rst.getString(6));
}
else {
JOptionPane.showMessageDialog(desktop,"库中没有此种商品!","提示",1);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
return;
}
}
catch (Exception e1) {
//e1.printStackTrace();
JOptionPane.showMessageDialog(desktop,"请检查数据库连接!","错误",0);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
return;
}
}
}
class AddToSellListPerform implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
Vector vector = new Vector();//
JDialog jdTip = new JDialog();
JLabel tip = new JLabel();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str_date = sdf.format(cal.getTime());
int stocks,sellQuantity;
stocks = Integer.valueOf(txt_stocks.getText());
sellQuantity = Integer.valueOf(txt_sellQuantity.getText());
if(stocks<sellQuantity) {
JOptionPane.showMessageDialog(desktop,"库存商品不足!","警告",2);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
return;
}
else {
DefaultTableModel defTableModel = (DefaultTableModel)jTable.getModel();
int columNum = defTableModel.getColumnCount();
// txt_goodsId,txt_goodsName,txt_sellPrice,txt_sellQuantity,txt_sellDate;
//System.out.println("columNum = " + columNum);
vector.add(txt_goodsId.getText().trim());
vector.add(txt_goodsName.getText().trim());
vector.add(txt_sellPrice.getText().trim());
vector.add(txt_sellQuantity.getText().trim());
vector.add(str_date);
defTableModel.getDataVector().add(vector);
txt_goodsId.setText("");
txt_goodsName.setText("");
txt_sellPrice.setText("");
txt_sellQuantity.setText("");
txt_stocks.setText("");
((DefaultTableModel)jTable.getModel()).fireTableDataChanged();
}
}
}
class BalancePerform implements ActionListener {
double sum ;
int i;
javax.swing.table.TableModel model = jTable.getModel();
public void actionPerformed(ActionEvent e) {
int numRows = jTable.getRowCount();
// TODO 自动生成方法存根
sum = 0;
for(i = 0 ; i < numRows ; i ++ ) {
//purPrice = Float.valueOf((String)model.getValueAt(i,2));
//purQuantity = Integer.valueOf((String)model.getValueAt(i,3));
sum += Float.valueOf((String)model.getValueAt(i,2))*Integer.valueOf((String)model.getValueAt(i,3));
//System.out.println("in sum = " + sum);
}
//System.out.println("sum = " + sum);
BigDecimal bd = new BigDecimal(sum);
BigDecimal bd1 = bd.setScale(3,bd.ROUND_HALF_UP);
sum = bd1.doubleValue();
//System.out.println("sum = " + sum);
//将原来的值清空
if(!txt_shouldCash.getText().equals(""))
txt_shouldCash.setText("");
if(!txt_actualCash.getText().equals(""))
txt_actualCash.setText("");
if(!txt_change.getText().equals(""))
txt_change.setText("");
txt_shouldCash.setText(Double.toString(sum));
}
}
class ChargePerform implements ActionListener {//收款,同时更新数据库
double shouldCash,actualCash,change;
String goodsId,goodsName,sellDate;
float sellPrice,retailPrice;
int sellQuantity,stocks,customerId;//采购数量,库存量,客户id
javax.swing.table.TableModel model = jTable.getModel();
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
shouldCash = Double.valueOf(txt_shouldCash.getText());
actualCash = Double.valueOf(txt_actualCash.getText());
if(txt_actualCash.getText().equals("")||shouldCash > actualCash) {
JOptionPane.showMessageDialog(desktop,"请检查实收金额!","警告",2);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
return;
}
change = actualCash - shouldCash;
BigDecimal bd = new BigDecimal(change);
BigDecimal bd1 = bd.setScale(3,bd.ROUND_HALF_UP);
change = bd1.doubleValue();
//System.out.println("change = " + Double.toString(change));
txt_change.setText(Double.toString(change));
int numRows = jTable.getRowCount();
// int numCols = jTable.getColumnCount();
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(customerId)from sell";
rst = stmt.executeQuery(sql);
if(rst.next()) {
customerId = rst.getInt(1) + 1;
}
else {
customerId = 1;
}
for(i = 0; i < numRows; i++) {
goodsId =(String) model.getValueAt(i,0);
goodsName = (String)model.getValueAt(i,1);
sellPrice = Float.valueOf((String)model.getValueAt(i,2));
sellQuantity = Integer.valueOf((String)model.getValueAt(i,3));
sellDate = (String)model.getValueAt(i,4);
//更新stock表
sql = "select * from stock where goodsId = '" + goodsId + "'";
rst = stmt.executeQuery(sql);
if(rst.next()) {
stocks = rst.getInt("stocks") - sellQuantity ;
}
sql = "update stock set stocks = " + stocks + " where goodsId = '" + goodsId + "'";
stmt.executeUpdate(sql);
//System.out.println("更新stock表");
//更新sell表
sql = "insert into sell values (" + customerId + ",'" + goodsId + "'," + sellQuantity + ",'" + sellDate + "'," + sellPrice + ")";
stmt.execute(sql);
//System.out.println("更新sell表");
}
//清空JTable表
((DefaultTableModel)jTable.getModel()).getDataVector().removeAllElements();
((DefaultTableModel)jTable.getModel()).fireTableDataChanged();
JOptionPane.showMessageDialog(desktop,"商品成功售出,已更新库存!","提示",1);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
}
catch (Exception ce) {
//ce.printStackTrace();
}
}
}
}
//stocksQuery,purQuery,sellQuery,exitSys
class StocksQueryPerform extends Perform {//库存查询:按药品代码,药品名称查询。
private MyInternalFrame stockQuery = new MyInternalFrame();
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private String []columnNames={"商品编码","商品名称","库存量","生产厂家","规格","零售价格"};
private Object[][]p ={};
private DefaultTableModel defaultTable = new DefaultTableModel(p,columnNames);
private JLabel lab_goodsId, lab_goodsName;
private JTextField txt_goodsId,txt_goodsName;
private JButton btn_QueryAsGoodsId,btn_QueryAsGoodsName;
public StocksQueryPerform(JDesktopPane desktop) {
this.desktop = desktop;
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
lab_goodsId = new JLabel("商品代码");
lab_goodsName = new JLabel("商品名称");
txt_goodsId = new JTextField();
txt_goodsName = new JTextField();
btn_QueryAsGoodsId = new JButton("按商品代码查询");
btn_QueryAsGoodsName = new JButton("按商品名称查询");
lab_goodsId.setBounds(20,10,100,20);
lab_goodsName.setBounds(20,40,100,20);
txt_goodsId.setBounds(140,10,100,20);
txt_goodsName.setBounds(140,40,100,20);
btn_QueryAsGoodsId.setBounds(280,10,180,20);
btn_QueryAsGoodsName.setBounds(280,40,180,20);
btn_QueryAsGoodsId.addActionListener(new QueryAsGoodsIdPerform());
btn_QueryAsGoodsName.addActionListener(new QueryAsGoodsNamePerform());
jTable = new JTable();
jTable.setModel(defaultTable);
jScrollPane = new JScrollPane(jTable);
jScrollPane.setViewportView(jTable);
jScrollPane.setBounds(20,70,860,500);
stockQuery.setTitle("库存查询");
stockQuery.setLayout(null);
stockQuery.add(lab_goodsId);
stockQuery.add(lab_goodsName);
stockQuery.add(txt_goodsId);
stockQuery.add(txt_goodsName);
stockQuery.add(btn_QueryAsGoodsId);
stockQuery.add(btn_QueryAsGoodsName);
stockQuery.add(jScrollPane);
stockQuery.setVisible(true);
desktop.add(stockQuery,0);
}
class QueryAsGoodsIdPerform implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
Vector vector = new Vector();
DefaultTableModel defTableModel = (DefaultTableModel)jTable.getModel();
//药品编码","药品名称","库存量","生产厂家","规格","零售价格
String goodsId,goodsName,manufacturer,standard;
int stocks;
float retailPrice;
((DefaultTableModel)jTable.getModel()).getDataVector().removeAllElements();
((DefaultTableModel)jTable.getModel()).fireTableDataChanged();
try {
DBConnection dbc = new DBConnection();
Connection conn = dbc.getDBConnection();
Statement stmt = conn.createStatement();
String sql;
ResultSet rst;
goodsId = txt_goodsId.getText();
sql = "select * from stock where goodsId = '" + goodsId + "'";
rst = stmt.executeQuery(sql);
if(rst.next()) {
goodsName = rst.getString(2);
stocks = rst.getInt(3);
manufacturer = rst.getString(4);
standard = rst.getString(5);
retailPrice = rst.getFloat(6);
vector.add(goodsId);
vector.add(goodsName);
vector.add(stocks);
vector.add(manufacturer);
vector.add(standard);
vector.add(retailPrice);
defTableModel.getDataVector().add(vector);
((DefaultTableModel)jTable.getModel()).fireTableDataChanged();
}
else {
JOptionPane.showMessageDialog(desktop,"没有满足条件的查询结果!","提示",1);//0是错误类型x,1是信息类型圆形i,2是警告三角i,3是问号
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -