JavaSwing进销存之商品管理模块(项目第三天)

二、商品管理模块

分析关联的表

kind表

position表

goods表

 

先写类别,再写货架,再写商品

先写商品,再写类别,再写货架

先讲商品表:

业务:三表联查,将数据显示在表格中。

`select goods.goodsId,goods.`name`,goods.barcode,goods.price,goods.number,position.`name` as pname,kind.`name` as kname from kind inner join goods on kind.kindId = goods.kindId inner join position on goods.positionId = position.positionId`

下拉列表框:指类别和货架,为什么?因为下拉列表中的类别名称、货架名称来自数据库表中查询出来的数据。

查所有的类别名称

select name from kind

查所有的货架名称

select name from position

 

根据类别名称查类别的id

select kindid from kind where name = '保健品'

根据货柜名称查货柜的id

select positionId from position where name = 'A1'

如果类别正在使用,不能删除,货架同理。

如何判断类别是否在用?

用当前的货框id去商品表查,如果查得到说明在用,如果查不到说明没有被使用。

select goodsId from goods where positionId = 1
​
select goodsId from goods where kindId = 1

效果图:

实体代码

package com.store.entity;

/**
 * @ClassName Goods
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/9/1 0001-9:06
 * @Version V1.0
 **/
public class Goods {
    /**
     * 商品i
     */
    private Integer goodsId;
    /**
     * 商品名称
     */
    private String name;
    /**
     * 条形码
     */
    private String barcode;
    /**
     * 价格
     */
    private Float price;
    /**
     * 数量
     */
    private Integer number;
    /**
     * 外键
     */
    private Integer positionId;
    /**
     * 外键
     */
    private Integer kindId;
    /**
     * 商品图片
     */
    private String icon;
    /**
     * 类别名称
     */
    private String kname;
    /**
     * 货架名称
     */
    private String pname;

    public Goods() {
    }

    public Goods(Integer goodsId, String name, String barcode, Float price, Integer number, Integer positionId, Integer kindId, String icon) {
        this.goodsId = goodsId;
        this.name = name;
        this.barcode = barcode;
        this.price = price;
        this.number = number;
        this.positionId = positionId;
        this.kindId = kindId;
        this.icon = icon;
    }

    public Goods(String name, String barcode, Float price, Integer number, Integer positionId, Integer kindId, String icon) {
        this.name = name;
        this.barcode = barcode;
        this.price = price;
        this.number = number;
        this.positionId = positionId;
        this.kindId = kindId;
        this.icon = icon;
    }

    public Integer getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Integer getPositionId() {
        return positionId;
    }

    public void setPositionId(Integer positionId) {
        this.positionId = positionId;
    }

    public Integer getKindId() {
        return kindId;
    }

    public void setKindId(Integer kindId) {
        this.kindId = kindId;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getKname() {
        return kname;
    }

    public void setKname(String kname) {
        this.kname = kname;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "goodsId=" + goodsId +
                ", name='" + name + '\'' +
                ", barcode='" + barcode + '\'' +
                ", price=" + price +
                ", number=" + number +
                ", positionId=" + positionId +
                ", kindId=" + kindId +
                ", icon='" + icon + '\'' +
                '}';
    }
}

导出Excel代码

package com.store.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExportExcel {
    //要处理的表格
    JTable table;
    //文件输出流
    FileOutputStream fos;
    //文件选择对话框
    JFileChooser jfc = new JFileChooser();

    // 导出Excel 的数据来自哪张表
    public ExportExcel(JTable table) {
        this.table = table;
        // 过滤 只接收后缀名是xls的文件
        jfc.addChoosableFileFilter(new FileFilter() {
            public boolean accept(File file) {
                return (file.getName().indexOf("xls") != -1);
            }

            public String getDescription() {
                return "Excel";
            }
        });
        // 弹出保存对话框
        jfc.showSaveDialog(null);
        // 获得保存文件的路径
        File file = jfc.getSelectedFile();
        try {
            // 输出到哪里 路径+文件名 d:/t57/store.xls
            this.fos = new FileOutputStream(file + ".xls");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public void export() {
        // 创建工作薄
        HSSFWorkbook wb = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet hs = wb.createSheet();
        // 获得表格模型,传过来的表格,我们要拿出里面的数据
        TableModel tm = table.getModel();
        // 有多少行
        int row = tm.getRowCount();
        // 有多少列
        int cloumn = tm.getColumnCount();
        // System.out.println("row " + row + "  column  " + cloumn);
        // 创建excel的单元格
        HSSFCellStyle style = wb.createCellStyle();
        // 周边的线的样式
        // 底
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        // 左
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 右
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 上
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 前景色,文字的颜色
        style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
        // Pattern模式,实线 虚线 点划线
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        // 字体
        HSSFFont font = wb.createFont();
        // 字号
        font.setFontHeightInPoints((short) 11);
        // 把字体告诉样式
        style.setFont(font);
        // 单元格样式
        HSSFCellStyle style1 = wb.createCellStyle();
        // 苗条型
        style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //
        style1.setFillForegroundColor(HSSFColor.ORANGE.index);
        style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        // 字体
        HSSFFont font1 = wb.createFont();
        font1.setFontHeightInPoints((short) 15);
        font1.setBoldweight((short) 700);
        style1.setFont(font);
        // 循环 行
        for (int i = 0; i < row + 1; i++) {
            // 造一行
            HSSFRow hr = hs.createRow(i);

            // 列
            for (int j = 0; j < cloumn; j++) {
                if (i == 0) {
                    // 表头
                    // 获得列名
                    String value = tm.getColumnName(j);
                    System.out.println("value " + value);
                    int len = value.length();

                    //hs.setColumnWidth(j, (len * 400));
                    hs.setColumnWidth(j, 200);
                    System.out.println(hs.getColumnWidth(j));
                    HSSFRichTextString srts = new HSSFRichTextString(value);
                    HSSFCell hc = hr.createCell(j);

                    hc.setCellStyle(style1);
                    hc.setCellValue(srts);

                } else {
                    String vv = tm.getColumnName(j);
                    // System.out.println("value " + value);
                    int len = vv.length();
                    // 算列宽(导出去是合适的列宽)
                    hs.setColumnWidth(j, (len * 400));
                    // System.out.println("vlue  " + tm.getValueAt(i - 1, j));
                    // 取值
                    if (tm.getValueAt(i - 1, j) != null) {
                        // 数据
                        String value = tm.getValueAt(i - 1, j).toString();
                        HSSFRichTextString srts = new HSSFRichTextString(value);
                        HSSFCell hc = hr.createCell((short) j);
                        hc.setCellStyle(style);

                        if (value.equals("") || value == null) {
                            hc.setCellValue(new HSSFRichTextString(""));
                        } else {
                            hc.setCellValue(srts);
                        }
                    }
                }
            }

        }
        // hs.setColumnWidth(0, 400);
        try {
            wb.write(fos);
            fos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

实现类代码

package com.store.dao.impl;

import com.store.dao.GoodsDao;
import com.store.db.DBHelper;
import com.store.entity.Goods;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GoodsDaoImpl
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/9/1 0001-9:33
 * @Version V1.0
 **/
public class GoodsDaoImpl extends DBHelper implements GoodsDao {
    @Override
    public List<Goods> findAll() {
        List<Goods> lists = null;
        conn = getConnection();
        String sql = "select goods.goodsId,goods.`name`,goods.barcode,goods.price,goods.number,position.`name` as pname,kind.`name` as kname from kind inner join goods on kind.kindId = goods.kindId inner join position on goods.positionId = position.positionId";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if (rs != null) {
                lists = new ArrayList<>();
                while (rs.next()) {
                    Goods goods = new Goods();
                    //装
                    goods.setGoodsId(rs.getInt("goodsId"));
                    goods.setName(rs.getString("name"));
                    goods.setBarcode(rs.getString("barcode"));
                    goods.setPrice(rs.getFloat("price"));
                    goods.setNumber(rs.getInt("number"));
                    goods.setPname(rs.getString("pname"));
                    goods.setKname(rs.getString("kname"));
                    lists.add(goods);
                }
            }
        } catch (SQLException e) {

        } finally {
            closeAll(rs, pstmt, conn);
        }
        return lists;
    }
}

服务层代码

package com.store.service;

import com.store.entity.Goods;

import java.util.List;

public interface GoodsService {
    /**
     * 查询所有商品信息
     *
     * @return 商品信息的集合
     */
    List<Goods> findAll();
}
package com.store.service.impl;

import com.store.dao.GoodsDao;
import com.store.dao.impl.GoodsDaoImpl;
import com.store.entity.Goods;
import com.store.service.GoodsService;

import java.util.List;

/**
 * @ClassName GoodsServiceImpl
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/9/1 0001-9:41
 * @Version V1.0
 **/
public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public List<Goods> findAll() {
        return goodsDao.findAll();
    }
}

界面代码

package com.store.ui;

import com.store.entity.Goods;
import com.store.service.GoodsService;
import com.store.service.impl.GoodsServiceImpl;
import com.store.util.ExportExcel;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

/**
 * @ClassName GoodsPanel
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/8/31 0031-10:37
 * @Version V1.0
 **/
public class GoodsPanel extends JPanel {
    GoodsService service = new GoodsServiceImpl();
    //1.声明要用的
    //上
    JPanel panelTop;
    //上上
    JPanel panelTop1;
    //上下
    JPanel panelTop2;
    //下
    JPanel panelBottom;
    //
    JScrollPane scrollPane;
    JTable table;
    DefaultTableModel dtm;
    JButton btnExcel;

    public GoodsPanel() {
        //上
        panelTop = new JPanel();
        //上上
        panelTop1 = new JPanel();
        //上下
        panelTop2 = new JPanel();
        //下
        panelBottom = new JPanel();
        btnExcel = new JButton("导出Excel");
        //
        scrollPane = new JScrollPane();
        table = new JTable();
        dtm = new DefaultTableModel();
        table.setModel(dtm);//关系
        refresh();//调
        //3.放
        //最大的改成边界布局
        setLayout(new BorderLayout());
        //设置连界布局
        panelTop.setLayout(new BorderLayout());
        panelTop.add(panelTop1, BorderLayout.NORTH);
        panelTop.add(panelTop2, BorderLayout.CENTER);
        panelTop2.add(btnExcel);


        //底部
        scrollPane.setViewportView(table);
        panelBottom.setLayout(new BorderLayout());
        panelBottom.add(scrollPane);
        add(panelTop, BorderLayout.NORTH);//北
        add(panelBottom, BorderLayout.CENTER);
        btnExcel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //导出Excel
                new ExportExcel(table).export();
            }
        });
    }

    //造表头,也可以从数据库查,利用别名
    Vector getCols() {
        Vector cols = new Vector();
        cols.add("编号");
        cols.add("名称");
        cols.add("条形码");
        cols.add("价格");
        cols.add("数量");
        cols.add("位置");
        cols.add("类型");
        return cols;
    }

    //造表身
    Vector<Vector> getRows() {
        List<Goods> lists = service.findAll();

        Vector<Vector> rows = new Vector<>();
        if (lists != null && lists.size() > 0) {
            for (Goods g : lists) {
                Vector v = new Vector();
                //装
                v.add(g.getGoodsId());
                v.add(g.getName());
                v.add(g.getBarcode());
                v.add(g.getPrice());
                v.add(g.getNumber());
                v.add(g.getPname());
                v.add(g.getKname());
                rows.add(v);
            }
        }

        return rows;
    }

    //刷新
    public void refresh() {
        Vector cols = getCols();
        Vector<Vector> rows = getRows();
        dtm.setDataVector(rows, cols);
    }
}

参照以前所学知识:完成以下功能

(1)选择“基础信息管理”/“客户信息管理”按钮,在弹出的窗体中进行对客户信息添加、修改以及删除操作。 (2)选择“基础信息管理”/“商品信息管理”按钮,在弹出的窗体中进行对商品信息添加、修改以及删除操作。 (3)选择“基础信息管理”/“供应商信息管理”按钮,在弹出的窗体中进行对供应商信息添加、修改以及删除操作。 (4)选择“进货管理”/“进货单”按钮,在弹出的窗体中进行添加进货单、商品入库信息操作。 (5)选择“进货管理”/“进货退货”按钮,在弹出的窗体中进行添加退货信息操作。 (6)选择“销售管理”/“销售单”按钮,在弹出的窗体中进行添加商品销售信息操作。 (7)选择“销售管理”/“销售退货”按钮,在弹出的窗体中进行添加商品退货信息操作。 (8)选择“查询统计”/“客户信息查询”按钮,在弹出的窗体中进行对客户信息的查询操作。 (9)选择“查询统计”/“商品信息查询”按钮,在弹出的窗体中进行对商品信息的查询操作。 (10)选择“查询统计”/“供应商信息查询”按钮,在弹出的窗体中进行对供应商信息的查询操作。 (11)选择“查询统计”/“销售信息查询”按钮,在弹出的窗体中进行对销售信息进行查询操作。 (12)选择“查询统计”/“销售退货查询”按钮,在弹出的窗体中进行对销售退货信息的查询操作。 (13)选择“查询统计”/“入库查询”按钮,在弹出的窗体中进行对商品入库信息的查询操作。 (14)选择“查询统计”/“入库退货查询”按钮,在弹出的窗体中进行对入库商品退货的信息查询操作。 (15)选择“查询统计”/“销售排行”按钮,在弹出的窗体中进行对销售信息排行查询操作。 (16)选择“库存管理”/“库存盘点”按钮,在弹出的窗体中进行查看商品的库存的品种、剩余量等信息操作。 (17)选择“库存管理”/“价格调整”按钮,在弹出的窗体中进行对商品价格的调整操作。 (18)选择“系统管理”/“操作员管理”按钮,在弹出的窗体中进行对操作员信息的添加、修改以及删除操作。 (19)选择“系统管理”/“更改密码”按钮,在弹出的窗体中进行对操作员的密码修改操作。 (20)选择“系统管理”/“权限管理”按钮,在弹出的窗体中进行对用户的权限设置操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤永红

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值