diff --git a/Supermarket/src/com/lingnan/supermarket/dao/impl/BufferImpl.java b/Supermarket/src/com/lingnan/supermarket/dao/impl/BufferImpl.java index 35da321..6133106 100644 --- a/Supermarket/src/com/lingnan/supermarket/dao/impl/BufferImpl.java +++ b/Supermarket/src/com/lingnan/supermarket/dao/impl/BufferImpl.java @@ -1,122 +1,122 @@ package com.lingnan.supermarket.dao.impl; +import com.lingnan.supermarket.dao.BufferService; +import com.lingnan.supermarket.dto.Buffer; +import com.lingnan.supermarket.dto.Production; +import com.lingnan.supermarket.utils.JDBCUtil; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; -import com.lingnan.supermarket.dao.BufferService; -import com.lingnan.supermarket.dto.Buffer; -import com.lingnan.supermarket.dto.Production; -import com.lingnan.supermarket.utils.JDBCUtil; - +// 实现BufferService接口的缓冲区操作类 public class BufferImpl implements BufferService { - - - - /*------------------------收银出货模块-----------------------*/ /*返回出货缓冲表所有delmark=1的记录并排序*/ + // 获取所有出货缓冲区的记录 public Vector allOutBuffer(){ - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet=null; - Vector v = new Vector(); - Buffer Buffer; + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + Vector v = new Vector(); // 创建一个向量用于存储Buffer对象 + Buffer Buffer; // 声明一个Buffer对象 try { - pstmt = conn.prepareStatement("select * from OutBuffer "); - resultSet = pstmt.executeQuery(); - while(resultSet.next()) { - Buffer = new Buffer(); - Buffer.setId(resultSet.getString("id")); - Buffer.setName(resultSet.getString("name")); - Buffer.setInPrice(resultSet.getFloat("inPrice")); - Buffer.setOutPrice(resultSet.getFloat("OutPrice")); - Buffer.setLife(resultSet.getInt("life")); - Buffer.setSum(resultSet.getInt("sum")); - Buffer.setSupplyId(resultSet.getInt("supplyId")); - Buffer.setId2(resultSet.getString("id2")); - Buffer.setName2(resultSet.getString("name2")); - Buffer.setPrice(resultSet.getFloat("price")); - Buffer.setDelmark(resultSet.getInt("delmark")); - v.add(Buffer); + pstmt = conn.prepareStatement("select * from OutBuffer "); // 准备SQL查询语句 + resultSet = pstmt.executeQuery(); // 执行查询 + while(resultSet.next()) { // 遍历结果集 + Buffer = new Buffer(); // 创建一个新的Buffer对象 + Buffer.setId(resultSet.getString("id")); // 设置商品ID + Buffer.setName(resultSet.getString("name")); // 设置商品名称 + Buffer.setInPrice(resultSet.getFloat("inPrice")); // 设置进货价格 + Buffer.setOutPrice(resultSet.getFloat("OutPrice")); // 设置出货价格 + Buffer.setLife(resultSet.getInt("life")); // 设置商品保质期 + Buffer.setSum(resultSet.getInt("sum")); // 设置商品数量 + Buffer.setSupplyId(resultSet.getInt("supplyId")); // 设置供应商ID + Buffer.setId2(resultSet.getString("id2")); // 设置商品类别ID + Buffer.setName2(resultSet.getString("name2")); // 设置商品类别名称 + Buffer.setPrice(resultSet.getFloat("price")); // 设置商品总价 + Buffer.setDelmark(resultSet.getInt("delmark")); // 设置删除标记 + v.add(Buffer); // 将Buffer对象添加到向量中 } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return v; + return v; // 返回存储所有Buffer对象的向量 } /*通过id查找返回boolean*/ - public Buffer findOutBufferbyId(String id) + // 根据ID查找出货缓冲区的记录 + public Buffer findOutBufferbyId(String id) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet=null; - Buffer buffer = new Buffer(); - try { - pstmt = conn.prepareStatement("select * from OutBuffer where id=? "); - pstmt.setString(1, id); - resultSet = pstmt.executeQuery(); - - if(resultSet.next()) { - buffer.setId(resultSet.getString("id")); - buffer.setName(resultSet.getString("name")); - buffer.setInPrice(resultSet.getFloat("inPrice")); - buffer.setOutPrice(resultSet.getFloat("OutPrice")); - buffer.setLife(resultSet.getInt("life")); - buffer.setSum(resultSet.getInt("sum")); - buffer.setSupplyId(resultSet.getInt("supplyId")); - buffer.setId2(resultSet.getString("id2")); - buffer.setName2(resultSet.getString("name2")); - buffer.setPrice(resultSet.getFloat("price")); - buffer.setDelmark(resultSet.getInt("delmark")); - flag = true; - }else { - return null; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + Buffer buffer = new Buffer(); // 创建一个新的Buffer对象 + try { + pstmt = conn.prepareStatement("select * from OutBuffer where id=? "); // 准备SQL查询语句 + pstmt.setString(1, id); // 设置查询参数 + resultSet = pstmt.executeQuery(); // 执行查询 + + if(resultSet.next()) { // 如果找到对应记录 + buffer.setId(resultSet.getString("id")); // 设置商品ID + buffer.setName(resultSet.getString("name")); // 设置商品名称 + buffer.setInPrice(resultSet.getFloat("inPrice")); // 设置进货价格 + buffer.setOutPrice(resultSet.getFloat("OutPrice")); // 设置出货价格 + buffer.setLife(resultSet.getInt("life")); // 设置商品保质期 + buffer.setSum(resultSet.getInt("sum")); // 设置商品数量 + buffer.setSupplyId(resultSet.getInt("supplyId")); // 设置供应商ID + buffer.setId2(resultSet.getString("id2")); // 设置商品类别ID + buffer.setName2(resultSet.getString("name2")); // 设置商品类别名称 + buffer.setPrice(resultSet.getFloat("price")); // 设置商品总价 + buffer.setDelmark(resultSet.getInt("delmark")); // 设置删除标记 + flag = true; // 设置标志为true + }else { + return null; // 没有找到该商品,返回null + } + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + return buffer; // 返回找到的Buffer对象 } - return buffer; -} /*如果缓冲区没有那就把整条记录插入插入操作*/ + // 向出货缓冲区添加新商品记录 public boolean addOutBufferNewProd(String id,int sum) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet=null; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("call InsertOutBuffer(?,?) "); - pstmt.setString(1, id); - pstmt.setInt(2, sum); - - - if( pstmt.executeUpdate()==1) { - flag = true; + pstmt = conn.prepareStatement("call InsertOutBuffer(?,?) "); // 准备存储过程调用语句 + pstmt.setString(1, id); // 设置商品ID参数 + pstmt.setInt(2, sum); // 设置商品数量参数 + + + if( pstmt.executeUpdate()==1) { // 执行更新操作 + flag = true; // 如果成功执行插入,设置标志为true } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回插入结果标志 } - - + + /*根据商品id获取sum和outprice public Buffer findOutBufferSumAndOutPrice(String id) { Buffer Buffer = new Buffer(); @@ -127,13 +127,13 @@ public class BufferImpl implements BufferService { pstmt = conn.prepareStatement("select * from OutBuffer where id=? "); pstmt.setString(1, id); resultSet = pstmt.executeQuery(); - + if(resultSet.next()) { Buffer.setSum(resultSet.getInt("sum")); Buffer.setOutPrice(resultSet.getFloat("OutPrice")); } - + } catch (SQLException e) { e.printStackTrace(); }finally { @@ -143,320 +143,351 @@ public class BufferImpl implements BufferService { } */ - + /*如果delmark是1,即是已经添加的商品*/ + // 更新已存在商品的出货缓冲区记录 public boolean addOutBufferExeistProd(String id,int sum,Buffer buffer){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("update OutBuffer set sum=?,price=? where id=? "); - pstmt.setInt(1, sum+buffer.getSum()); - pstmt.setFloat(2,( sum+buffer.getSum()*buffer.getOutPrice())); - pstmt.setString(3, id); - if(pstmt.executeUpdate()==1) { - flag = true; + pstmt = conn.prepareStatement("update OutBuffer set sum=?,price=? where id=? "); // 准备SQL更新语句 + pstmt.setInt(1, sum+buffer.getSum()); // 设置更新后的商品数量 + pstmt.setFloat(2,( sum+buffer.getSum()*buffer.getOutPrice())); // 设置更新后的商品总价 + pstmt.setString(3, id); // 设置商品ID + if(pstmt.executeUpdate()==1) { // 执行更新操作 + flag = true; // 如果成功执行更新,设置标志为true } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回更新结果标志 } /*获得购物车总价*/ + // 计算出货缓冲区所有商品的总价 public Float OutBufferAllPrice(){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("select sum(price) from OutBuffer "); - resultSet=pstmt.executeQuery(); - if(resultSet.next()) { - return resultSet.getFloat("sum(price)"); + pstmt = conn.prepareStatement("select sum(price) from OutBuffer "); // 准备SQL查询语句 + resultSet=pstmt.executeQuery(); // 执行查询 + if(resultSet.next()) { // 如果有结果 + return resultSet.getFloat("sum(price)"); // 返回总价 } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return null; + return null; // 如果没有结果,返回null } - - + + /*结账后对5个表进行操作*/ + // 进行结账操作并更新数据库 public boolean Account(String number,String time,String id,int sum,Float price){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("call Account(?,?,?,?,?)"); - pstmt.setString(1,number); - pstmt.setString(2,time ); - pstmt.setString(3,id ); - pstmt.setInt(4,sum ); - pstmt.setFloat(5, price); - if(pstmt.execute()) - flag = true; + pstmt = conn.prepareStatement("call Account(?,?,?,?,?)"); // 准备存储过程调用语句 + pstmt.setString(1,number); // 设置订单号 + pstmt.setString(2,time ); // 设置时间 + pstmt.setString(3,id ); // 设置商品ID + pstmt.setInt(4,sum ); // 设置商品数量 + pstmt.setFloat(5, price); // 设置商品总价 + if(pstmt.execute()) // 执行更新操作 + flag = true; // 如果成功执行更新,设置标志为true - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回更新结果标志 } - + /*删除所有购物车*/ + // 清空出货缓冲区 public boolean DelAllOutBuffer(){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("delete from OutBuffer"); - if(pstmt.execute()) - flag = true; + pstmt = conn.prepareStatement("delete from OutBuffer"); // 准备SQL删除语句 + if(pstmt.execute()) // 执行删除操作 + flag = true; // 如果成功执行删除,设置标志为true - - } catch (SQLException e) { - e.printStackTrace(); - }finally { + flag = true; // 如果成功执行删除,设置标志为true - JDBCUtil.close(resultSet, pstmt, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回删除结果标志 } /*往订单表插入一条记录*/ + // 向订单表插入一条新记录 public boolean InsertOutOrder(String number,Float allPrice,String time,String username){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("insert into OutOrder values(?,?,?,?,?)"); - pstmt.setString(1, number); - pstmt.setFloat(2, allPrice); - pstmt.setString(3, time); - pstmt.setString(4, username); - pstmt.setInt(5, 1); - if(pstmt.execute()) - flag = true; + pstmt = conn.prepareStatement("insert into OutOrder values(?,?,?,?,?)"); // 准备SQL插入语句 + pstmt.setString(1, number); // 设置订单号 + pstmt.setFloat(2, allPrice); // 设置总价 + pstmt.setString(3, time); // 设置时间 + pstmt.setString(4, username); // 设置用户名 + pstmt.setInt(5, 1); // 设置删除标记 + if(pstmt.execute()) // 执行插入操作 + flag = true; // 如果成功执行插入,设置标志为true - - } catch (SQLException e) { - e.printStackTrace(); - }finally { + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 - JDBCUtil.close(resultSet, pstmt, conn); + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回插入结果标志 } /*删除缓冲区OutBuffer一条记录通过id*/ + // 根据ID删除出货缓冲区的一条记录 public boolean DelOutBufferById(String id) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("delete from OutBuffer where id=?"); - pstmt.setString(1, id); - if(pstmt.executeUpdate()==1) - flag = true; - - } catch (SQLException e) { - e.printStackTrace(); - }finally { + pstmt = conn.prepareStatement("delete from OutBuffer where id=?"); // 准备SQL删除语句 + pstmt.setString(1, id); // 设置商品ID + if(pstmt.executeUpdate()==1) // 执行删除操作 + flag = true; // 如果成功执行删除,设置标志为true - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回删除结果标志 } - + /*更改记录通过id*/ + // 更新出货缓冲区记录 public boolean UpdateOutBufferById(String id,int sum) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("upadte OutBuffer set sum=? where id=?"); - pstmt.setInt(1, sum); - pstmt.setString(2, id); - if(pstmt.execute()) - flag = true; - - } catch (SQLException e) { - e.printStackTrace(); - }finally { + pstmt = conn.prepareStatement("update OutBuffer set sum=? where id=?"); // 准备SQL更新语句 + pstmt.setInt(1, sum); // 设置更新后的商品数量 + pstmt.setString(2, id); // 设置商品ID + if(pstmt.execute()) // 执行更新操作 + flag = true; // 如果成功执行更新,设置标志为true - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回更新结果标志 } - - - - - - - - - - - - + + + + + + + + + /*---------------------------进货模块----------------------------*/ + /*编辑人程文锋2210461197*/ /*返回出货缓冲表所有delmark=1的记录并排序*/ + // 获取所有进货缓冲区的记录 + /** + * 从数据库中获取所有在库的商品信息,并返回一个包含Production对象的向量。 + * + * @return 包含所有Production对象的向量 + */ public Vector allInBuffer(){ - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet=null; - Vector v = new Vector(); - Production production; + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + Vector v = new Vector(); // 创建一个向量用于存储Production对象 + Production production; // 声明一个Production对象 try { - pstmt = conn.prepareStatement("select * from InBuffer "); - resultSet = pstmt.executeQuery(); - while(resultSet.next()) { - production = new Production(); - production.setId(resultSet.getString("id")); - production.setName(resultSet.getString("name")); - production.setInPrice(resultSet.getFloat("inPrice")); - production.setOutPrice(resultSet.getFloat("OutPrice")); - production.setLife(resultSet.getInt("life")); - production.setSum(resultSet.getInt("sum")); - production.setSupplyId(resultSet.getInt("supplyId")); - production.setId2(resultSet.getString("id2")); - production.setName2(resultSet.getString("name2")); - production.setPrice(resultSet.getFloat("price")); - production.setDelmark(resultSet.getInt("delmark")); - v.add(production); + pstmt = conn.prepareStatement("select * from InBuffer "); // 准备SQL查询语句 + resultSet = pstmt.executeQuery(); // 执行查询 + while(resultSet.next()) { // 遍历结果集 + production = new Production(); // 创建一个新的Production对象 + production.setId(resultSet.getString("id")); // 设置商品ID + production.setName(resultSet.getString("name")); // 设置商品名称 + production.setInPrice(resultSet.getFloat("inPrice")); // 设置进货价格 + production.setOutPrice(resultSet.getFloat("OutPrice")); // 设置出货价格 + production.setLife(resultSet.getInt("life")); // 设置商品保质期 + production.setSum(resultSet.getInt("sum")); // 设置商品数量 + production.setSupplyId(resultSet.getInt("supplyId")); // 设置供应商ID + production.setId2(resultSet.getString("id2")); // 设置商品类别ID + production.setName2(resultSet.getString("name2")); // 设置商品类别名称 + production.setPrice(resultSet.getFloat("price")); // 设置商品总价 + production.setDelmark(resultSet.getInt("delmark")); // 设置删除标记 + v.add(production); // 将Production对象添加到向量中 } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return v; + return v; // 返回存储所有Production对象的向量 } - /*通过id查找返回boolean*/ - public Buffer findInBufferbyId(String id) + /** + * 根据商品ID在数据库中查找对应的Buffer对象 + * @param id 商品的ID + * @return 找到的Buffer对象,若未找到则返回null + */ + public Buffer findInBufferbyId(String id) { - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet=null; - Buffer buffer = new Buffer(); - try { - pstmt = conn.prepareStatement("select * from InBuffer where id=? "); - pstmt.setString(1, id); - resultSet = pstmt.executeQuery(); - - if(resultSet.next()) { - buffer.setId(resultSet.getString("id")); - buffer.setName(resultSet.getString("name")); - buffer.setInPrice(resultSet.getFloat("inPrice")); - buffer.setOutPrice(resultSet.getFloat("OutPrice")); - buffer.setLife(resultSet.getInt("life")); - buffer.setSum(resultSet.getInt("sum")); - buffer.setSupplyId(resultSet.getInt("supplyId")); - buffer.setId2(resultSet.getString("id2")); - buffer.setName2(resultSet.getString("name2")); - buffer.setPrice(resultSet.getFloat("price")); - buffer.setDelmark(resultSet.getInt("delmark")); - } - else { - return null; - } + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + Buffer buffer = new Buffer(); // 创建一个新的Buffer对象 + try { + pstmt = conn.prepareStatement("select * from InBuffer where id=? "); // 准备SQL查询语句 + pstmt.setString(1, id); // 设置查询参数 + resultSet = pstmt.executeQuery(); // 执行查询 + + if(resultSet.next()) { // 如果找到对应记录 + buffer.setId(resultSet.getString("id")); // 设置商品ID + buffer.setName(resultSet.getString("name")); // 设置商品名称 + buffer.setInPrice(resultSet.getFloat("inPrice")); // 设置进货价格 + buffer.setOutPrice(resultSet.getFloat("OutPrice")); // 设置出货价格 + buffer.setLife(resultSet.getInt("life")); // 设置商品保质期 + buffer.setSum(resultSet.getInt("sum")); // 设置商品数量 + buffer.setSupplyId(resultSet.getInt("supplyId")); // 设置供应商ID + buffer.setId2(resultSet.getString("id2")); // 设置商品类别ID + buffer.setName2(resultSet.getString("name2")); // 设置商品类别名称 + buffer.setPrice(resultSet.getFloat("price")); // 设置商品总价 + buffer.setDelmark(resultSet.getInt("delmark")); // 设置删除标记 + } + else { + return null; // 没有找到该商品,返回null + } + - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 + } + return buffer; // 返回找到的Buffer对象 } - return buffer; -} + + /*如果缓冲区没有那就把整条记录插入插入操作*/ +// 向进货缓冲区添加新商品记录 + /** + * 添加新商品记录到进货缓冲区 + * + * @param id 商品ID + * @param sum 商品数量 + * @return 返回插入操作是否成功的标志 + */ public boolean addInBufferNewProd(String id,int sum) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet=null; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("call InsertInBuffer(?,?) "); - pstmt.setString(1, id); - pstmt.setInt(2, sum); - - - if( pstmt.executeUpdate()==1) { - flag = true; + pstmt = conn.prepareStatement("call InsertInBuffer(?,?) "); // 准备存储过程调用语句 + pstmt.setString(1, id); // 设置商品ID参数 + pstmt.setInt(2, sum); // 设置商品数量参数 + + + if( pstmt.executeUpdate()==1) { // 执行更新操作 + flag = true; // 如果成功执行插入,设置标志为true } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回插入结果标志 } - + + + + // 向进货缓冲区插入一条记录 + // 此方法负责将一个商品记录插入到进货缓冲区中,并返回插入操作是否成功的标志 public boolean insertInBuffer(Production p) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet=null; - try { - pstmt = conn.prepareStatement("insert into InBuffer values(?,?,?,?,?,?,?,?,?,?,?)"); - pstmt.setString(1, p.getId()); - pstmt.setString(2, p.getName()); - pstmt.setFloat(3, p.getInPrice()); - pstmt.setFloat(4, p.getOutPrice()); - pstmt.setInt(5, p.getLife()); - pstmt.setInt(6, p.getSum()); - pstmt.setInt(7, p.getSupplyId()); - pstmt.setString(8, p.getId2()); - pstmt.setString(9, p.getName2()); - pstmt.setFloat(10,p.getPrice()); - pstmt.setInt(11, 1); - pstmt.executeUpdate(); - - - if( pstmt.execute()) { - flag = true; - } + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + try { + pstmt = conn.prepareStatement("insert into InBuffer values(?,?,?,?,?,?,?,?,?,?,?)"); // 准备SQL插入语句 + pstmt.setString(1, p.getId()); // 设置商品ID + pstmt.setString(2, p.getName()); // 设置商品名称 + pstmt.setFloat(3, p.getInPrice()); // 设置进货价格 + pstmt.setFloat(4, p.getOutPrice()); // 设置出货价格 + pstmt.setInt(5, p.getLife()); // 设置商品保质期 + pstmt.setInt(6, p.getSum()); // 设置商品数量 + pstmt.setInt(7, p.getSupplyId()); // 设置供应商ID + pstmt.setString(8, p.getId2()); // 设置商品类别ID + pstmt.setString(9, p.getName2()); // 设置商品类别名称 + pstmt.setFloat(10,p.getPrice()); // 设置商品总价 + pstmt.setInt(11, 1); // 设置删除标记 + pstmt.executeUpdate(); // 执行插入操作 + + + if( pstmt.execute()) { // 如果成功执行插入,设置标志为true + flag = true; + } + - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 + } + return flag; // 返回插入结果标志 } - return flag; -} - + /* 根据商品id获取sum和outprice public Buffer findInBufferSumAndInPrice(String id) { Buffer Buffer = new Buffer(); @@ -467,13 +498,13 @@ public class BufferImpl implements BufferService { pstmt = conn.prepareStatement("select * from InBuffer where id=? "); pstmt.setString(1, id); resultSet = pstmt.executeQuery(); - + if(resultSet.next()) { Buffer.setSum(resultSet.getInt("sum")); Buffer.setOutPrice(resultSet.getFloat("InPrice")); } - + } catch (SQLException e) { e.printStackTrace(); }finally { @@ -481,165 +512,206 @@ public class BufferImpl implements BufferService { } return Buffer; }*/ - /*如果delmark是1,即是已经添加的商品*/ + // 更新已存在商品的进货缓冲区记录 + // 方法名称:addInBufferExeistProd + // 参数: + // id - 商品的唯一标识符 + // sum - 要添加的商品数量 + // buffer - 包含商品信息的缓冲区对象 + // 返回值:布尔值,表示更新操作是否成功 public boolean addInBufferExeistProd(String id,int sum,Buffer buffer){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("update InBuffer set sum=?,price=? where id=? "); - pstmt.setInt(1, sum+buffer.getSum()); - pstmt.setFloat(2,( sum+buffer.getSum()*buffer.getInPrice())); - pstmt.setString(3, id); - if(pstmt.executeUpdate()==1) { - flag = true; + pstmt = conn.prepareStatement("update InBuffer set sum=?,price=? where id=? "); // 准备SQL更新语句 + pstmt.setInt(1, sum+buffer.getSum()); // 设置更新后的商品数量 + pstmt.setFloat(2,( sum+buffer.getSum()*buffer.getInPrice())); // 设置更新后的商品总价 + pstmt.setString(3, id); // 设置商品ID + if(pstmt.executeUpdate()==1) { // 执行更新操作 + flag = true; // 如果成功执行更新,设置标志为true } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 - JDBCUtil.close(resultSet, pstmt, conn); + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回更新结果标志 } + /*获得购物车总价*/ +// 计算进货缓冲区所有商品的总价 +// 此方法用于计算数据库中进货缓冲区所有商品的总价,并返回相应的浮点数值。 public Float InBufferAllPrice(){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("select sum(price) from InBuffer "); - resultSet=pstmt.executeQuery(); - if(resultSet.next()) { - return resultSet.getFloat("sum(price)"); + pstmt = conn.prepareStatement("select sum(price) from InBuffer "); // 准备SQL查询语句 + resultSet=pstmt.executeQuery(); // 执行查询 + if(resultSet.next()) { // 如果有结果 + return resultSet.getFloat("sum(price)"); // 返回总价 } - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return null; + return null; // 如果没有结果,返回null } - - + + /*结账后对5个表进行操作*/ +// 进行进货时的记录操作 + /** + * 在库存中记录进货信息的函数 + * + * @param number 订单号 + * @param time 进货时间 + * @param id 商品ID + * @param sum 商品数量 + * @param price 商品总价 + * @return 返回操作是否成功的标志 + */ public boolean Stock(String number,String time,String id,int sum,Float price){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("call Stock(?,?,?,?,?)"); - pstmt.setString(1,number); - pstmt.setString(2,time ); - pstmt.setString(3,id ); - pstmt.setInt(4,sum ); - pstmt.setFloat(5, price); - if(pstmt.executeUpdate()==1) - flag = true; - - - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - - JDBCUtil.close(resultSet, pstmt, conn); + pstmt = conn.prepareStatement("call Stock(?,?,?,?,?)"); // 准备存储过程调用语句 + pstmt.setString(1,number); // 设置订单号 + pstmt.setString(2,time ); // 设置时间 + pstmt.setString(3,id ); // 设置商品ID + pstmt.setInt(4,sum ); // 设置商品数量 + pstmt.setFloat(5, price); // 设置商品总价 + if(pstmt.executeUpdate()==1) // 执行更新操作 + flag = true; // 如果成功执行更新,设置标志为true + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回更新结果标志 } - + + /*删除所有购物车*/ + // 清空进货缓冲区 + /** + * 从数据库中删除进货缓冲区中的所有记录 + * + * @return 返回操作是否成功的标志 + */ public boolean DelAllInBuffer(){ - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("delete from InBuffer"); - if(pstmt.execute()) - flag = true; - - - - } catch (SQLException e) { - e.printStackTrace(); - }finally { - - JDBCUtil.close(resultSet, pstmt, conn); + pstmt = conn.prepareStatement("delete from InBuffer"); // 准备SQL删除语句 + if(pstmt.execute()) // 执行删除操作 + flag = true; // 如果成功执行删除,设置标志为true + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回删除结果标志 } - /*删除缓冲区InBuffer一条记录通过id*/ + // 根据ID删除进货缓冲区的一条记录 + /** + * 根据商品ID删除进货缓冲区中的一条记录 + * + * @param id 商品ID + * @return 删除操作是否成功的标志 + */ public boolean DelInBufferById(String id) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null;; + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null;; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("delete from InBuffer where id=?"); - pstmt.setString(1, id); - if(pstmt.executeUpdate()==1) - flag = true; - - } catch (SQLException e) { - e.printStackTrace(); - }finally { + pstmt = conn.prepareStatement("delete from InBuffer where id=?"); // 准备SQL删除语句 + pstmt.setString(1, id); // 设置商品ID + if(pstmt.executeUpdate()==1) // 执行删除操作 + flag = true; // 如果成功执行删除,设置标志为true - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回删除结果标志 } - - - - /*更改记录通过id*/ - public boolean UpdateInBufferById(String id,int sum) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement pstmt = null; - ResultSet resultSet = null; + + + + + + /*更改记录通过id*/ + // 更新进货缓冲区记录 + /** + * 更新进货缓冲区中指定商品ID的数量。 + * + * @param id 商品的ID + * @param sum 更新后的商品数量 + * @return 返回更新是否成功的标志 + */ +public boolean UpdateInBufferById(String id,int sum) { + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet = null; // 初始化结果集对象 try { - pstmt = conn.prepareStatement("upadte InBuffer set sum=? where id=?"); - pstmt.setInt(1, sum); - pstmt.setString(2, id); - if(pstmt.execute()) - flag = true; - - } catch (SQLException e) { - e.printStackTrace(); - }finally { + pstmt = conn.prepareStatement("update InBuffer set sum=? where id=?"); // 准备SQL更新语句 + pstmt.setInt(1, sum); // 设置更新后的商品数量 + pstmt.setString(2, id); // 设置商品ID + if(pstmt.execute()) // 执行更新操作 + flag = true; // 如果成功执行更新,设置标志为true - JDBCUtil.close(resultSet, pstmt, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + }finally { // 无论是否发生异常,都会执行的代码块 + + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - return flag; + return flag; // 返回更新结果标志 } + + /** + * 按照顺序插入订单 + * + * @param number 订单编号 + * @param allPrice 总价格 + * @param time 订单时间 + * @param username 用户名 + * @return 返回插入成功与否 + */ @Override public boolean InsertInOrder(String number, Float allPrice, String time, String username) { // TODO Auto-generated method stub + // 插入订单的操作 return false; } - - - - } - - diff --git a/Supermarket/src/com/lingnan/supermarket/dao/impl/SupplierInfImpl.java b/Supermarket/src/com/lingnan/supermarket/dao/impl/SupplierInfImpl.java index 26b2dab..c35f6b1 100644 --- a/Supermarket/src/com/lingnan/supermarket/dao/impl/SupplierInfImpl.java +++ b/Supermarket/src/com/lingnan/supermarket/dao/impl/SupplierInfImpl.java @@ -1,5 +1,9 @@ package com.lingnan.supermarket.dao.impl; +import com.lingnan.supermarket.dao.SupplierInfService; +import com.lingnan.supermarket.dto.SupplierInf; +import com.lingnan.supermarket.utils.JDBCUtil; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -8,211 +12,255 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.Vector; -import com.lingnan.supermarket.dao.SupplierInfService; -import com.lingnan.supermarket.dto.ProdCatalog; -import com.lingnan.supermarket.dto.SupplierInf; -import com.lingnan.supermarket.dto.User; -import com.lingnan.supermarket.utils.JDBCUtil; +/** + * 供应商信息服务实现类 + * @cwf 2210461197 2024/12/1 + */ + +// 实现供应商信息服务接口的类 public class SupplierInfImpl implements SupplierInfService{ + // 查询所有供应商信息的方法 @Override + // 该方法用于查找所有未被标记为删除的供应商信息,返回一个供应商信息的向量 + // 此方法连接数据库,执行查询,并将结果封装为供应商信息对象的向量 public Vector findAllSupplierInf() { - Vector supplierInfs = new Vector(); - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - ResultSet resultSet=null; - - String SQL = "select * from supplierInf where delmark = 1"; - + Vector supplierInfs = new Vector(); // 创建一个向量用于存储供应商信息 + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + + String SQL = "select * from supplierInf where delmark = 1"; // SQL查询语句,选择未被标记为删除的供应商信息 + try { - preparedStatement = conn.prepareStatement(SQL); - resultSet = preparedStatement.executeQuery(); - while(resultSet.next()) { - SupplierInf supplierInf = new SupplierInf(); - supplierInf.setId(resultSet.getInt("id")); - supplierInf.setName(resultSet.getString("name")); - supplierInf.setContact(resultSet.getString("contact")); - supplierInf.setEmail(resultSet.getString("email")); - supplierInf.setAddress(resultSet.getString("address")); - supplierInf.setDelmark(resultSet.getInt("delmark")); - supplierInfs.add(supplierInf); + preparedStatement = conn.prepareStatement(SQL); // 准备SQL语句 + resultSet = preparedStatement.executeQuery(); // 执行查询 + while(resultSet.next()) { // 遍历结果集 + SupplierInf supplierInf = new SupplierInf(); // 创建一个新的供应商信息对象 + supplierInf.setId(resultSet.getInt("id")); // 设置供应商ID + supplierInf.setName(resultSet.getString("name")); // 设置供应商名称 + supplierInf.setContact(resultSet.getString("contact")); // 设置供应商联系人 + supplierInf.setEmail(resultSet.getString("email")); // 设置供应商邮箱 + supplierInf.setAddress(resultSet.getString("address")); // 设置供应商地址 + supplierInf.setDelmark(resultSet.getInt("delmark")); // 设置删除标记 + supplierInfs.add(supplierInf); // 将供应商信息对象添加到向量中 } - - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(resultSet, preparedStatement, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + // 关闭数据库连接和相关资源 + JDBCUtil.close(resultSet, preparedStatement, conn); // 关闭结果集、预编译语句和数据库连接 } - return supplierInfs; + return supplierInfs; // 返回存储所有供应商信息的向量 } + // 根据供应商名称查询供应商信息的方法 @Override + // 定义一个查找供应商信息的方法,根据传入的供应商对象进行查询 + /** + * 根据供应商名称查找供应商信息 + * @param supplierInf 包含供应商名称的供应商信息对象 + * @return 返回包含查询到的供应商信息的向量 + */ public Vector findByNameSupplierInf(SupplierInf supplierInf) { //SupplierInf supplierInf = new SupplierInf(); - Connection conn = JDBCUtil.getConn(); - Vector v = new Vector<>(); - System.out.println(supplierInf.getName()); - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + Vector v = new Vector<>(); // 创建一个向量用于存储供应商信息 + System.out.println(supplierInf.getName()); // 打印供应商名称 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + ResultSet resultSet = null; // 初始化结果集对象 try { - if(!supplierInf.getName().equals("")){ - String s='%'+supplierInf.getName()+'%'; - preparedStatement = conn.prepareStatement("select * from supplierInf where name like ? and delmark = 1"); - preparedStatement.setString(1, s); + if(!supplierInf.getName().equals("")){ // 如果供应商名称不为空 + String s='%'+supplierInf.getName()+'%'; // 构建模糊查询字符串 + preparedStatement = conn.prepareStatement("select * from supplierInf where name like ? and delmark = 1"); // 准备SQL查询语句 + preparedStatement.setString(1, s); // 设置查询参数 }else - preparedStatement = conn.prepareStatement("select * from supplierInf where delmark = 1"); - resultSet = preparedStatement.executeQuery(); - while (resultSet.next()) { - supplierInf = new SupplierInf(); - supplierInf.setId(resultSet.getInt("id")); - supplierInf.setName(resultSet.getString("name")); - supplierInf.setAddress(resultSet.getString("address")); - supplierInf.setContact(resultSet.getString("contact")); - supplierInf.setEmail(resultSet.getString("email")); - supplierInf.setDelmark(1); - v.add(supplierInf); + preparedStatement = conn.prepareStatement("select * from supplierInf where delmark = 1"); // 准备SQL查询语句,选择未被标记为删除的供应商信息 + + resultSet = preparedStatement.executeQuery(); // 执行查询 + while (resultSet.next()) { // 遍历结果集 + supplierInf = new SupplierInf(); // 创建一个新的供应商信息对象 + supplierInf.setId(resultSet.getInt("id")); // 设置供应商ID + supplierInf.setName(resultSet.getString("name")); // 设置供应商名称 + supplierInf.setAddress(resultSet.getString("address")); // 设置供应商地址 + supplierInf.setContact(resultSet.getString("contact")); // 设置供应商联系人 + supplierInf.setEmail(resultSet.getString("email")); // 设置供应商邮箱 + supplierInf.setDelmark(1); // 设置删除标记 + v.add(supplierInf); // 将供应商信息对象添加到向量中 + } + Iterator it=v.iterator(); // 获取向量的迭代器 + while(it.hasNext()){ // 遍历向量 + supplierInf=it.next(); // 获取下一个供应商信息对象 + System.out.println(supplierInf.getId()+" "+supplierInf.getName()+" "+supplierInf.getAddress()+" "+supplierInf.getContact()+" "+supplierInf.getEmail()+" "+supplierInf.getDelmark()+" "); // 打印供应商信息 } - Iterator it=v.iterator(); - while(it.hasNext()){ - supplierInf=it.next(); - System.out.println(supplierInf.getId()+" "+supplierInf.getName()+" "+supplierInf.getAddress()+" "+supplierInf.getContact()+" "+supplierInf.getEmail()+" "+supplierInf.getDelmark()+" "); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(resultSet, preparedStatement, conn); + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, preparedStatement, conn); // 关闭结果集、预编译语句和数据库连接 } - return v; + return v; // 返回存储供应商信息的向量 } - +// 添加供应商信息的方法 + /** + * 添加供应商信息 + * + * @param supplierInf 供应商信息对象 + * @return 插入结果标志,1表示成功,0表示失败 + */ @Override public int addSupplierInf(SupplierInf supplierInf) { - int flag = 0; - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - + int flag = 0; // 初始化标志为0 + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + try { preparedStatement = conn.prepareStatement - ("insert into supplierInf values (null,?,?,?,?,?)"); + ("insert into supplierInf values (null,?,?,?,?,?)"); // 准备SQL插入语句 //preparedStatement.setInt(1, supplierInf.getId()); - preparedStatement.setString(1, supplierInf.getName()); - preparedStatement.setString(2, supplierInf.getAddress()); - preparedStatement.setString(3, supplierInf.getContact()); - preparedStatement.setString(4, supplierInf.getEmail()); - preparedStatement.setInt(5, 1); - preparedStatement.executeUpdate(); - flag = 1; - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(null,preparedStatement, conn); + preparedStatement.setString(1, supplierInf.getName()); // 设置供应商名称 + preparedStatement.setString(2, supplierInf.getAddress()); // 设置供应商地址 + preparedStatement.setString(3, supplierInf.getContact()); // 设置供应商联系人 + preparedStatement.setString(4, supplierInf.getEmail()); // 设置供应商邮箱 + preparedStatement.setInt(5, 1); // 设置删除标记 + preparedStatement.executeUpdate(); // 执行插入操作 + flag = 1; // 设置标志为1,表示插入成功 + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接 } - - return flag; - } + return flag; // 返回插入结果标志 + } + // 删除供应商信息的方法 + /** + * 删除指定ID的供应商信息 + * + * @param id 要删除的供应商ID + * @return 删除操作的结果标志,1表示成功,0表示失败 + */ @Override public int deleteSupplierInf(int id) { - int flag = 0; - - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - + int flag = 0; // 初始化标志为0 + + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + try { preparedStatement = conn.prepareStatement - ("delete from supplierInf where id = ?"); - preparedStatement.setInt(1, id); - preparedStatement.executeUpdate(); - flag = 1; - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(null,preparedStatement, conn); + ("delete from supplierInf where id = ?"); // 准备SQL删除语句 + preparedStatement.setInt(1, id); // 设置供应商ID + preparedStatement.executeUpdate(); // 执行删除操作 + flag = 1; // 设置标志为1,表示删除成功 + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接 } - return flag; + return flag; // 返回删除结果标志 } + // 更新供应商信息的方法 + /** + * 更新供应商信息 + * + * @param supplierInf 供应商信息对象 + * @return 更新结果标志,1表示成功,0表示失败 + */ @Override public int updateSupplierInf(SupplierInf supplierInf) { - int flag=0; - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; + int flag=0; // 初始化标志为0 + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 try { - preparedStatement = conn.prepareStatement("update supplierInf set name=?,address=?,contact=?,email=?,delmark=? where id = ?"); - preparedStatement.setString(1,supplierInf.getName()); - preparedStatement.setString(2,supplierInf.getAddress()); - preparedStatement.setString(3,supplierInf.getContact()); - preparedStatement.setString(4, supplierInf.getEmail()); - preparedStatement.setInt(5, supplierInf.getDelmark()); - preparedStatement.setInt(6,supplierInf.getId()); - preparedStatement.executeUpdate(); - flag = 1; - - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(null, preparedStatement, conn); + preparedStatement = conn.prepareStatement("update supplierInf set name=?,address=?,contact=?,email=?,delmark=? where id = ?"); // 准备SQL更新语句 + preparedStatement.setString(1,supplierInf.getName()); // 设置供应商名称 + preparedStatement.setString(2,supplierInf.getAddress()); // 设置供应商地址 + preparedStatement.setString(3,supplierInf.getContact()); // 设置供应商联系人 + preparedStatement.setString(4, supplierInf.getEmail()); // 设置供应商邮箱 + preparedStatement.setInt(5, supplierInf.getDelmark()); // 设置删除标记 + preparedStatement.setInt(6,supplierInf.getId()); // 设置供应商ID + preparedStatement.executeUpdate(); // 执行更新操作 + flag = 1; // 设置标志为1,表示更新成功 + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(null, preparedStatement, conn); // 关闭预编译语句和数据库连接 } - return flag; + return flag; // 返回更新结果标志 } - + + + // 获取所有供应商名称的方法 + // 该方法从数据库中查询所有供应商的名称,并将其存储在一个列表中返回 @Override public ArrayList findNameSupplier() { - Connection conn=JDBCUtil.getConn(); - ArrayList v = new ArrayList(); - SupplierInf supplierInf; - PreparedStatement pstmt = null; - ResultSet resultSet=null; - v.add("全部"); + Connection conn=JDBCUtil.getConn(); // 获取数据库连接 + ArrayList v = new ArrayList(); // 创建一个列表用于存储供应商名称 + SupplierInf supplierInf; // 声明一个供应商信息对象 + PreparedStatement pstmt = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + v.add("全部"); // 添加默认选项“全部” try { - pstmt = conn.prepareStatement("select * from supplierInf"); - resultSet = pstmt.executeQuery(); - while(resultSet.next()){ - - v.add(resultSet.getString("name")); + pstmt = conn.prepareStatement("select * from supplierInf"); // 准备SQL查询语句 + resultSet = pstmt.executeQuery(); // 执行查询 + while(resultSet.next()){ // 遍历结果集 + + v.add(resultSet.getString("name")); // 将供应商名称添加到列表中 } - } catch (SQLException e) { + } catch (SQLException e) { // 捕获SQL异常 // TODO Auto-generated catch block - e.printStackTrace(); - }finally{ - JDBCUtil.close(resultSet, pstmt, conn); + e.printStackTrace(); // 打印异常堆栈信息 + }finally{ // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接 } - - - return v; + + + return v; // 返回存储供应商名称的列表 } + // 根据供应商名称查找其ID的方法 + /** + * 根据给定的供应商名称查找对应的供应商ID。 + * 如果名称为“全部”,返回默认ID(0)。 + * 如果未找到对应的供应商,返回-1作为标志。 + * + * @param name 供应商名称 + * @return 供应商ID或标志 + */ @Override public int findIdSupplierByName(String name) { - int flag = -1; - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - ResultSet resultSet= null; - int id=0; + int flag = -1; // 初始化标志为-1 + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + ResultSet resultSet= null; // 初始化结果集对象 + int id=0; // 初始化供应商ID为0 try { - if(name.equals("全部")) - return id; + if(name.equals("全部")) // 如果供应商名称为“全部” + return id; // 返回默认ID preparedStatement = conn.prepareStatement - ("select * from supplierInf where name=?"); - preparedStatement.setString(1, name); - resultSet=preparedStatement.executeQuery(); - if(resultSet.next()){ - return resultSet.getInt("id"); + ("select * from supplierInf where name=?"); // 准备SQL查询语句 + preparedStatement.setString(1, name); // 设置查询参数 + resultSet=preparedStatement.executeQuery(); // 执行查询 + if(resultSet.next()){ // 如果找到对应记录 + return resultSet.getInt("id"); // 返回供应商ID } - - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(null,preparedStatement, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接 } - - return flag; + + return flag; // 返回查询结果标志 } - -} + +} \ No newline at end of file diff --git a/Supermarket/src/com/lingnan/supermarket/dao/impl/inRecordServiceImpl.java b/Supermarket/src/com/lingnan/supermarket/dao/impl/inRecordServiceImpl.java index bf5f585..9964df6 100644 --- a/Supermarket/src/com/lingnan/supermarket/dao/impl/inRecordServiceImpl.java +++ b/Supermarket/src/com/lingnan/supermarket/dao/impl/inRecordServiceImpl.java @@ -1,152 +1,174 @@ package com.lingnan.supermarket.dao.impl; +import com.lingnan.supermarket.dao.inRecordService; +import com.lingnan.supermarket.dto.InRecord; +import com.lingnan.supermarket.dto.Production; +import com.lingnan.supermarket.utils.JDBCUtil; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; import java.util.Vector; - -import com.lingnan.supermarket.dao.inRecordService; -import com.lingnan.supermarket.dto.InOrder; -import com.lingnan.supermarket.dto.InRecord; -import com.lingnan.supermarket.dto.Production; -import com.lingnan.supermarket.dto.User; -import com.lingnan.supermarket.utils.JDBCUtil; +/** + * 进货记录服务实现类 + * @cwf2210461197 2024/12/2 + * @version 1.0 + */ public class inRecordServiceImpl implements inRecordService{ - + /** + * 查询所有进货记录的方法 + * @return 包含所有进货记录的向量 + */ @Override public Vector findAllinRecord() { - Vector inRecords = new Vector(); - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - ResultSet resultSet=null; - - String SQL = "select * from inRecord"; - + Vector inRecords = new Vector(); // 创建一个向量用于存储进货记录 + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + ResultSet resultSet=null; // 初始化结果集对象 + + String SQL = "select * from inRecord"; // SQL查询语句,选择所有进货记录 + try { - preparedStatement = conn.prepareStatement(SQL); - resultSet = preparedStatement.executeQuery(); - while(resultSet.next()) { - InRecord inRecord = new InRecord(); - inRecord.setiNumber(resultSet.getString("iNumber")); - inRecord.setSum(resultSet.getInt("sum")); - inRecord.setInPrice(resultSet.getFloat("inPrice")); - inRecords.add(inRecord); + preparedStatement = conn.prepareStatement(SQL); // 准备SQL语句 + resultSet = preparedStatement.executeQuery(); // 执行查询 + while(resultSet.next()) { // 遍历结果集 + InRecord inRecord = new InRecord(); // 创建一个新的进货记录对象 + inRecord.setiNumber(resultSet.getString("iNumber")); // 设置进货单号 + inRecord.setSum(resultSet.getInt("sum")); // 设置进货数量 + inRecord.setInPrice(resultSet.getFloat("inPrice")); // 设置进货价格 + inRecords.add(inRecord); // 将进货记录对象添加到向量中 } - - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(resultSet, preparedStatement, conn); + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, preparedStatement, conn); // 关闭结果集、预编译语句和数据库连接 } - return inRecords; + return inRecords; // 返回存储所有进货记录的向量 } - @Override + + /** + * 根据进货单号查询进货记录 + * + * @param iNumber 进货单号 + * @return 包含符合条件的进货记录的向量 + */ +@Override public Vector findByIdinRecord(String iNumber) { - InRecord inRecord; - Vector v = new Vector(); - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; + InRecord inRecord; // 声明一个进货记录对象 + Vector v = new Vector(); // 创建一个向量用于存储进货记录 + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + ResultSet resultSet = null; // 初始化结果集对象 try { - preparedStatement = conn.prepareStatement("select * from inRecord where iNumber = ?"); - preparedStatement.setString(1, iNumber); - resultSet = preparedStatement.executeQuery(); - while(resultSet.next()) { - inRecord = new InRecord(); - inRecord.setiNumber(resultSet.getString("iNumber")); - inRecord.setId(resultSet.getString("id")); - inRecord.setSum(resultSet.getInt("sum")); - inRecord.setInPrice(resultSet.getFloat("Price")); - v.add(inRecord); - } - - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(resultSet, preparedStatement, conn); + preparedStatement = conn.prepareStatement("select * from inRecord where iNumber = ?"); // 准备SQL查询语句 + preparedStatement.setString(1, iNumber); // 设置查询参数 + resultSet = preparedStatement.executeQuery(); // 执行查询 + while(resultSet.next()) { // 遍历结果集 + inRecord = new InRecord(); // 创建一个新的进货记录对象 + inRecord.setiNumber(resultSet.getString("iNumber")); // 设置进货单号 + inRecord.setId(resultSet.getString("id")); // 设置商品ID + inRecord.setSum(resultSet.getInt("sum")); // 设置进货数量 + inRecord.setInPrice(resultSet.getFloat("Price")); // 设置进货价格 + v.add(inRecord); // 将进货记录对象添加到向量中 + } + + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(resultSet, preparedStatement, conn); // 关闭结果集、预编译语句和数据库连接 } - return v; + return v; // 返回存储进货记录的向量 } + @Override - public int addinRecord(InRecord ir) { - int flag = 0; - - String iNumber = ir.getiNumber(); - int sum = ir.getSum(); - Float inPrice = ir.getInPrice(); - - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - + // 该方法用于将进货记录添加到数据库中。 +// 参数 ir 是一个包含进货信息的对象。 +// 返回值 flag 表示插入的结果,1表示成功,-1表示失败。 +public int addinRecord(InRecord ir) { + int flag = 0; // 初始化标志为0 + + String iNumber = ir.getiNumber(); // 获取进货单号 + int sum = ir.getSum(); // 获取进货数量 + Float inPrice = ir.getInPrice(); // 获取进货价格 + + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + try { preparedStatement = conn.prepareStatement - ("insert into inRecord values (?,?,?)"); - preparedStatement.setString(1, iNumber); - preparedStatement.setInt(2, sum); - preparedStatement.setFloat(3, inPrice); - preparedStatement.executeUpdate(); - flag = 1; - } catch (SQLException e) { - flag = -1; - e.printStackTrace(); - } finally { - JDBCUtil.close(null,preparedStatement, conn); + ("insert into inRecord values (?,?,?)"); // 准备SQL插入语句 + preparedStatement.setString(1, iNumber); // 设置进货单号 + preparedStatement.setInt(2, sum); // 设置进货数量 + preparedStatement.setFloat(3, inPrice); // 设置进货价格 + preparedStatement.executeUpdate(); // 执行插入操作 + flag = 1; // 设置标志为1,表示插入成功 + } catch (SQLException e) { // 捕获SQL异常 + flag = -1; // 设置标志为-1,表示插入失败 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接 } - - return flag; + + return flag; // 返回插入结果标志 } - @Override + + // 删除进货记录方法,根据进货单号进行删除操作 +@Override public int deleteinRecord(String iNumber) { - int flag = 0; - - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - + int flag = 0; // 初始化标志为0 + + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + try { preparedStatement = conn.prepareStatement - ("delete from inRecord where iNumber = ?"); - preparedStatement.setString(1, iNumber); - preparedStatement.executeUpdate(); - flag = 1; - } catch (SQLException e) { - flag = -1; - e.printStackTrace(); - } finally { - JDBCUtil.close(null,preparedStatement, conn); + ("delete from inRecord where iNumber = ?"); // 准备SQL删除语句 + preparedStatement.setString(1, iNumber); // 设置进货单号 + preparedStatement.executeUpdate(); // 执行删除操作 + flag = 1; // 设置标志为1,表示删除成功 + } catch (SQLException e) { // 捕获SQL异常 + flag = -1; // 设置标志为-1,表示删除失败 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接 } - return flag; + return flag; // 返回删除结果标志 } - public boolean insertInRecord(String iNumber,Production p) { - boolean flag = false; - Connection conn = JDBCUtil.getConn(); - PreparedStatement preparedStatement = null; - + /** + * 将进货记录插入到数据库中 + * + * @param iNumber 进货单号 + * @param p 进货的商品对象 + * @return 成功插入返回true,失败返回false + */ +public boolean insertInRecord(String iNumber,Production p) { + boolean flag = false; // 初始化标志为false + Connection conn = JDBCUtil.getConn(); // 获取数据库连接 + PreparedStatement preparedStatement = null; // 初始化预编译语句对象 + try { - preparedStatement = conn.prepareStatement("insert into inRecord values(?,?,?,?)"); - preparedStatement.setString(1, iNumber); - preparedStatement.setString(2, p.getId()); - preparedStatement.setInt(3,p.getSum()); - preparedStatement.setFloat(4, p.getPrice()); - - if(preparedStatement.executeUpdate()==1) - flag = true; - } catch (SQLException e) { - e.printStackTrace(); - } finally { - JDBCUtil.close(null,preparedStatement, conn); + preparedStatement = conn.prepareStatement("insert into inRecord values(?,?,?,?)"); // 准备SQL插入语句 + preparedStatement.setString(1, iNumber); // 设置进货单号 + preparedStatement.setString(2, p.getId()); // 设置商品ID + preparedStatement.setInt(3,p.getSum()); // 设置进货数量 + preparedStatement.setFloat(4, p.getPrice()); // 设置进货价格 + + if(preparedStatement.executeUpdate()==1) // 执行插入操作 + flag = true; // 如果成功执行插入,设置标志为true + } catch (SQLException e) { // 捕获SQL异常 + e.printStackTrace(); // 打印异常堆栈信息 + } finally { // 无论是否发生异常,都会执行的代码块 + JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接 } - return flag; + return flag; // 返回插入结果标志 } - - - -} +} \ No newline at end of file diff --git a/Supermarket/src/com/lingnan/supermarket/dao/inOrderService.java b/Supermarket/src/com/lingnan/supermarket/dao/inOrderService.java index 293aa66..6ca4286 100644 --- a/Supermarket/src/com/lingnan/supermarket/dao/inOrderService.java +++ b/Supermarket/src/com/lingnan/supermarket/dao/inOrderService.java @@ -1,18 +1,19 @@ package com.lingnan.supermarket.dao; -import java.util.Vector; - import com.lingnan.supermarket.dto.InOrder; +import java.util.Vector; + +// 进货订单服务接口,定义了与进货订单相关的操作 public interface inOrderService { //查询全部进货订单 Vector findAllInOrder (); //查询某个进货订单 InOrder findByIdinOrder (String iNumber); - + //默认princial为a1. 创建时new一个时间插入,总价随机模拟 + // 添加新的进货订单 int addInOrder(String iNumber,float allInPrice ); + // 删除指定的进货订单 int deleteInOrder(String iNumber); - - } diff --git a/Supermarket/src/com/lingnan/supermarket/dto/InRecord.java b/Supermarket/src/com/lingnan/supermarket/dto/InRecord.java index 5bc40f7..24a7c77 100644 --- a/Supermarket/src/com/lingnan/supermarket/dto/InRecord.java +++ b/Supermarket/src/com/lingnan/supermarket/dto/InRecord.java @@ -2,26 +2,40 @@ package com.lingnan.supermarket.dto; import com.lingnan.supermarket.dto.base.BsDomain; -public class InRecord extends BsDomain{ - private String iNumber; - private int sum; - private Float inPrice; +// 表示入库记录的数据传输对象类,继承自BsDomain +public class InRecord extends BsDomain { + private String iNumber; // 入库编号 + private int sum; // 入库总数 + private Float inPrice; // 入库单价 + + // 获取入库编号 public String getiNumber() { return iNumber; } + + // 设置入库编号 public void setiNumber(String iNumber) { this.iNumber = iNumber; } + + // 获取入库总数 public int getSum() { return sum; } + + // 设置入库总数 public void setSum(int sum) { this.sum = sum; } + + // 获取入库单价 public Float getInPrice() { return inPrice; } + + // 设置入库单价 public void setInPrice(Float inPrice) { this.inPrice = inPrice; } } + diff --git a/Supermarket/src/com/lingnan/supermarket/dto/SupplierInf.java b/Supermarket/src/com/lingnan/supermarket/dto/SupplierInf.java index 96c027a..a5dbb2c 100644 --- a/Supermarket/src/com/lingnan/supermarket/dto/SupplierInf.java +++ b/Supermarket/src/com/lingnan/supermarket/dto/SupplierInf.java @@ -2,22 +2,20 @@ package com.lingnan.supermarket.dto; import com.lingnan.supermarket.dto.base.BaseDomain; - +// 供应商信息类,继承自BaseDomain public class SupplierInf extends BaseDomain{ - private String name; - private String address; - private String contact; - private String email; + private String name; // 供应商名称 + private String address; // 供应商地址 + private String contact; // 供应商联系人 + private String email; // 供应商电子邮件 public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } - private int delmark; - - - + private int delmark; // 删除标记 + public int getDelmark() { return delmark; } @@ -42,4 +40,4 @@ public class SupplierInf extends BaseDomain{ public void setContact(String contact) { this.contact = contact; } - } +} diff --git a/Supermarket/src/com/lingnan/supermarket/table/InOrderTM.java b/Supermarket/src/com/lingnan/supermarket/table/InOrderTM.java index 60934ee..e662052 100644 --- a/Supermarket/src/com/lingnan/supermarket/table/InOrderTM.java +++ b/Supermarket/src/com/lingnan/supermarket/table/InOrderTM.java @@ -1,69 +1,70 @@ package com.lingnan.supermarket.table; -import java.util.List; -import java.util.Vector; - -import javax.swing.JFrame; -import javax.swing.table.AbstractTableModel; - -import com.lingnan.supermarket.dto.Buffer; +import com.lingnan.supermarket.dao.impl.inOrderServiceImpl; +import com.lingnan.supermarket.dao.impl.productionImpl; import com.lingnan.supermarket.dto.InOrder; -import com.lingnan.supermarket.dto.Buffer; -import com.lingnan.supermarket.dto.Production; -import com.lingnan.supermarket.dto.User; -import com.lingnan.supermarket.dao.UserService; -import com.lingnan.supermarket.dao.impl.*; -import com.lingnan.supermarket.dialog.InDialog; +import javax.swing.table.AbstractTableModel; +import java.util.Vector; +// 定义InOrderTM类,继承自AbstractTableModel public class InOrderTM extends AbstractTableModel{ - + + // 定义列名数组 private String [] columnName = {"订单号","总价","时间","负责人","状态"}; + // 实例化生产实现类 private productionImpl prodDao = new productionImpl(); - + + // 用于存储InOrder对象的向量 private Vector InOrders; + // 实例化入库订单服务实现类 private inOrderServiceImpl inOrderImpl= new inOrderServiceImpl(); + // 用于存储当前操作的InOrder对象 private InOrder inOrder ; - + + // 用于存储当前操作的订单号 String iNumber ;/*订单号*/ - - + + // 获取所有入库订单记录 public void allInOrderRecord() { - //将添加的商品加入到静态变量Vector数组中 + // 将添加的商品加入到静态变量Vector数组中 /*prod = InDialog.getProduction();*/ InOrders = inOrderImpl.findAllInOrder(); } - - //查找分类结果 + + // 根据分类查找结果 public void resultOfFind(int catalog) { if(catalog==0) InOrders = inOrderImpl.findAllInOrder(); else InOrders = inOrderImpl.FindStatus(catalog); } - - //根据订单查找 + + // 根据订单号查找 public void resultOfNumber(String Number) { InOrders=new Vector(); inOrder = inOrderImpl.findByIdinOrder(Number); InOrders.add(inOrder); } + // 返回表格行数 @Override public int getRowCount() { return InOrders.size(); } - -/* public Float getAllPrice() { - return BufferImpl.InBufferAllPrice(); - } -*/ + + /* public Float getAllPrice() { + return BufferImpl.InBufferAllPrice(); + } + */ + // 返回列数 @Override - public int getColumnCount() { + public int getColumnCount() { return columnName.length; } + // 获取指定单元格的值 @Override public Object getValueAt(int rowIndex, int columnIndex) { inOrder = InOrders.get(rowIndex); @@ -76,7 +77,7 @@ public class InOrderTM extends AbstractTableModel{ }else if(columnIndex==1) { return inOrder.getAllInPrice(); }else if(columnIndex==2) { - return inOrder.getInDate(); + return inOrder.getInDate(); }else if(columnIndex==3) { return inOrder.getPrincipal(); }else if(columnIndex==4) { @@ -92,16 +93,17 @@ public class InOrderTM extends AbstractTableModel{ return null; } } - + + // 返回要修改或删除的记录的订单号 public String getINumber() { /*返回要修改或删除的记录*/ return iNumber; } - - + + // 返回指定列的列名 @Override public String getColumnName(int column) { return columnName[column]; } - - -} + + +} \ No newline at end of file diff --git a/Supermarket/src/com/lingnan/supermarket/table/InRecordTM.java b/Supermarket/src/com/lingnan/supermarket/table/InRecordTM.java index 8bb0ad9..ee3dbef 100644 --- a/Supermarket/src/com/lingnan/supermarket/table/InRecordTM.java +++ b/Supermarket/src/com/lingnan/supermarket/table/InRecordTM.java @@ -1,62 +1,60 @@ package com.lingnan.supermarket.table; -import java.util.List; -import java.util.Vector; - -import javax.swing.JFrame; -import javax.swing.table.AbstractTableModel; - -import com.lingnan.supermarket.dto.Buffer; -import com.lingnan.supermarket.dto.InOrder; +import com.lingnan.supermarket.dao.impl.inRecordServiceImpl; +import com.lingnan.supermarket.dao.impl.productionImpl; import com.lingnan.supermarket.dto.InRecord; -import com.lingnan.supermarket.dto.Buffer; -import com.lingnan.supermarket.dto.Production; -import com.lingnan.supermarket.dto.User; -import com.lingnan.supermarket.dao.UserService; -import com.lingnan.supermarket.dao.impl.*; -import com.lingnan.supermarket.dialog.InDialog; +import javax.swing.table.AbstractTableModel; +import java.util.Vector; +// 定义InRecordTM类,继承自AbstractTableModel public class InRecordTM extends AbstractTableModel{ - + + // 定义列名数组 private String [] columnName = {"订单号","id","数量","金额"}; + // 实例化生产实现类 private productionImpl prodDao = new productionImpl(); - + + // 用于存储InRecord对象的向量 private Vector InRecords; - + // 实例化入库记录服务实现类 private inRecordServiceImpl inRecordImpl = new inRecordServiceImpl(); + // 用于存储当前操作的InRecord对象 private InRecord inRecord= new InRecord(); - + // 用于存储当前操作的订单号 private String iNumber ;/*订单号*/ - - + + // 构造方法,接收一个订单号作为参数 public InRecordTM(String iNumber) { this.iNumber=iNumber; } - + + // 根据订单号查找入库记录 public void findInRecordByINumber() { - //将添加的商品加入到静态变量Vector数组中 + // 将添加的商品加入到静态变量Vector数组中 /*prod = InDialog.getProduction();*/ InRecords = inRecordImpl.findByIdinRecord(iNumber); } - - + + // 返回表格行数 @Override public int getRowCount() { return InRecords.size(); } - -/* public Float getAllPrice() { - return BufferImpl.InBufferAllPrice(); - } -*/ + + /* public Float getAllPrice() { + return BufferImpl.InBufferAllPrice(); + } + */ + // 返回列数 @Override - public int getColumnCount() { + public int getColumnCount() { return columnName.length; } + // 获取指定单元格的值 @Override public Object getValueAt(int rowIndex, int columnIndex) { inRecord = InRecords.get(rowIndex); @@ -69,23 +67,24 @@ public class InRecordTM extends AbstractTableModel{ }else if(columnIndex==1) { return inRecord.getId(); }else if(columnIndex==2) { - return inRecord.getSum(); + return inRecord.getSum(); }else if(columnIndex==3) { return inRecord.getInPrice(); }else { return null; } } - + + // 返回要修改或删除的记录的订单号 public String getINumber() { /*返回要修改或删除的记录*/ return iNumber; } - - + + // 返回指定列的列名 @Override public String getColumnName(int column) { return columnName[column]; } - - -} + + +} \ No newline at end of file diff --git a/Supermarket/src/com/lingnan/supermarket/table/InTableModel.java b/Supermarket/src/com/lingnan/supermarket/table/InTableModel.java index b8f28ad..8deb7ee 100644 --- a/Supermarket/src/com/lingnan/supermarket/table/InTableModel.java +++ b/Supermarket/src/com/lingnan/supermarket/table/InTableModel.java @@ -1,99 +1,87 @@ -package com.lingnan.supermarket.table; +package com.lingnan.supermarket.table; // 定义包名 -import java.util.List; -import java.util.Vector; +import com.lingnan.supermarket.dao.impl.productionImpl; +import com.lingnan.supermarket.dto.Production; -import javax.swing.JFrame; import javax.swing.table.AbstractTableModel; +import java.util.Vector; +// 定义InTableModel类,继承自AbstractTableModel +public class InTableModel extends AbstractTableModel { -import com.lingnan.supermarket.dto.InOrder; - -import com.lingnan.supermarket.dto.Production; -import com.lingnan.supermarket.dto.User; -import com.lingnan.supermarket.dao.UserService; -import com.lingnan.supermarket.dao.impl.*; -import com.lingnan.supermarket.dialog.InDialog; - + // 列名数组 + private String[] columnName = {"id", "名称", "数量", "单价", "价格", "保质期", "类别", "供应商id"}; + // 声明生产实现类的实例 + private productionImpl prodDao = new productionImpl(); -public class InTableModel extends AbstractTableModel{ - - - private String [] columnName = {"id","名称","数量","单价","价格","保质期","类别","供应商id"}; + // 用于存储Production对象的向量 + private Vector v; - private productionImpl prodDao = new productionImpl(); - - private Vector v; + // 用于存储选中记录的id + String id; - - String id ; - - + // 构造方法,接收一个Production的向量作为参数 public InTableModel(Vector v) { - System.out.println("调用imtablemodel里面的构造函数"); - this.v=v; + System.out.println("调用InTableModel里面的构造函数"); + this.v = v; // 将传入的向量赋值给实例变量 } - - - + + // 返回表格行数 public int getRowCount() { - return v.size(); + return v.size(); // 向量的大小即为行数 } - + + // 计算并返回所有商品的总价格 public Float getAllPrice() { - Float allPrice=(float) 0; - for(Production p:v) { - allPrice+=p.getPrice(); + Float allPrice = (float) 0; // 初始化总价格为0 + for (Production p : v) { // 遍历每个Production对象 + allPrice += p.getPrice(); // 累加单个产品的价格 } - return allPrice; + return allPrice; // 返回总价格 } + // 返回列数 @Override - public int getColumnCount() { - return columnName.length; + public int getColumnCount() { + return columnName.length; // 列数为列名数组的长度 } + // 获取指定单元格的值 @Override public Object getValueAt(int rowIndex, int columnIndex) { - Production p = v.get(rowIndex); -/* System.out.println( "id="+users.get(rowIndex).getId()); - System.out.println("rowIndex"+rowIndex); - System.out.println("columnIndex"+columnIndex);*/ - id=p.getId(); - if(columnIndex==0) { - return p.getId(); - }else if(columnIndex==1) { - return p.getName(); - }else if(columnIndex==2) { - return p.getSum(); - }else if(columnIndex==3) { - return p.getInPrice() ; - }else if(columnIndex==4) { - return p.getPrice() ; - }else if(columnIndex==5) { - return p.getLife(); - }else if(columnIndex==6) { - return p.getName2()+p.getId2(); - }else if(columnIndex==7) { - return p.getSupplyId(); - }else { - return null; + Production p = v.get(rowIndex); // 获取行索引对应的Production对象 + id = p.getId(); // 获取产品ID + // 根据列索引返回相应的属性值 + if (columnIndex == 0) { + return p.getId(); // 返回ID + } else if (columnIndex == 1) { + return p.getName(); // 返回名称 + } else if (columnIndex == 2) { + return p.getSum(); // 返回数量 + } else if (columnIndex == 3) { + return p.getInPrice(); // 返回单价 + } else if (columnIndex == 4) { + return p.getPrice(); // 返回价格 + } else if (columnIndex == 5) { + return p.getLife(); // 返回保质期 + } else if (columnIndex == 6) { + return p.getName2() + p.getId2(); // 返回类别 + } else if (columnIndex == 7) { + return p.getSupplyId(); // 返回供应商ID + } else { + return null; // 其他情况返回null } } - - public String getId() { /*返回要修改或删除的记录*/ - return id; + + // 返回要修改或删除的记录的ID + public String getId() { + return id; // 返回当前选中记录的ID } - - + + // 返回指定列的列名 @Override public String getColumnName(int column) { - return columnName[column]; + return columnName[column]; // 返回列名 } - - - - - -} +} \ No newline at end of file