还是之前的while直接跳出循环的问题,这是测试类的代码跟商品管理类一块
//测试类 public class TestDemo { //商品管理 public void GoodsManage(){ int num=0; do { System.out.println("****************************"); System.out.println(" "+"**商品管理**"); System.out.println(" "+"1--商品信息导入"); System.out.println(" "+"2--显示所有商品讯息"); System.out.println(" "+"9--返回上一级菜单"); System.out.println("****************************"); System.out.println("请输入对应数字进行商品管理:"); GoodsManage gm=new GoodsManage(); Scanner sc=new Scanner(System.in); try{ num=sc.nextInt(); }catch(Exception e){ System.out.println("输入错误,请输入数值型数据"); } switch (num) { case 1: gm.importGoods(); break; case 2: gm.displayAllGoods(); break; case 9: break; } } while (num!=9); } //购物车管理 public void ShoppingCart(){ 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("****************************"); GoodsManage gm=new GoodsManage(); ShoppingCart sCart=new ShoppingCart(); Scanner sc=new Scanner(System.in); int num=0; try{ num=sc.nextInt(); }catch(Exception e){ System.out.println("输入错误,请输入数值型数据"); } switch (num) { case 1: sCart.addGoodsToCart(gm); break; case 2: sCart.updateNumInCart(); break; case 3: sCart.displayAllInCart(); case 4: sCart.settleAccounts(); case 9: break; } } public static void main(String[] args) { System.out.println("****************************"); System.out.println(" "+"**主菜单**"); System.out.println(" "+"1--商品管理"); System.out.println(" "+"2--购物车"); System.out.println(" "+"0--退出"); System.out.println("****************************"); System.out.println("请输入对应的数字进行操作:"); TestDemo td=new TestDemo(); Scanner sc=new Scanner(System.in); int num=0; try{ num=sc.nextInt(); }catch(Exception e){ System.out.println("输入错误,请输入数值型数据"); } switch(num) { case 1: td.GoodsManage(); case 2: td.ShoppingCart(); case 0: break; } } } //商品管理类 private Set<Goods> goodSet=new HashSet<Goods>();// 存储商品的容器 // 构造方法 public GoodsManage() { } // getter和setter方法 public GoodsManage(Set goodSet) { this.goodSet = goodSet; } public Set getGoodSet() { return goodSet; } public void setGoodSet(Set goodSet) { this.goodSet = goodSet; } // 商品信息导入 public void importGoods() { Goods g1 = new Goods("s001", "手机", 132, "android手机"); Goods g2 = new Goods("s002", "水杯", 56, "不锈钢水杯"); Goods g3 = new Goods("s003", "笔记本电脑", 4999, "14寸笔记本电脑"); goodSet.add(g1); goodSet.add(g2); goodSet.add(g3); this.setGoodSet(goodSet); System.out.println("商品讯息导入"); System.out.println("导入成功!!!"); } // 显示所以商品信息 public void displayAllGoods() { System.out.println("显示所有商品信息\n所有商品信息为:"); Iterator<Goods> it =goodSet.iterator(); while (it.hasNext()) { System.out.println(it.next() + " "); } } //商品类 public class Goods { private String id;// 商品编号 private String name;// 商品名字 private double price;// 商品价格 private String desp;// 商品描述 // 构造方法 public Goods() { } public Goods(String id, String name, double price, String desp) { this.id = id; this.name = name; this.price = price; this.desp = desp; } public Goods(String id, String name, double price) { this.id = id; this.name = name; this.price = price; } // getter和setter方法 public String id() { return id; } public void id(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getDesp() { return desp; } public void setDesp(String desp) { this.desp = desp; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((desp == null) ? 0 : desp.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((name == null) ? 0 : name.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 (desp == null) { if (other.desp != null) return false; } else if (!desp.equals(other.desp)) return false; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(price) != Double .doubleToLongBits(other.price)) return false; return true; } // 重写toString public String toString() { return "商品信息" + id + "商品名称" + name + "商品价格" + price + "商品描述" + desp; } } 如果说是没有重写hashCode()跟equals方法,要怎么重写,请详细讲下。
26
收起
正在回答
2回答
do { System.out.println("****************************"); System.out.println(" "+"**商品管理**"); System.out.println(" "+"1--商品信息导入"); System.out.println(" "+"2--显示所有商品讯息"); System.out.println(" "+"9--返回上一级菜单"); System.out.println("****************************"); System.out.println("请输入对应数字进行商品管理:"); GoodsManage gm=new GoodsManage(); Scanner sc=new Scanner(System.in); try{ num=sc.nextInt(); }catch(Exception e){ System.out.println("输入错误,请输入数值型数据"); } switch (num) { case 1: gm.importGoods(); break; case 2: gm.displayAllGoods(); break; case 9: break; } } while (num!=9);
在你的这段代码中,每循环一次就会GoodsManage gm=new GoodsManage();也就是每次循环执行的时候都产生一个新的GoodsManage对象,所以,导入操作进行的数据添加,在查询的时候就无法展示出来,也就是你所说的循环无数据不执行。祝学习愉快~~
qq_寄给你一朵玫瑰_0
2017-10-03 11:11:32
加上上面的这是所以的代码 //购物车中的商品信息类 public class GoodsInCart { private Goods goods;// 商品信息 private int num;// 商品数量 // 构造方法 public GoodsInCart() { } 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 ShoppingCart { private Map<String, GoodsInCart> shoppingCart = new HashMap<String, GoodsInCart>(); // 购物车 // 构造方法 public ShoppingCart() { } public ShoppingCart(Map<String, GoodsInCart> shoppingCart ) { super(); this.shoppingCart = shoppingCart ; } // getter和setter方法 public Map<String, GoodsInCart> gethSoppingCart () { return shoppingCart ; } public void setMap(Map<String, GoodsInCart> shoppingCart ) { this.shoppingCart = shoppingCart ; } GoodsInCart gi=new GoodsInCart(); Scanner sc = new Scanner(System.in); // 添加商品到购物车 public void addGoodsToCart(GoodsManage gm){ System.out.println("添加商品到购物车:"); System.out.println("所有商品信息为:"); Iterator<Goods> it = gm.getGoodSet().iterator(); while (true) { while (it.hasNext()) { System.out.println(it.next()); } try{ System.out.println("请输入要添加的商品编号:"); String ku = sc.next(); GoodsInCart gi = new GoodsInCart(); while (it.hasNext()) { Goods n = it.next(); if (ku.equals(n.id())) { // 将数量导入 gi.setGoods(n); break; } } System.out.println("请输入要添加的商品数量:"); int num=sc.nextInt(); // 将数量导入 gi.setNum(num); // 将物品放入购物车 shoppingCart.put(ku, gi); break; } catch (Exception e) { System.out.println("输入错误,请重新输入!"); continue; } } } // 修改购物车中的商品数量 public void updateNumInCart(){ System.out.println("修改购物车中商品数量"); Iterator<GoodsInCart> it=shoppingCart.values().iterator(); System.out.println("请输入要修改的商品编号:"); String id=sc.next(); while(it.hasNext()) { if (id.equals(gi.getGoods().id())) { it.next(); break; }else{ System.out.println("输入有误"); continue; } } System.out.println("请输入要修改的商品数量:"); int num=sc.nextInt(); gi.setNum(num); if(num==0){ shoppingCart.remove(gi.getGoods().id()); System.out.println("因为商品数量为0,该商品已从购物车中移除!"); } shoppingCart.put(id, gi); } //显示购物车中的所以商品 public void displayAllInCart() { System.out.println("显示购物车中所有商品信息"); Iterator<GoodsInCart> it=shoppingCart.values().iterator(); while(it.hasNext()){ System.out.println( it.next()+" "); } } // 结算 public void settleAccounts() { System.out.println("结算"); double sum=0; Iterator<GoodsInCart> it=shoppingCart.values().iterator(); while(it.hasNext()){ GoodsInCart gs=it.next(); sum=sum+gs.getNum()*gs.getGoods().getPrice(); shoppingCart.put(gi.getGoods().id(), gi); } System.out.println("商品的总价为: "+ sum); while (it.hasNext()) { gi=it.next(); System.out.println("商品名称: "+gi.getGoods().getName()+"商品价格:"+gi.getGoods().getPrice()+ "商品描述:"+gi.getGoods().getDesp()+"商品数量:"+gi.getNum()); } } }
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星