答案:系统通过Java控制台实现商品增删改查,使用ArrayList存储数据,Scanner接收输入,包含添加、查询、修改、删除商品及退出功能,适合初学者练习面向对象编程与基础数据结构。

制作一个小型商品管理系统可以用Java结合控制台输入输出完成,适合初学者练习面向对象编程和基础数据结构。系统可以实现商品的增删改查功能,不需要数据库,使用集合类存储数据即可。
1. 系统功能设计
小型商品管理系统应具备以下基本功能:
添加商品:输入商品编号、名称、价格、库存等信息 查看所有商品:列出当前系统中所有商品 根据编号查询商品:通过商品ID查找具体信息 修改商品信息:更新价格或库存 删除商品:按编号移除商品 退出系统:结束程序运行
2. 创建商品类(Product)
定义一个商品类来封装商品属性和行为:
public class Product { private String id; private String name; private double price; private int stock; public Product(String id, String name, double price, int stock) { this.id = id; this.name = name; this.price = price; this.stock = stock; } // Getter 和 Setter 方法 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 int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } @Override public String toString() { return "商品编号:" + id + " | 名称:" + name + " | 价格:" + price + "元 | 库存:" + stock + "件"; }}
3. 主程序逻辑(使用Scanner和ArrayList)
在主类中使用ArrayList存储商品列表,并用Scanner接收用户输入:
立即学习“Java免费学习笔记(深入)”;
import java.util.ArrayList;import java.util.Scanner;public class ProductManager { private ArrayList products = new ArrayList(); private Scanner scanner = new Scanner(System.in); public void start() { while (true) { System.out.println("n=== 小型商品管理系统 ==="); System.out.println("1. 添加商品"); System.out.println("2. 查看所有商品"); System.out.println("3. 查询商品"); System.out.println("4. 修改商品"); System.out.println("5. 删除商品"); System.out.println("6. 退出"); System.out.print("请选择操作:"); int choice = scanner.nextInt(); scanner.nextLine(); // 消费换行符 switch (choice) { case 1 -> addProduct(); case 2 -> showAllProducts(); case 3 -> findProduct(); case 4 -> updateProduct(); case 5 -> deleteProduct(); case 6 -> { System.out.println("系统已退出!"); return; } default -> System.out.println("无效选择,请重试。"); } } } private void addProduct() { System.out.print("请输入商品编号:"); String id = scanner.nextLine(); if (findProductById(id) != null) { System.out.println("该编号商品已存在!"); return; } System.out.print("请输入商品名称:"); String name = scanner.nextLine(); System.out.print("请输入价格:"); double price = scanner.nextDouble(); System.out.print("请输入库存数量:"); int stock = scanner.nextInt(); scanner.nextLine(); products.add(new Product(id, name, price, stock)); System.out.println("商品添加成功!"); } private void showAllProducts() { if (products.isEmpty()) { System.out.println("暂无商品信息。"); } else { for (Product p : products) { System.out.println(p); } } } private void findProduct() { System.out.print("请输入要查询的商品编号:"); String id = scanner.nextLine(); Product p = findProductById(id); if (p != null) { System.out.println("找到商品:" + p); } else { System.out.println("未找到该商品!"); } } private Product findProductById(String id) { for (Product p : products) { if (p.getId().equals(id)) { return p; } } return null; } private void updateProduct() { System.out.print("请输入要修改的商品编号:"); String id = scanner.nextLine(); Product p = findProductById(id); if (p == null) { System.out.println("未找到该商品!"); return; } System.out.print("请输入新名称(原:" + p.getName() + "):"); String name = scanner.nextLine(); System.out.print("请输入新价格(原:" + p.getPrice() + "):"); double price = scanner.nextDouble(); System.out.print("请输入新库存(原:" + p.getStock() + "):"); int stock = scanner.nextInt(); scanner.nextLine(); p.setName(name); p.setPrice(price); p.setStock(stock); System.out.println("商品信息已更新!"); } private void deleteProduct() { System.out.print("请输入要删除的商品编号:"); String id = scanner.nextLine(); Product p = findProductById(id); if (p != null) { products.remove(p); System.out.println("商品已删除!"); } else { System.out.println("未找到该商品!"); } } public static void main(String[] args) { new ProductManager().start(); }}
4. 运行与扩展建议
这个系统目前基于内存存储,关闭程序后数据会丢失。如需持久化,可考虑以下改进:
将商品数据保存到文本文件中,启动时读取加载 使用简单的CSV格式存储,便于查看和编辑 加入输入验证,防止空值或负数价格/库存 封装菜单显示为单独方法,提高代码可读性基本上就这些。不复杂但容易忽略细节,比如Scanner换行处理和重复ID检查。掌握这个项目后,可以尝试加入图形界面(Swing)或连接数据库(JDBC)进一步提升。
以上就是如何使用Java制作小型商品管理系统的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/32825.html
微信扫一扫
支付宝扫一扫