Java【案例6 您所在的位置:网站首页 超市盘库存为什么对不上 Java【案例6

Java【案例6

2023-09-10 19:42| 来源: 网络整理| 查看: 265

【案例6-1】 库存管理系统

【案例介绍】 1.任务描述 像商城和超市这样的地方,都需要有自己的库房,并且库房商品的库存变化有专人记录,这样才能保证商城和超市正常运转。 本例要求编写一个程序,模拟库存管理系统。该系统主要包括系统首页、商品入库、商品显示和删除商品功能。每个功能的具体要求如下: (1) 系统的首页:用于显示系统所有的操作,并且可以选择使用某一个功能。 (2) 商品入库功能:首先提示是否要录入商品,根据用户输入的信息判断是否需要录入商品。如果需要录入商品,则需要用户输入商品的名称、颜色、价格和数量等信息。录入完成后,提示商品录入成功并打印所有商品。如果不需要录入商品,则返回系统首页。 (3) 商品显示功能:用户选择商品显示功能后,在控制台打印仓库所有商品信息。 (4) 删除商品功能:用户选择删除商品功能后,根据用户输入的商品编号删除商品,并在控制台打印删除后的所有商品。 本案例要求使用Collection集合存储自定义的对象,并用迭代器、增强for循环遍历集合。

package com.j2se.myInstances.example6_1; import java.util.*; public class Demo { private static List products = new ArrayList(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); Product product = new Product("iPhone X", "Red", 5999, 3); products.add(product); int choose; while (true) { printMenu(); choose = sc.nextInt(); if (choose == 1) { Product p = new Product(); System.out.println("产品名称:"); String pname = sc.next(); System.out.println("产品颜色:"); String pcolor = sc.next(); System.out.println("产品价格:"); double price = sc.nextDouble(); System.out.println("产品数量:"); int pnum = sc.nextInt(); p.setName(pname); p.setColor(pcolor); p.setPrice(price); p.setNum(pnum); addProduct(p); } else if (choose == 2) { showInfo(products); System.out.println("\n"); } else if (choose == 3) { System.out.println("请输入需要删除的商品编号:"); int i = sc.nextInt(); delete(i); } else { System.out.println("输入有误,请重新输入!"); } } } public static void delete(int i) { i--; products.remove(i); System.out.println("出库成功!\n"); } public static void addProduct(Product p) { products.add(p); System.out.println("产品已入库\n"); } public static void showInfo(Collection products) { Iterator it = products.iterator(); System.out.println("编号\t产品名称\t\t颜色\t价格\t\t库存"); int n = 1; while (it.hasNext()) { Product p = it.next(); System.out.println(n+"\t"+p.getName() + "\t" + p.getColor() + "\t" + p.getPrice() + "\t" + p.getNum()); n++; } } public static void printMenu() { System.out.println("欢迎使用库房管理系统~"); System.out.println("1. 商品入库"); System.out.println("2. 商品展示"); System.out.println("3. 商品出库"); System.out.println("请输入您要选择的操作:"); } }

补充Product类:

class Product { private String name; private String color; private double price; private int num; public Product(String name, String color, double price, int num) { this.name = name; this.color = color; this.price = price; this.num = num; } public String getName() { return name;} public String getColor() { return color; } public double getPrice() {return price; } public int getNum() {return num;} }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有