Angus.524 2021-12-21 10:40 采纳率: 50%
浏览 136
已结题

问题:要求实现商品超市的商品维护模块。

商品维护模块主要包括:商品添加(输入商品编号,名称,价格,数量),商品列表显示,商品入库(输入商品编号,然后提示用户输入入库数量,然后进行修改操作),商品出库(输入商品编号,然后提示用户输入出库数量,然后进行修改操作),修改商品价格(提示用户输入编号,然后输入新价格)。

采用Scanner提示用户进行输入,用System.out.println实现界面。

  • 写回答

2条回答 默认 最新

  • 关注

    public class Demo3Redo {

    public static void main(String[] args) {
        Map<String, Good> goodsMap = new HashMap<String, Good>() {
            {
                put("GOOD1", new Good("GOOD1", "商品1", 10.0, 30));
                put("GOOD2", new Good("GOOD2", "商品2", 90.0, 20));
                put("GOOD3", new Good("GOOD3", "商品3", 10.3, 11));
                put("GOOD4", new Good("GOOD4", "商品4", 9.0, 10));
    
            }
        };
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入菜单编号:");
            System.out.println("1 商品展示:");
            System.out.println("2 商品入库:");
            System.out.println("3 商品出库:");
            System.out.println("4 价格修改:");
            System.out.println("5 退出:");
            String flag = sc.nextLine();
            switch (flag) {
                case "1":
                    System.out.println("商品编号  商品名称  商品价格  商品库存");
                    for (Map.Entry<String, Good> goodEntry : goodsMap.entrySet()) {
                        Good good = goodEntry.getValue();
                        System.out.println(good.getId() + "    " + good.getName() + "    " + good.getPrice() + "    " + good.getInventory());
                    }
                    break;
                case "2":
                    System.out.println("请输入商品编号:");
                    String id = sc.nextLine();
                    Good good = goodsMap.get(id);
                    System.out.println("请输入入库数量:");
                    String count = sc.nextLine();
                    good.setInventory(good.getInventory() + new Integer(count));
                    break;
                case "3":
                    System.out.println("请输入商品编号:");
                    String idIncre = sc.nextLine();
                    Good goodIncre = goodsMap.get(idIncre);
                    System.out.println("请输入出库数量:");
                    String incre = sc.nextLine();
                    int i = goodIncre.getInventory() - new Integer(incre);
                    if (i < 0) {
                        i = 0;
                    }
                    goodIncre.setInventory(i);
                    break;
                case "4":
                    System.out.println("请输入商品编号:");
                    String idPrice = sc.nextLine();
                    Good goodPrice = goodsMap.get(idPrice);
                    System.out.println("价格:");
                    String price = sc.nextLine();
                    goodPrice.setPrice(new Double(price));
                    break;
                case  "5":
                    System.exit(0);
            }
        }
    
    }
    

    }

    class Good {
    private String id;
    private String name;
    private Double price;
    private int inventory;

    public Good(String id, String name, Double price, int inventory) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.inventory = inventory;
    }
    
    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 getInventory() {
        return inventory;
    }
    
    public void setInventory(int inventory) {
        this.inventory = inventory;
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月23日
  • 已采纳回答 12月23日
  • 创建了问题 12月21日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因