Java实战:手把手教你构建简单库存管理系统
Java实战:手把手教你构建简单库存管理系统
商场、超市等场景都需要对库房商品进行有效的库存管理,本文将使用Java构建一个简单的库存管理系统,涵盖商品入库、显示、删除等核心功能,并结合代码进行详细解释。
1. 系统需求分析
本系统需要实现以下功能:
- 系统首页: 展示系统所有功能菜单,供用户选择操作。* 商品入库: 引导用户输入商品名称、颜色、价格和数量等信息,并将商品添加到库存中。* 商品显示: 展示仓库中所有商品的详细信息。* 删除商品: 根据用户输入的商品名称,从库存中删除指定商品。
2. 代码实现
2.1 商品类 (Product)
首先,我们需要定义一个Product类来表示商品,包含商品的基本属性:javaclass Product { private String name; private String color; private double price; private int quantity;
public Product(String name, String color, double price, int quantity) { this.name = name; this.color = color; this.price = price; this.quantity = quantity; }
public void display() { System.out.println('Name: ' + name); System.out.println('Color: ' + color); System.out.println('Price: ' + price); System.out.println('Quantity: ' + quantity); System.out.println(); }
public String getName() { return name; }}
2.2 库存管理系统类 (InventoryManagementSystem)
接下来,我们创建InventoryManagementSystem类来实现库存管理系统的核心逻辑:javaimport java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Scanner;
public class InventoryManagementSystem { private List
public InventoryManagementSystem() { products = new ArrayList<>(); }
public void displayMenu() { System.out.println('1. Add Product'); System.out.println('2. Display Products'); System.out.println('3. Delete Product'); System.out.println('4. Exit'); System.out.print('Enter your choice: '); }
public void addProduct() { Scanner scanner = new Scanner(System.in); System.out.print('Do you want to add a product (yes/no)? '); String choice = scanner.nextLine();
if (choice.equalsIgnoreCase('yes')) { System.out.print('Enter product name: '); String name = scanner.nextLine(); System.out.print('Enter product color: '); String color = scanner.nextLine(); System.out.print('Enter product price: '); double price = scanner.nextDouble(); System.out.print('Enter product quantity: '); int quantity = scanner.nextInt();
Product product = new Product(name, color, price, quantity); products.add(product);
System.out.println('Product added successfully!'); System.out.println(); } }
public void displayProducts() { if (products.isEmpty()) { System.out.println('No products found in the inventory.'); } else { System.out.println('Inventory Products:'); for (Product product : products) { product.display(); } } }
public void deleteProduct() { if (products.isEmpty()) { System.out.println('No products found in the inventory.'); } else { Scanner scanner = new Scanner(System.in); System.out.print('Enter the product name to delete: '); String name = scanner.nextLine();
Iterator<Product> iterator = products.iterator(); while (iterator.hasNext()) { Product product = iterator.next(); if (product.getName().equalsIgnoreCase(name)) { iterator.remove(); System.out.println('Product deleted successfully!'); System.out.println(); return; } }
System.out.println('Product not found in the inventory.'); System.out.println(); } }
public static void main(String[] args) { InventoryManagementSystem system = new InventoryManagementSystem(); Scanner scanner = new Scanner(System.in); int choice;
do { system.displayMenu(); choice = scanner.nextInt(); scanner.nextLine(); // Consume newline character
switch (choice) { case 1: system.addProduct(); break; case 2: system.displayProducts(); break; case 3: system.deleteProduct(); break; case 4: System.out.println('Exiting the program. Goodbye!'); break; default: System.out.println('Invalid choice. Please try again.'); System.out.println(); } } while (choice != 4); }}
3. 代码解释
-
Product类: 该类表示单个商品,包含名称、颜色、价格和数量等属性,以及用于显示商品信息的
display方法和获取商品名称的getName方法。 -
InventoryManagementSystem类: *
products: 使用ArrayList存储所有商品对象。 *displayMenu: 显示系统功能菜单。 *addProduct: 实现商品入库功能,接收用户输入的商品信息并创建Product对象,添加到products集合中。 *displayProducts: 遍历products集合,调用Product对象的display方法显示所有商品信息。 *deleteProduct: 接收用户输入的商品名称,使用迭代器遍历products集合,找到并删除匹配的商品。 *main: 程序入口,创建InventoryManagementSystem对象,并通过循环展示菜单,根据用户选择调用相应功能。
4. 总结
本文通过Java实现了一个简单的库存管理系统,演示了如何使用集合、迭代器、增强for循环等知识点,以及面向对象编程的基本思想。你可以根据实际需求扩展该系统,例如添加修改商品信息、统计库存等功能。
原文地址: https://www.cveoy.top/t/topic/S24 著作权归作者所有。请勿转载和采集!