Java实现超市管理系统简单的增,删,改,查功能

 下面是一个简单的超市管理系统,只实现了简单的增,删,改,查功能

创建一个主类 

import java.util.Scanner;
import java.util.ArrayList;

public class Supermarket {
    public static ArrayList<Product> productList = new ArrayList<>();//创建一个产品动态数组
    public static void main(String[] args) {
        while (true) {
            System.out.println("请选择操作:");
            System.out.println("1. 添加商品" + "\t" + "2. 修改商品" + "\t" + "3. 删除商品" + "\t" + "4. 查询商品");
            System.out.println("请输入选项:");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.nextLine();
            switch (choice) {
                case "1" -> addProduct.add();
                case "2" -> changeProduct.change();
                case "3" -> deleteProduct.delete();
                case "4" -> searchProduct.search();
                default -> System.out.println("无效的选项,请重新输入!");
            }
        }
    }
}

创建一个Product类

public class Product {
    private String id;//编号
    private String name;//名字
    private double price;//价格
    public Product(){

    }
    public Product(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

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

    public String InforShow() {
        return "商品编号:" + id + "  商品名称:" + name + "  商品单价:" + price + "元。";
    }
}

创建一个addProduct类,实现增加商品

import java.util.Scanner;

public class addProduct {
    //增加商品
    public static void add() {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入商品编号:");
        String id = scanner.next();
        System.out.println("请输入商品名称:");
        String name = scanner.next();
        System.out.println("请输入商品单价:");
        double price = scanner.nextDouble();
        Product product = new Product(id, name, price);
        Supermarket.productList.add(product);
        System.out.println("商品添加成功!");
    }
}

创建一个deleteProduct类,实现删除商品

import java.util.Scanner;

public class deleteProduct {
//删除商品
    public static void delete() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的商品编号:");
        String id = scanner.nextLine();
        boolean found = false;
        for (Product product : Supermarket.productList) {
            if (product.getId().equals(id)) {
                Supermarket.productList.remove(product);
                found = true;
                System.out.println("商品删除成功!");
                break;
            }
        }
        if (!found) {
            System.out.println("未找到该商品!");
        }
    }
}

创建一个changProduct类,实现更改商品

public class changeProduct {
//更改商品信息
    public static void change() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的商品编号:");
        String id = scanner.nextLine();
        boolean found = false;
        for (Product product : Supermarket.productList) {
            if (product.getId().equals(id)) {
                System.out.println("请输入新的商品名称:");
                String name = scanner.nextLine();
                System.out.println("请输入新的商品单价:");
                double price = scanner.nextDouble();
                product.setName(name);
                product.setPrice(price);
                found = true;
                System.out.println("商品修改成功!");
                break;
            }
        }
        if (!found) {
            System.out.println("未找到该商品!");
        }
    }
}

创建一个searchProduct类,实现查找商品

import java.util.Scanner;
//查询商品信息
public class searchProduct {
    public static void search() {
        for (Product product:Supermarket.productList) {
                System.out.println(product.InforShow());
            }
    }
}

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较常见的Java项目,我可以给你提供一些思路和代码示例。 1. 需求分析 超市商品信息管理系统主要包括以下功能: - 商品信息的录入、删除修改询 - 商品分类管理 - 商品库存管理 2. 构建数据库 首先我们需要定义数据库表结构,可以使用MySQL或者其他关系型数据库。以下是一个简单的表结构: ``` CREATE TABLE goods ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, price DECIMAL(10,2) NOT NULL, category_id INT(11) NOT NULL, stock INT(11) NOT NULL, PRIMARY KEY (id) ); CREATE TABLE categories ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, PRIMARY KEY (id) ); ``` 3. Java代码实现 下面是一个简单Java代码示例,包含了商品信息功能。 ```java import java.sql.*; public class GoodsManager { // 连接数据库的信息 private static final String URL = "jdbc:mysql://localhost:3306/test"; private static final String USERNAME = "root"; private static final String PASSWORD = "password"; // 增加商品信息 public void addGoods(String name, double price, int categoryId, int stock) throws SQLException { Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); PreparedStatement stmt = conn.prepareStatement("INSERT INTO goods (name, price, category_id, stock) VALUES (?, ?, ?, ?)"); stmt.setString(1, name); stmt.setDouble(2, price); stmt.setInt(3, categoryId); stmt.setInt(4, stock); stmt.executeUpdate(); stmt.close(); conn.close(); } // 删除商品信息 public void deleteGoods(int id) throws SQLException { Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); PreparedStatement stmt = conn.prepareStatement("DELETE FROM goods WHERE id = ?"); stmt.setInt(1, id); stmt.executeUpdate(); stmt.close(); conn.close(); } // 修改商品信息 public void updateGoods(int id, String name, double price, int categoryId, int stock) throws SQLException { Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); PreparedStatement stmt = conn.prepareStatement("UPDATE goods SET name = ?, price = ?, category_id = ?, stock = ? WHERE id = ?"); stmt.setString(1, name); stmt.setDouble(2, price); stmt.setInt(3, categoryId); stmt.setInt(4, stock); stmt.setInt(5, id); stmt.executeUpdate(); stmt.close(); conn.close(); } // 商品信息 public ResultSet queryGoods(String name, int categoryId) throws SQLException { Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM goods WHERE name LIKE ? AND category_id = ?"); stmt.setString(1, "%" + name + "%"); stmt.setInt(2, categoryId); ResultSet rs = stmt.executeQuery(); return rs; } } ``` 以上代码实现商品信息功能,具体使用方法可以根据自己的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值