添加商品到购物车程序终止了,请老师解答一下
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; /** * 购物车类 * @author xiaod * */ public class ShoppingCart { //属性 Map<String,GoodsInCart> shoppingCart=new HashMap<String,GoodsInCart>(); //构造方法 public void 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; } //添加商品到购物车 public void addGoodsToCart(GoodsManage gm,Set<Goods>goodsSet) { if(gm.getGoodsSet().size()==0) { System.out.println("请先导入商品信息!"); }else { System.out.println("添加商品到购物车:"); System.out.println("所有商品信息为:"); gm.displayAllGoods(goodsSet); System.out.println("请输入要添加的商品编号:"); Scanner sc=new Scanner(System.in); String goodsId=sc.next(); Iterator<Goods> it=gm.getGoodsSet().iterator(); boolean flag=false; Goods goods=null; while(it.hasNext()) { goods=it.next(); if(goods.getGoodsId().equals(goodsId)) { flag=true; break; } } if(flag) { System.out.println("请输入要添加的商品数量:"); int num=sc.nextInt(); if(num!=0) { GoodsInCart gic=new GoodsInCart(goods,num); shoppingCart.put(goodsId, gic); }else { System.out.println("商品数量不为零!"); } }else { System.out.println("请输入正确的商品编号!"); } } } //修改购物车中商品的数量 public void updateNumInCart() { if(shoppingCart.size()==0) { System.out.println("购物车中暂无商品信息,请先添加商品!"); }else { System.out.println("修改购物车中商品的数量"); System.out.println("请输入要修改商品的编号"); Scanner sc=new Scanner(System.in); String goodsId=sc.next(); Iterator<GoodsInCart> itGoodsInCart=shoppingCart.values().iterator(); boolean flag=false; Goods goods=null; while(itGoodsInCart.hasNext()) { goods=itGoodsInCart.next().getGoods(); if(goods.getGoodsId().equals(goodsId)) { flag=true; break; } } } } //显示购物车中的所有商品 public void displayAllInCart(){ if(shoppingCart.values().size()==0) { System.out.println("购物车中暂无商品信息,请先添加商品!"); }else { System.out.println("显示购物车中的所有商品信息"); Iterator<GoodsInCart> itGoodsInCart = shoppingCart.values().iterator(); while(itGoodsInCart.hasNext()) { System.out.println(itGoodsInCart.next()); } } } //结算 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.getGoodsId()+",名称:"+g.getGoodsName()+",价格:" +g.getPrice()+",商品描述:"+g.getGoodsDesp()+",数量:"+gic.getNum()+"]"); double sum1=g.getPrice()*gic.getNum(); sum=sum+sum1; } } }
54
收起
正在回答 回答被采纳积分+1
9回答
一叶知秋519
2017-10-22 00:04:55
在addGoodsToCart方法中,对商品库是否为空的判断改为goodsSet.size()==0;将测试类中manageGoods方法中的GoodsManage类的对象去掉,保证前后操作的是同一个GoodsManage;另外你的测试类中,manageShoppingCart方法依旧和manageGoods方法一样,使用递归调用来保证程序的连贯性;即
switch(num) { case 1: // goodsManage.displayAllGoods(goodsSet); shoppingCart.addGoodsToCart(goodsManage,goodsSet); this.manageShoppingCart(); break; case 2: shoppingCart.updateNumInCart(); this.manageShoppingCart(); break; case 3: shoppingCart.displayAllInCart(); this.manageShoppingCart(); break; case 4: shoppingCart.settleAccounts(); this.manageShoppingCart(); break; case 9: mainMenu(); break; }
祝学习愉快~
无厘头的童年
2017-10-21 23:36:38
public class Goods { private String goodsId; //商品编号 private String goodsName;//商品名称 private double price; //商品价格 private String goodsDesp;//商品描述 //构造方法 public Goods(String goodsId, String goodsName, double price, String goodsDesp) { super(); this.goodsId = goodsId; this.goodsName = goodsName; this.price = price; this.goodsDesp = goodsDesp; } //getter和setter方法 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; } //hashCode()方法 @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; } //equals()方法 @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 (goodsDesp == null) { if (other.goodsDesp != null) return false; } else if (!goodsDesp.equals(other.goodsDesp)) return false; if (goodsId == null) { if (other.goodsId != null) return false; } else if (!goodsId.equals(other.goodsId)) 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 "商品信息 [编号:" + goodsId + ", 名称:" + goodsName + ", 价格:" + price + ", 描述:" + goodsDesp + "]"; } }
无厘头的童年
2017-10-21 23:36:03
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; } }
无厘头的童年
2017-10-21 23:35:24
import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * 测试类 * @author xiaod * */ public class TestDemo { private Set<Goods> goodsSet=new HashSet<Goods>(); private ShoppingCart shoppingCart=new ShoppingCart(); //初始化购物车 GoodsManage goodsManage=new GoodsManage(); //商品管理 public void manageGoods() { System.out.println("=============================================="); System.out.println("**商品管理**"); System.out.println("1--商品信息导入"); System.out.println("2--显示所有商品信息"); System.out.println("9--返回上一级菜单"); System.out.println("=============================================="); System.out.println("请输入对应的数字对商品进行管理:"); Scanner src = new Scanner(System.in); int num = src.nextInt(); GoodsManage goodsManage=new GoodsManage(); //根据商品编号执行操作 switch(num) { case 1: goodsManage.importGoods(goodsSet); this.manageGoods(); break; case 2: goodsManage.displayAllGoods(goodsSet); this.manageGoods(); break; case 9: mainMenu(); break; } } //购物车管理 public void manageShoppingCart() { 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("=============================================="); System.out.println("请输入对应的数字对购物车进行管理"); Scanner src=new Scanner(System.in); int num=src.nextInt(); //根据编号执行操作 switch(num) { case 1: goodsManage.displayAllGoods(goodsSet); shoppingCart.addGoodsToCart(goodsManage,goodsSet); break; case 2: shoppingCart.updateNumInCart(); break; case 3: shoppingCart.displayAllInCart(); break; case 4: shoppingCart.settleAccounts(); break; case 9: mainMenu(); break; } } //主菜单方法 public void mainMenu() { //开始进入点 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: manageGoods(); break; case 2: manageShoppingCart(); break; case 0: break; } } public static void main(String[] args) { TestDemo demo=new TestDemo(); demo.mainMenu(); } }
无厘头的童年
2017-10-20 19:35:44
import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * 商品管理类 * @author xiaod * */ 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(Set<Goods> goodsSet) { 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, "不锈钢水杯")); System.out.println("商品信息导入"); System.out.println("导入成功!"); } //显示所有商品信息 public void displayAllGoods(Set<Goods> goodsSet) { System.out.println("显示所有商品信息\n所有商品信息为:"); Iterator it=goodsSet.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
请老师看一下
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星