⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buymaterial.java

📁 以一个酒店的进销存系统为例
💻 JAVA
字号:
package com.csbook.restaurant;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author pengtao
 * @version 1.0
 */

public class BuyMaterial extends JInternalFrame {
   JPanel panel1 = new JPanel();
   BorderLayout borderLayout1 = new BorderLayout();
   JPanel jPanel1 = new JPanel();
   GridLayout gridLayout1 = new GridLayout(4,4);
   JLabel jLabel1 = new JLabel();
   JLabel jLabel3 = new JLabel();
   JLabel jLabel4 = new JLabel();
   JLabel jLabel5 = new JLabel();
   JTextField operator = new JTextField();
   JLabel jLabel6 = new JLabel();
   JLabel jLabel7 = new JLabel();
   JPanel jPanel2 = new JPanel();
   FlowLayout flowLayout1 = new FlowLayout();
   JLabel jLabel8 = new JLabel();
   JPanel jPanel3 = new JPanel();
   JButton ok = new JButton();
   JButton cancel = new JButton();
   JTextField remark = new JTextField();
   JTextField price = new JTextField();
   JTextField discount = new JTextField();
   JLabel jLabel2 = new JLabel();
   JTextField amount = new JTextField();
   String currentOper="";
   JComboBox commodity = new JComboBox();
   Hashtable foodSupplier=new Hashtable();
   JTextField supplier = new JTextField();

   public BuyMaterial(String title,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable,String operator) {
     super(title);
     this.currentOper=operator;
     try {
       jbInit();
       pack();
     }
     catch(Exception ex) {
       ex.printStackTrace();
     }
   }

   private void jbInit() throws Exception {
     panel1.setLayout(borderLayout1);
     jPanel1.setLayout(gridLayout1);
     jLabel1.setText("所进商品:");
     jLabel3.setText("备注:");
     jLabel4.setText("折扣:");
     jLabel5.setText("经手人:");
     jLabel6.setText("单价:");
     jLabel7.setText("供应商:");
     jPanel2.setLayout(flowLayout1);
     jLabel8.setText("进货入库");
     ok.setText("确定");
    ok.addActionListener(new BuyMaterial_ok_actionAdapter(this));
     cancel.setText("取消");
    cancel.addActionListener(new BuyMaterial_cancel_actionAdapter(this));
     remark.setText("");
     operator.setText("");
    price.setText("");
    discount.setText("");
     jLabel2.setText("数量:");
    amount.setText("");
    this.prepareShow();
    commodity.setSelectedIndex(-1);
    commodity.addActionListener(new BuyMaterial_commodity_actionAdapter(this));
    getContentPane().add(panel1);
     panel1.add(jPanel1,  BorderLayout.CENTER);
     jPanel1.add(jLabel1, null);
    jPanel1.add(commodity, null);
    jPanel1.add(jLabel7, null);
    jPanel1.add(supplier, null);
     jPanel1.add(jLabel6, null);
    jPanel1.add(price, null);
     jPanel1.add(jLabel5, null);
     jPanel1.add(operator, null);
     jPanel1.add(jLabel4, null);
    jPanel1.add(discount, null);
     panel1.add(jPanel2,  BorderLayout.NORTH);
     jPanel2.add(jLabel8, null);
     panel1.add(jPanel3, BorderLayout.SOUTH);
     jPanel3.add(ok, null);
     jPanel3.add(cancel, null);
    jPanel1.add(jLabel2, null);
    jPanel1.add(amount, null);
    jPanel1.add(jLabel3, null);
    jPanel1.add(remark, null);
    jPanel1.add(remark, null);
    operator.setText(currentOper);
    operator.setEditable(false);
   }

  void ok_actionPerformed(ActionEvent e) {
    if(price.getText().trim().equals("")||discount.getText().trim().equals("")||amount.getText().trim().equals(""))
    {
      JOptionPane.showMessageDialog(this, "单价、数量和折扣不能为空,无折扣请输入1!", "警告",
                                   JOptionPane.ERROR_MESSAGE);
     return;
    }
    String temp=price.getText().trim();
    try {
      Float.parseFloat(temp);
    }
    catch (NumberFormatException ex) {
     JOptionPane.showMessageDialog(this, "您输入的数据不合法!", "警告",
                                   JOptionPane.ERROR_MESSAGE);
     return;
   }

   temp=discount.getText().trim();
     try {
       float disc=Float.parseFloat(temp);
       if(disc>1||disc<0)
         JOptionPane.showMessageDialog(this, "折扣率只能在0和1之间!", "警告",
                                     JOptionPane.ERROR_MESSAGE);
     }
     catch (NumberFormatException ex) {
       JOptionPane.showMessageDialog(this, "您输入的数据不合法!", "警告",
                                     JOptionPane.ERROR_MESSAGE);
     }

   temp=amount.getText().trim();
   try {
           Float.parseFloat(temp);
         }
   catch (NumberFormatException ex) {
           JOptionPane.showMessageDialog(this, "您输入的数据不合法!", "警告",
                                         JOptionPane.ERROR_MESSAGE);
       }

       Connection conn=null;
       PreparedStatement ps=null;
       ResultSet rs=null;
       try{
          conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
          //建立库存入库单
          ps=conn.prepareStatement("insert into commodityIn(commodity,operator,supplier,amount,price,discount,totalPrice,remark,stockDate) values(?,?,?,?,?,?,?,?,?)");
          ps.setString(1,commodity.getSelectedItem().toString());
          ps.setString(2,operator.getText());
          ps.setString(3,supplier.getText());
          ps.setFloat(4,Float.parseFloat(amount.getText()));
          ps.setFloat(5,Float.parseFloat(price.getText()));
          ps.setFloat(6,Float.parseFloat(discount.getText()));
          ps.setFloat(7,Float.parseFloat(amount.getText())*Float.parseFloat(price.getText())*Float.parseFloat(discount.getText()));
          ps.setString(8,remark.getText());
          java.sql.Date currentDate=new java.sql.Date(System.currentTimeMillis());
          ps.setDate(9,currentDate);
          ps.executeUpdate();
          //更改库存信息
          ps=conn.prepareStatement("select amount from stockInfo where commodity=?");
          ps.setString(1,commodity.getSelectedItem().toString());
          rs=ps.executeQuery();
          float curAmount=0;
          boolean hasRecord=false;
          if(rs.next()){
            hasRecord = true;
            curAmount=rs.getFloat("amount");
          }
          if(hasRecord)
          {
            ps=conn.prepareStatement("update stockInfo set amount=? where commodity=?");
            ps.setFloat(1,curAmount+Float.parseFloat(amount.getText()));
            ps.setString(2,commodity.getSelectedItem().toString());
            ps.executeUpdate();
          }
          else
          {
           ps=conn.prepareStatement("insert into stockInfo(commodity,amount,updateTime) values(?,?,?)");
           ps.setString(1,commodity.getSelectedItem().toString());
           ps.setFloat(2,Float.parseFloat(amount.getText()));
           ps.setDate(3,currentDate);
           ps.executeUpdate();
         }
          JOptionPane.showMessageDialog(this, "已成功进行入库操作!", "操作",JOptionPane.PLAIN_MESSAGE);
        }
            catch(SQLException ex){
            ex.printStackTrace();
            }
            finally{
           if(ps!=null)try{ps.close();}catch(SQLException ignore){}
          if(conn!=null)try{conn.close();}catch(SQLException ignore){}
          }

  }


  void cancel_actionPerformed(ActionEvent e) {
  this.dispose();
  }

  private void prepareShow(){
  Connection conn=null;
  PreparedStatement ps=null;
  ResultSet rs=null;
  try{
    conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
    ps=conn.prepareStatement("select name,producer from food");
    rs=ps.executeQuery();
    while(rs.next())
    {
      commodity.addItem(rs.getString("name"));
      foodSupplier.put(rs.getString("name"),rs.getString("producer"));
    }
  }
  catch(SQLException ex){
      ex.printStackTrace();
      }
  finally{
      if(rs!=null)try{rs.close();}catch(SQLException ignore){}
      if(ps!=null)try{ps.close();}catch(SQLException ignore){}
      if(conn!=null)try{conn.close();}catch(SQLException ignore){}
    }
  }

  void commodity_actionPerformed(ActionEvent e) {
    if(commodity.getSelectedIndex()==-1)
      return;
    String selectedFood=commodity.getSelectedItem().toString();
    supplier.setText((foodSupplier.get(selectedFood)).toString());
  }
 }

class BuyMaterial_ok_actionAdapter implements java.awt.event.ActionListener {
  BuyMaterial adaptee;

  BuyMaterial_ok_actionAdapter(BuyMaterial adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.ok_actionPerformed(e);
  }
}

class BuyMaterial_cancel_actionAdapter implements java.awt.event.ActionListener {
  BuyMaterial adaptee;

  BuyMaterial_cancel_actionAdapter(BuyMaterial adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.cancel_actionPerformed(e);
  }
}

class BuyMaterial_commodity_actionAdapter implements java.awt.event.ActionListener {
  BuyMaterial adaptee;

  BuyMaterial_commodity_actionAdapter(BuyMaterial adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.commodity_actionPerformed(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -