5-2修改数量不知道为什么错了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | package com.imooc.goods; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class ShoppingCart { private Map<String, GoodsInCart> shoppingCart; Scanner sc = new Scanner(System.in); public ShoppingCart() { shoppingCart = new HashMap<String,GoodsInCart>(); } public ShoppingCart(Map<String, GoodsInCart> shoppingCart) { this .shoppingCart = shoppingCart; } public Map<String, GoodsInCart> getShoppingCart() { return shoppingCart; } public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) { this .shoppingCart = shoppingCart; } public void addGoodsToCart(GoodsManage gm) { // 添加商品到购物车 gm.displayAllGoods(); Iterator<Goods> it = gm.getGoodsSet().iterator(); boolean flag = true ; while (flag) { System.out.println( "请输入要添加的商品编号:" ); String id = sc.next(); GoodsInCart gic = new GoodsInCart(); while (it.hasNext()) { Goods g = it.next(); if (id.equals(g.getGoodsId())) { gic.setGoods(g); break ; } } System.out.println( "请输入要添加的商品数量:" ); int num = sc.nextInt(); gic.setNum(num); shoppingCart.put(id, gic); if (num> 0 ) flag = false ; else System.out.println( "输入错误,请重新输入!" ); continue ; } } public void updateNumInCart() { // 修改购物车中的商品数量 Iterator<GoodsInCart> it = shoppingCart.values().iterator(); System.out.println( "请输入要修改的商品编号:" ); String id = sc.next(); GoodsInCart gic = new GoodsInCart(); while (it.hasNext()) { if (id.equals(gic.getGoods().getGoodsId())){ it.next(); break ; } else { System.out.println( "商品编号不存在" ); continue ; } } System.out.println( "请输入要修改的商品数量:" ); int num = sc.nextInt(); gic.setNum(num); if (num== 0 ){ System.out.println( "因为商品数量为0,商品被移除" ); shoppingCart.remove(id,gic); } else { shoppingCart.put(id, gic); } } public void displayAllInCart() { // 显示购物车中的所有商品 if (shoppingCart.size() != 0 ) { Iterator<GoodsInCart> it = shoppingCart.values().iterator(); while (it.hasNext()) { GoodsInCart gic = it.next(); Goods g = gic.getGoods(); System.out.println( "商品编号:" + g.getGoodsId() + ",商品名称:" + g.getGoodsName() + ",商品价格:" + g.getPrice() + ",商品描述:" + g.getGoodsDesp() + ",商品数量:" + gic.getNum()); } } else { System.out.println( "购物车是空的哟,赶紧装满吧~" ); } } public void settleAccounts() { // 结算 double sum = 0 ; Iterator<GoodsInCart> it = shoppingCart.values().iterator(); while (it.hasNext()) { GoodsInCart gic = it.next(); Goods g = gic.getGoods(); System.out.println( "商品编号:" + g.getGoodsId() + ",商品名称:" + g.getGoodsName() + ",商品价格:" + g.getPrice() + ",商品描述:" + g.getGoodsDesp() + ",商品数量:" + gic.getNum()); double sum1 = g.getPrice() * gic.getNum(); sum = sum + sum1; } System.out.println( "商品的总价为:" + sum); shoppingCart.clear(); } } |
1 | <br> |
18
收起
正在回答
2回答
你好,以下是调试后的代码,修改数量时,直接在判断存在的if语句中进行,gic也定义到while循环内,即:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public void updateNumInCart() { // 修改购物车中的商品数量 //Scanner sc=new Scanner(System.in); Iterator<GoodsInCart> it = shoppingCart.values().iterator(); System.out.println( "请输入要修改的商品编号:" ); String id = sc.next(); // GoodsInCart gic = new GoodsInCart(); while (it.hasNext()) { GoodsInCart gic = it.next(); if (id.equals(gic.getGoods().getGoodsId())){ System.out.println( "请输入要修改的商品数量:" ); int num = sc.nextInt(); gic.setNum(num); if (num== 0 ){ System.out.println( "因为商品数量为0,商品被移除" ); shoppingCart.remove(id,gic); } else { shoppingCart.put(id, gic); } break ; } else { System.out.println( "商品编号不存在" ); continue ; } } } |
如果还有问题,可以再次提问。祝学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧