Java超市收银系统(七、商品修改和删除)

引言

        当选择1时,显示 “输入商品编码:”,输入商品表中条码,若条码存在则删除商品表中的数据信息;若条码不存在,则显示 “你输入的编码不存在,请重新输入”。当选择2时,显示 “输入商品编码:”,输入商品表中条码,若条码不存在,则显示 “你输入的编码不存在,请重新输入”。若条码存在,显示 “输入商品编码:”,然后开始修改“商品名称、商品单价、商品供应商”,若不修改则直接回车即可。当选择3时,退出系统。

功能实现

        Driver 类是一个简单的控制台应用程序,用于管理超市的商品。它提供了删除和修改商品的功能,使用 ProductDAO 进行数据库操作,并通过 Product 类表示商品数据。而在最开始我们已经简单介绍了dao、vo、util的主要功能(Java超市收银系统(一、用户登录)_如何制作一个简单的收银系统-CSDN博客),所以这里主要介绍ui界面包的代码实现。

        这里提一嘴,dao作为功能函数包,主要是用来实现对数据库的操作,而数据库的基本操作就是增加、删除、修改、查询(增删改查),且Java实现主要过程如下:

1. 加载MySQL驱动

2. 建立与MySQL服务器的连接(前两条已经在util包中DBUtil类实现,直接调用该类中的连接函数即可)

3. 创建语句对象

4. 执行语句

5. 处理结果

6. 关闭连接

        main 方法运行一个无限循环,显示一个菜单供用户选择操作。 用户读取一个整数值来选择操作。程序根据用户选择调用相应的方法(deleteProduct 或 updateProduct)。如果用户选择退出,则调用 System.exit(0) 退出应用程序。

        根据商品编码删除商品。首先提示用户输入商品编码,使用 ProductDAO.queryByBarcode 查询数据库中的商品,检查编码是否与数据库中获取的商品的编码匹配。 如果匹配,使用ProductDAO.delete 删除商品,并确认删除成功。 如果不匹配,提示用户编码不存在。

        根据商品编码修改商品的详细信息。提示用户输入商品编码,使用 ProductDAO.queryByBarcode 查询数据库中的商品,检查商品是否存在,并且编码是否匹配。如果商品存在,提示用户更新商品的名称、单价和供应商。只有在提供了新的值时才会更新,调用 ProductDAO.update 保存修改,并确认修改成功。 如果商品不存在,提示用户编码不存在。

结果展示

完全代码

dao—ProductDAO

package dao;

import util.DBUtil;
import vo.Product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ProductDAO {

    public static Product queryByBarcode(String barcode) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        Product product = null;
        try {
            con = DBUtil.getConnection();
            String sql = "SELECT * FROM t_shangping WHERE tiaoma = ?";
            pst = con.prepareStatement(sql);
            pst.setString(1, barcode);
            rs = pst.executeQuery();
            if (rs.next()) {
                product = new Product();
                product.setBarCode(rs.getString("tiaoma"));
                product.setProductName(rs.getString("mingcheng"));
                product.setPrice(rs.getFloat("danjia"));
                product.setSupply(rs.getString("gongyingshang"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(con, pst);
        }
        return product;
    }
    public static boolean update(Product product) {
        Connection con = null;
        PreparedStatement pst = null;
        boolean success = false;
        try {
            con = DBUtil.getConnection();
            String sql = "UPDATE t_shangping SET mingcheng = ?, danjia = ?, gongyingshang = ? WHERE tiaoma = ?";
            pst = con.prepareStatement(sql);
            pst.setString(1, product.getProductName());
            pst.setFloat(2, product.getPrice());
            pst.setString(3, product.getSupply());
            pst.setString(4, product.getBarCode());
            int rowsAffected = pst.executeUpdate();
            if (rowsAffected > 0) {
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(con, pst);
        }
        return success;
    }

    //delete 删除商品信息
    public static boolean delete(Product product) {
        Connection con = null;
        PreparedStatement pst = null;
        boolean success = false;
        try {
            con = DBUtil.getConnection();
            String sql = "DELETE FROM t_shangping WHERE tiaoma = ?";
            pst = con.prepareStatement(sql);
            pst.setString(1, product.getBarCode());
            int rowsAffected = pst.executeUpdate();
            if (rowsAffected > 0) {
                success = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(con, pst);
        }
        return success;
    }

}

ui—Driver

package ui;

import dao.ProductDAO;
import vo.Product;

import java.util.Scanner;

public class Driver {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("===****超市商品管理维护====");
            System.out.println("1、删除商品");
            System.out.println("2、修改商品");
            System.out.println("3、退出");
            System.out.println("请选择(1-3):");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    deleteProduct();
                    break;
                case 2:
                    updateProduct();
                    break;
                case 3:
                    System.exit(0);
                    break;
                default:
                    System.out.println("错误");
            }
        }
    }
    private static void deleteProduct() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入商品编码:");
        String barCode = scanner.nextLine();
        Product product = ProductDAO.queryByBarcode(barCode);
        if (barCode.equals(product.getBarCode())) {
            ProductDAO.delete(product);
            System.out.println("你成功删除该商品");
        }else {
            System.out.println("你输入的编码不存在,请重新输入");
        }
    }
    private static void updateProduct() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入商品编码:");
        String barCode = scanner.nextLine();
        Product product = ProductDAO.queryByBarcode(barCode);
        if (product != null && barCode.equals(product.getBarCode())) {
            System.out.println("商品名称(" + product.getProductName() + "):");
            String name = scanner.nextLine();
            if (!name.isEmpty()) {
                product.setProductName(name);
            }

            System.out.println("商品单价(" + product.getPrice() + "):");
            String priceStr = scanner.nextLine();
            if (!priceStr.isEmpty()) {
                float price = Float.parseFloat(priceStr);
                product.setPrice(price);
            }

            System.out.println("商品供应商(" + product.getSupply() + "):");
            String supplier = scanner.nextLine();
            if (!supplier.isEmpty()) {
                product.setSupply(supplier);
            }

            ProductDAO.update(product);
            System.out.println("成功修改该商品");
        } else {
            System.out.println("你输入的编码不存在,请重新输入");
        }
    }



}

util—DBUtil

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBUtil {

    //驱动加载,只需执行一次
    static{
        String driveName = "com.mysql.cj.jdbc.Driver";
        try {
            Class.forName(driveName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //获取链接
    public static Connection getConnection(){
        String url = "jdbc:mysql://localhost:3306/sale?useUnicode=true&characterEncoding=utf-8";
        String user = "root";
        String password = "123456";
        Connection con = null;
        try {
            con = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return con;
    }

    //关闭资源
    public static void close(Connection con, PreparedStatement pst){
        if(con!=null) {
            try {
                con.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(pst!=null) {
            try {
                pst.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

}

vo—Product

package vo;

public class Product {
    private String barCode;
    private String productName;
    private float price;
    private String supply;

    public Product() {
    }

    public Product(String barCode, String productName, float price, String supply) {
        this.barCode = barCode;
        this.productName = productName;
        this.price = price;
        this.supply = supply;
    }

    public String getBarCode() {
        return barCode;
    }

    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public float getPrice() {
        return price;
    }

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

    public String getSupply() {
        return supply;
    }

    public void setSupply(String supply) {
        this.supply = supply;
    }
}

mysql—workbench/Navicat

/*
Navicat MySQL Data Transfer

Source Server         : localhost_3306
Source Server Version : 80032
Source Host           : localhost:3306
Source Database       : xiaoshou

Target Server Type    : MYSQL
Target Server Version : 80032
File Encoding         : 65001

Date: 2023-05-11 10:27:07
*/

SET FOREIGN_KEY_CHECKS=0;
use sale;
-- ----------------------------
-- Table structure for t_shangping
-- ----------------------------
DROP TABLE IF EXISTS `t_shangping`;
CREATE TABLE `t_shangping` (
  `tiaoma` varchar(255) NOT NULL,
  `mingcheng` varchar(255) DEFAULT NULL,
  `danjia` decimal(10,2) DEFAULT NULL,
  `gongyingshang` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`tiaoma`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
select * from t_shangping;

-- ----------------------------
-- Records of t_shangping
-- ----------------------------
INSERT INTO `t_shangping` VALUES ('100001', '手机', '4500.00', '华为');
INSERT INTO `t_shangping` VALUES ('100002', '鼠标', '61.00', '华为');
INSERT INTO `t_shangping` VALUES ('100003', '矿泉水', '2.50', '农夫山泉');
INSERT INTO `t_shangping` VALUES ('100004', '香烟', '20.00', '武汉卷烟厂');
INSERT INTO `t_shangping` VALUES ('100005', '牙膏', '4.50', '中华牙膏厂');
INSERT INTO `t_shangping` VALUES ('200001', '电脑', '4300.00', 'dell');
INSERT INTO `t_shangping` VALUES ('200002', '小明同学', '5.50', '武汉饮料集团');

-- ----------------------------
-- Table structure for t_shouyinmingxi
-- ----------------------------
DROP TABLE IF EXISTS `t_shouyinmingxi`;
CREATE TABLE `t_shouyinmingxi` (
  `liushuihao` varchar(255) NOT NULL,
  `tiaoma` varchar(255) DEFAULT NULL,
  `mingcheng` varchar(255) DEFAULT NULL,
  `danjia` decimal(10,0) DEFAULT NULL,
  `shuliang` int DEFAULT NULL,
  `shouyinyuan` varchar(255) DEFAULT NULL,
  `xiaoshoushijian` datetime DEFAULT NULL,
  PRIMARY KEY (`liushuihao`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
select * from t_shouyinmingxi;
-- ----------------------------
-- Records of t_shouyinmingxi
-- ----------------------------

-- ----------------------------
-- Table structure for t_yonghu
-- ----------------------------
DROP TABLE IF EXISTS `t_yonghu`;
CREATE TABLE `t_yonghu` (
  `yonghuming` varchar(255) NOT NULL,
  `mima` varchar(255) DEFAULT NULL,
  `xingming` varchar(255) DEFAULT NULL,
  `juese` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`yonghuming`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
select * from t_yonghu;
-- ----------------------------
-- Records of t_yonghu
-- ----------------------------
INSERT INTO `t_yonghu` VALUES ('mk', 'mk123', '明空', '管理员');
INSERT INTO `t_yonghu` VALUES ('jx', 'jx123', '瑾熙', '收银员');

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值