5-2修改数量不知道为什么错了

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>


正在回答

登陆购买课程后可参与讨论,去登陆

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;
            }
        }
        
    }

如果还有问题,可以再次提问。祝学习愉快~

一叶知秋519 2017-12-26 14:01:31

你的GoodsInCart gic = new GoodsInCart();应该写到while循环内判断商品是否存在的判断外,写为GoodsInCart goodsInCar = it.next();  应该是取购物车中的商品,对它的数量进行修改。 如果还有问题的话,建议将代码贴全,方便帮助你解答问题。祝学习愉快~

  • 提问者 EricFn #1
    原本的问题是修改数量里的while循环里if那个equals判断出错,运行的时候修改数量打上编号就跳到debug
    2017-12-26 14:06:24
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码