Java程序设计库存管理系统

类 Product

  • 包含了产品的名称(name)、数量(quantity)和价格(price)这三个属性。
  • 构造函数用于初始化这些属性。
  • 提供了获取属性值的方法,以及设置数量的方法。

类 InventorySystem

  • 用 ArrayList 来存储产品对象。
  • addProduct 方法用于添加产品,并给出添加成功的提示。
  • displayProducts 方法用于展示产品信息,若列表为空则给出相应提示,否则遍历并打印每个产品的详情。
  • updateProductQuantity 方法根据输入的产品名称更新其数量,若未找到指定产品则提示。

main 方法

  • 创建了库存管理系统对象和扫描器对象。
  • 通过一个无限循环展示操作选项,包括添加产品、显示产品信息、更新产品数量和退出。
  • 根据用户输入执行相应的操作。
import java.util.ArrayList;
import java.util.Scanner;

class Product {
    private String name;
    private int quantity;
    private double price;

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

    public String getName() {
        return name;
    }

    public int getQuantity() {
        return quantity;
    }

    public double getPrice() {
        return price;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

class InventorySystem {
    private ArrayList<Product> products = new ArrayList<>();

    public void addProduct(Product product) {
        products.add(product);
        System.out.println("产品添加成功。");
    }

    public void displayProducts() {
        if (products.isEmpty()) {
            System.out.println("库存管理系统中没有产品信息。");
        } else {
            System.out.println("库存管理系统中的产品信息:");
            for (Product product : products) {
                System.out.println("产品名称: " + product.getName() + ", 数量: " + product.getQuantity() + ", 价格: " + product.getPrice());
            }
        }
    }

    public void updateProductQuantity(String productName, int newQuantity) {
        for (Product product : products) {
            if (product.getName().equalsIgnoreCase(productName)) {
                product.setQuantity(newQuantity);
                System.out.println("产品数量更新成功。");
                return;
            }
        }
        System.out.println("找不到指定产品。");
    }

    public static void main(String[] args) {
        InventorySystem inventorySystem = new InventorySystem();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n1. 添加产品\n2. 显示所有产品信息\n3. 更新产品数量\n4. 退出");
            System.out.print("请输入您的选择: ");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("请输入产品名称: ");
                    String name = scanner.next();
                    System.out.print("请输入产品数量: ");
                    int quantity = scanner.nextInt();
                    System.out.print("请输入产品价格: ");
                    double price = scanner.nextDouble();
                    Product newProduct = new Product(name, quantity, price);
                    inventorySystem.addProduct(newProduct);
                    break;

                case 2:
                    inventorySystem.displayProducts();
                    break;

                case 3:
                    System.out.print("请输入要更新数量的产品名称: ");
                    String productName = scanner.next();
                    System.out.print("请输入新的产品数量: ");
                    int newQuantity = scanner.nextInt();
                    inventorySystem.updateProductQuantity(productName, newQuantity);
                    break;

                case 4:
                    System.out.println("程序退出。再见!");
                    System.exit(0);

                default:
                    System.out.println("无效选择。请重试。");
            }
        }
    }
}

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xingxing?!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值