添加商品到购物车报错
package com.imooc.goods; /** * 商品信息类 * @author ZYH * */ public class Goods { private String goodsId;// 商品编号 private String goodsName;// 商品名称 private double price;// 商品价格 private String goodsDesp;// 商品描述 // 构造方法 public Goods() { } public Goods(String goodsId, String goodsName, double price, String goodsDesp) { this.setGoodsId(goodsId); this.setGoodsName(goodsName); this.setPrice(price); this.setGoodsDesp(goodsDesp); } // get、set方法 public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; } 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 + ((goodsDesp == null) ? 0 : goodsDesp.hashCode()); result = prime * result + ((goodsId == null) ? 0 : goodsId.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.getClass() == Goods.class) { Goods goods = (Goods) obj; return goods.getGoodsId().equals(goodsId) && goods.getGoodsName().equals(goodsName) && (goods.getPrice() == price) && goods.getGoodsDesp().equals(goodsDesp); } return false; } @Override public String toString() { return "商品信息[编号:" + this.getGoodsId() + ", 名称:" + this.getGoodsName() + ", 价格:" + this.getPrice() + ", 描述:" + this.getGoodsDesp() + "]"; } } package com.imooc.goods; /** * 商品管理类 */ import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class GoodsManage { private Set<Goods> goods;//存放集合 public Set<Goods> getGoods() { return goods; } public void setGoods(Set<Goods> goods) { this.goods = goods; } //构造方法 public GoodsManage() { } public GoodsManage(Set<Goods> goods) { this.setGoods(goods); } Set<Goods> set=new HashSet<Goods>(); //商品信息导入 public void importGoods() { Goods good1=new Goods("goods001","水杯",56,"不锈钢水杯"); Goods good2 = new Goods("goods002", "饮水机", 299, "带净化功能的饮水机"); Goods good3 = new Goods("goods003", "笔记本电脑", 4999, "15寸笔记本电脑"); Goods good4 = new Goods("goods004", "手机", 2300, "android手机"); set.add(good1); set.add(good2); set.add(good3); set.add(good4); System.out.println("商品信息导入成功 !"); } //显示所有商品信息 public void displayAllGoods() { if(set.size()==0) { System.out.println("商品为空,请导入"); }else { Iterator<Goods> it=set.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } } } package com.imooc.goods; /** * 购物车中的商品信息类 * @author ZYH * */ public class GoodsInCart { private Goods goods;//商品信息 private int num;//商品数量 //构造方法 public GoodsInCart() { } public GoodsInCart(Goods goods,int num) { this.setGoods(goods); this.setNum(num); } 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; } } package com.imooc.goods; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; /** * 购物车类 * * @author ZYH * */ public class ShoppingCart { private Map<String, GoodsInCart> shoppingCart;// Map类购物车 // 构造方法 public ShoppingCart() { } public ShoppingCart(Map<String, GoodsInCart> shoppingCart) { this.setShoppingCart(shoppingCart); } public Map<String, GoodsInCart> getShoppingCart() { return shoppingCart; } public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) { this.shoppingCart = shoppingCart; } Map<String, GoodsInCart> shopping = new HashMap<String, GoodsInCart>(); Scanner sc = new Scanner(System.in); /** * 添加商品到购物车 * * @param gm */ public void addGoodsToCart(GoodsManage gm) { Set<Goods> set = gm.getGoods(); boolean flag; Goods goods; String str; if (gm.getGoods().size()==0) { System.out.println("购物车为空!"); } else { gm.displayAllGoods(); for (Goods a : set) { System.out.println("请输入购买的商品编码:"); str =sc.next(); if (a.getGoodsId().equals(str)) { flag = true; goods = a; break; } if (flag = true) { System.out.println("请输入购买数量:"); int n = sc.nextInt(); GoodsInCart go = new GoodsInCart(a, n); shopping.put(str, go); } else { System.out.println("没找到该商品!"); } } } } /** * 修改购物车中的商品数量 */ public void updateNumInCart() { Set<String> set = shopping.keySet(); String s = null; int n = 0; for (String str : set) { System.out.println("请输入要修改的商品编号:"); s = sc.next(); if (str.equals(s)) { System.out.println("请输入要修改的数量:"); n = sc.nextInt(); if (n == 0) { set.remove(str); System.out.println("因为数量为0,故已移除该商品"); } shopping.get(str).setNum(n); } Set<Entry<String, GoodsInCart>> set1 = shopping.entrySet(); for (Entry<String, GoodsInCart> entry : set1) { System.out.println(entry.getValue()); } } } /** * 显示购物车中所有商品 */ public void displayAllInCart() { Set<Entry<String, GoodsInCart>> set = shopping.entrySet(); if(set.size()==0) { System.out.println("购物车空,请补充商品"); } for (Entry<String, GoodsInCart> entry : set) { System.out.println(entry.getValue()); } } /** * 结算 */ public void settleAccounts() { Set<Entry<String, GoodsInCart>> set = shopping.entrySet(); int sum = 0; for (Entry<String, GoodsInCart> s : set) { sum += (s.getValue().getGoods().getPrice() * s.getValue().getNum()); } System.out.println("总金额为:" + sum); set.clear();//清空购物车 } } package com.imooc.goods; /** * 测试类 */ import java.util.Scanner; import java.util.Set; public class TestDemo { public static void main(String[] args) { GoodsManage gm = new GoodsManage(); ShoppingCart SC = new ShoppingCart(); Scanner sc = new Scanner(System.in); boolean flag = true; int n = 0; while (flag) { System.out.println("*********************************"); System.out.println(" 主菜单 "); System.out.println(" 1-商品管理 "); System.out.println(" 2-购物车 "); System.out.println(" 0-退出 "); System.out.println("*********************************"); n = sc.nextInt(); flag = flag; if (n < 0 || n > 2) { System.out.println("输入错误,请重新输入!"); flag = true; continue; }else if(n==0) { System.out.println("***欢迎您下次光临***"); break; }else { switch (n) { case 1: while (true) { System.out.println("*********************************"); System.out.println(" 商品管理"); System.out.println(" 1-商品信息导入 "); System.out.println(" 2-显示所有商品信息 "); System.out.println(" 9-返回上一级菜单 "); System.out.println("*********************************"); int i = sc.nextInt(); if (i != 1 && i != 2 && i != 9) { System.out.println("输入不正确,请重新输入!"); continue; } else { switch (i) { case 1: gm.importGoods(); break; case 2: gm.displayAllGoods(); break; } } if (i == 9) { flag = true; break; } } break; case 2: while (true) { System.out.println("*********************************"); System.out.println(" 购物车管理"); System.out.println(" 1-添加商品到购物车"); System.out.println(" 2-修改购物车中的商品数量"); System.out.println(" 3-显示购物车中的所有商品信息"); System.out.println(" 4-结算"); System.out.println(" 9-返回上一级菜单"); System.out.println("*********************************"); int i = sc.nextInt(); if (i < 1 || i > 4 && i!= 9) { System.out.println("输入不正确,请重新输入!"); continue; }else { switch (i) { case 1: SC.addGoodsToCart(gm); break; case 2: SC.updateNumInCart(); break; case 3: SC.displayAllInCart(); break; case 4: SC.settleAccounts(); break; } } if (i == 9) { flag = true; break; } }break; } } } } }
93
收起
正在回答
2回答
你好,在GoodsManage类中定义了一个private
Set<Goods> goods;
属性集合,又定义了一个Set<Goods> set=
new
HashSet<Goods>();集合属性,这两个保留一个就可以了,建议如果保留前者,要写成private Set<Goods> goods=new HashSet<Goods>();来初始化集合。祝学习愉快~
THappy
2018-01-26 15:51:22
我这里把你的代码修改后可以正常添加商品到购物车了,下面是修改后的代码和正常添加商品的运行截图,为了方便查看效果,展示购物车中的商品的方法也稍作了修改,建议再仔细对照参考一下~
public void addGoodsToCart(GoodsManage gm) { if (gm.getGoods().size() == 0) { System.out.println("未导入商品!"); } else { gm.displayAllGoods(); Iterator<Goods> it = gm.getGoods().iterator(); System.out.println("请输入购买的商品编码:"); String str = sc.next(); boolean flag = true; while (it.hasNext()) { Goods g = it.next(); if (str.equals(g.getGoodsId())) { flag=true; System.out.println("请输入要添加的数量"); int num = sc.nextInt(); GoodsInCart goodsInCart = new GoodsInCart(g, num); shopping.put(str, goodsInCart); break; } else { flag = false; } } if (!flag) { System.out.println("商品编号不存在"); } } } public void displayAllInCart() { Set<Entry<String, GoodsInCart>> set = shopping.entrySet(); if (set.size() == 0) { System.out.println("购物车空,请补充商品"); } for (Entry<String, GoodsInCart> entry : set) {//此处做了修改,因为需要展示购物车中的每个商品信息 GoodsInCart gic = entry.getValue(); System.out.println(gic.getGoods()+"数量:"+gic.getNum()); } }
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星