请老师帮看一下。

请老师帮看一下。

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
public class Goods {
 private String goodId;//编号
 private String goodsName;//名称
 private double price;//价格
 private String goodsDesp;//描述
  
 //构造方法
 public Goods(String goodId, String goodsName, double price, String goodsDesp) {
  super();
  this.goodId = goodId;
  this.goodsName = goodsName;
  this.price = price;
  this.goodsDesp = goodsDesp;
 }
 //getter和setter方法
 public String getGoodId() {
  return goodId;
 }
 public void setGoodId(String goodId) {
  this.goodId = goodId;
 }
 public String getGoodsName() {
  return goodsName;
 }
 public void setGoodsName(String goodsName) {
  this.goodsName = goodsName;
 }
 public double getPrice() {
  return price;
 }
 public void setPrice(double price) {
  this.price = price;
 }
 public String getGoodsDesp() {
  return goodsDesp;
 }
 public void setGoodsDesp(String goodsDesp) {
  this.goodsDesp = goodsDesp;
 }
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((goodId == null) ? 0 : goodId.hashCode());
  result = prime * result + ((goodsDesp == null) ? 0 : goodsDesp.hashCode());
  result = prime * result + ((goodsName == null) ? 0 : goodsName.hashCode());
  long temp;
  temp = Double.doubleToLongBits(price);
  result = prime * result + (int) (temp ^ (temp >>> 32));
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Goods other = (Goods) obj;
  if (goodId == null) {
   if (other.goodId != null)
    return false;
  else if (!goodId.equals(other.goodId))
   return false;
  if (goodsDesp == null) {
   if (other.goodsDesp != null)
    return false;
  else if (!goodsDesp.equals(other.goodsDesp))
   return false;
  if (goodsName == null) {
   if (other.goodsName != null)
    return false;
  else if (!goodsName.equals(other.goodsName))
   return false;
  if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
   return false;
  return true;
 }
 @Override
 public String toString() {
  return "Goods [商品编号:" + goodId + ", 商品名称:" + goodsName + ", 商品价格:" + price + ", 商品描述:" + goodsDesp
    "]";
 }
  
}
 
 
public class GoodsInCart {
 private Goods goods;//商品信息
 private int num;   //商品数量
  
 //构造方法
 public GoodsInCart(Goods goods, int num) {
  super();
  this.goods = goods;
  this.num = num;
 }
 //getter和setter方法
 public Goods getGoods() {
  return goods;
 }
 public void setGoods(Goods goods) {
  this.goods = goods;
 }
 public int getNum() {
  return num;
 }
 public void setNum(int num) {
  this.num = num;
 
}
 
 
public class GoodsManage {
 private Set<Goods> goodsSet = new HashSet<Goods>();// 存放商品的容器
 // 构造方法
 public GoodsManage() {
   
 }
 public GoodsManage(Set<Goods> goodsSet) {
  super();
  this.goodsSet = goodsSet;
 }
 // getter和setter方法
 public Set<Goods> getGoodsSet() {
  return goodsSet;
 }
 public void setGoodsSet(Set<Goods> goodsSet) {
  this.goodsSet = goodsSet;
 }
 // 商品信息导入
 public void importGoods() {
  goodsSet.add(new Goods("goods004""手机"2300"Android手机"));
  goodsSet.add(new Goods("goods002""饮水机"299"带净化功能的饮水机"));
  goodsSet.add(new Goods("goods003""笔记本电脑"4999"15寸笔记本电脑"));
  goodsSet.add(new Goods("goods001""水杯"56"不锈钢水杯"));
  this.setGoodsSet(goodsSet);
  System.out.println("商品讯息导入");
  System.out.println("导入成功!!!");
 }
 // 显示所有商品信息
 public void displayAllGoods() {
  System.out.println("显示所有商品信息\n所有商品信息为:");
  Iterator it=goodsSet.iterator();
  while(it.hasNext()) {
   System.out.println(it.next());
  }
 }
}
 
 
public class ShoppingCart {
 // 属性:购物车
 Map<String, GoodsInCart> shoppingCart = new HashMap<String, GoodsInCart>();
 // 构造方法
 public ShoppingCart(Map<String, GoodsInCart> shoppingCart) {
  super();
  this.shoppingCart = shoppingCart;
 }
 // getter和setter方法
 public Map<String, GoodsInCart> getShoppingCart() {
  return shoppingCart;
 }
 public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) {
  this.shoppingCart = shoppingCart;
 }
 Scanner s=new Scanner(System.in);
 // 添加商品到购物车
 public void addGoodsToCart(GoodsManage gm) {
  System.out.println("添加商品到购物车");
  gm.displayAllGoods();
  Iterator<Goods> it=gm.getGoodsSet().iterator();
  while(true) {
   try {
    System.out.println("请输入要添加的商品编号:");
    String id=s.next();
    GoodsInCart gic=new GoodsInCart();
    while(it.hasNext()) {
     Goods n=it.next();
     if(id.equals(n.getGoodId())){
      gic.setGoods(n);
      break;
     }
    }
    System.out.println("请输入要添加的商品数量:");
    int num=s.nextInt();
    gic.setNum(num);
    shoppingCart.put(id, gic);
   }catch(Exception e) {
    System.out.println("输入错误,请重新输入!");
    continue;
   }break;
  }
 }
 // 修改购物车中的商品数量
 public void updateNumInCart() {
  System.out.println("修改购物车中商品数量:");
  Iterator<GoodsInCart> it=shoppingCart.values().iterator();
  System.out.println("请输入要修改的商品编号:");
  String id=s.next();
  GoodsInCart gic=new GoodsInCart();
  while(it.hasNext()) {
   if(id.equals(gic.getGoods().getGoodId())) {
    it.next();
    break;
   }
  }
  System.out.println("请输入要修改的商品数量:");
  int num=s.nextInt();
  gic.setNum(num);
  shoppingCart.put(id, gic);
 }
 // 显示购物车中的所有商品
 public void displayAllInCart() {
  System.out.println("显示购物车中所有商品信息");
  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.getGoodId()+",名称:"+g.getGoodsName()+",价格:"
      +g.getPrice()+",商品描述:"+g.getGoodsDesp()+",数量:"+gic.getNum()+"]");
                }
                }else {
                 System.out.println("购物车是空的哟,赶紧装满吧~");
  }
 }
 // 结算
 public void settleAccounts() {
  System.out.println("结算");
  double sum=0;
  System.out.println("商品的总价为:"+sum);
  Iterator<GoodsInCart> it=shoppingCart.values().iterator();
  while(it.hasNext()) {
   GoodsInCart gic = it.next();
   Goods g=gic.getGoods();
   System.out.println("商品信息[编号:"+g.getGoodId()+",名称:"+g.getGoodsName()+",价格:"
     +g.getPrice()+",商品描述:"+g.getGoodsDesp()+",数量:"+gic.getNum()+"]");
   double sum1=g.getPrice()*gic.getNum();
   sum=sum+sum1;
    
  }
 }
}
 
 
package com.imooc.cart;
import java.util.Scanner;
/**
 * 测试类
 * @author xiaod
 *
 */
public class TestDemo {
  
 //商品管理
 public void manageGoods() {
   
  System.out.println("======================");
  System.out.println("*****商品管理*****");
  System.out.println("1--商品信息导入");
  System.out.println("2--显示所有商品讯息");
  System.out.println("9--回上一层");
  System.out.println("======================");
   
  Scanner src = new Scanner(System.in);
  int num = src.nextInt();
   
  GoodsManage goodsManage = new GoodsMaanage();
   
  //根据商品编号执行操作
  switch(num) {
  case 1:
   goodsManage.importGoods();
   this.manageGoods();
   break;
  case 2:
   goodsManage.displayAllGoods();
   break;
  case 0:
   break;
  }
 }
 public static void main(String[] args) {
  TestDemo demo = new TestDemo();
  //开始进入点
  System.out.println("======================");
  System.out.println("*****主菜单*****");
  System.out.println("1--商品管理");
  System.out.println("2--购物车");
  System.out.println("0--退出");
  System.out.println("======================");
   
  System.out.println("*****请输入对应的数字操作*****");
  Scanner src = new Scanner(System.in);
  int num = src.nextInt();
   
  //根据编号执行操作
  switch(num) {
  case 1:
   demo.manageGoods();
   break;
  case 2:
   break;
  case 0:
   break;
  }
 
 }
}
 
测试类和购物车类有错误呢。请老师详细解答,我仿写的。


正在回答

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

3回答

ShoppingCart类缺少必要的包,

需要导入:

import java.util.HashSet;

import java.util.Set;

import java.util.Iterator;

import java.util.Scanner;

祝学习愉快~

这个头像是黑豹 2018-03-25 20:33:14

第96行的super是什么意思啊,能不能告诉我下,谢谢

  • super()是调用父类的构造方法
    2018-03-26 10:55:49
提问者 无厘头的童年 2017-09-23 12:50:44

http://img1.sycdn.imooc.com/climg//59c5e7f50001d75d07000162.jpg这里报错是什么原因吖,还有运行商品导入没有商品显示呢。

  • 报错原因是你的GoodsInCart类中是没有无参的构造方法的。第二个问题,把GoodsManage类对象的定义放到主方法里面,给manageGoods方法传一个参数就是GoodsManage对象,不然每次调用manageGoods方法都会创建一个新的GoodsManage对象,所以没有商品显示
    2017-09-23 18:15:19
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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