为什么输入商品编号以后显示找不到商品
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 |
public class Goods { private String goodsId;//商品编号 private String goodsName;//商品名称 private double price;//商品价格 private String goodsDesp;//商品描述 //Goods类的构造方法 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() equals()方法
@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 == null) return false;
Goods other = (Goods) obj; if (goodsId == null) { if (other.goodsId != null) return false; } else if (!goodsId.equals(other.goodsId)) return false; return true; }
//重写toString方法 @Override public String toString() { return "[商品编号:" + goodsId + ",商品名称:" + goodsName + ",商品价格:" + price + "商品描述:" + goodsDesp + "]"; } } public class GoodsIntCart { private Goods goods; private int num; //构造方法 public GoodsIntCart(Goods goods, int num) { this.setGoods(goods); this.setNum(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; } @Override public String toString() { return goods+",数量:"+num; }
} import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class GoodsManage { Set<Goods> goodsSet=new HashSet<Goods>(); //构造方法 public GoodsManage() {
} //getter setter方法
public Set<Goods> getGoodsSet() { return goodsSet; } public void setGoodsSet(Set<Goods> goodsSet) { this.goodsSet = goodsSet; } //商品信息导入 public void importGoods() { Goods goods1=new Goods("s01", "键盘", 1299, ""); Goods goods2=new Goods("s02", "鼠标", 20, ""); Goods goods3=new Goods("s03", "洗衣粉", 50, ""); Goods goods4=new Goods("s04", "苹果", 1, ""); goodsSet.add(goods1); goodsSet.add(goods2); goodsSet.add(goods3); goodsSet.add(goods4); } //显示所有商品信息 public void displayAllGoods(GoodsManage use) {
use.importGoods(); Iterator<Goods> gmit=goodsSet.iterator(); while(gmit.hasNext()) { System.out.println(gmit.next()); }
}
} import java.io.ObjectOutputStream.PutField; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class ShoppingCart { Goods goods; GoodsIntCart gis; String str; int num; Map<String, GoodsIntCart> mapcart=new HashMap<String,GoodsIntCart>(); //添加商品到购物车 public void addGoodsToCart(GoodsManage gm) { gm.displayAllGoods(gm); gm.setGoodsSet(gm.goodsSet); if(mapcart.containsKey(str)) { System.out.println("商品已在购物车中,不可重复添加"); //break; }else if(gm.getGoodsSet().contains(this.getStr())) { gis= new GoodsIntCart(goods, num); System.out.println("添加成功~"); mapcart.put(this.str, gis); for (Goods g : gm.getGoodsSet()) { if (g.getGoodsId().equals("s01")) { goods = g; break; } } }else if(!gm.goodsSet.contains(this.getStr())) { System.out.println("你输入的商品编号不存在"); } //break; }
//修改购物车中的商品数量 public void updateNumInCart() {
} //显示购物车中的所有商品 public void displayAllInCart() { Set<Entry<String, GoodsIntCart>> e=mapcart.entrySet(); for(Entry<String, GoodsIntCart> ey:e) {
System.out.println(ey.getValue().getGoods()); } } //结算 public void settleAccounts() {
} //构造方法 public ShoppingCart() {
} //setter getter方法 public Map<String, GoodsIntCart> getMapcart() { return mapcart; } public void setMapcart(Map<String, GoodsIntCart> mapcart) { this.mapcart = mapcart; } public String getStr() { return str; } public void setStr(String str) { this.str=str; } public int getNum() { return num; } public void setNum(int num) { this.num = num; }
} import java.util.Scanner; public class TestDemo { //主菜单 public void menu() { System.out.println("********************* "); System.out.println("* **主菜单** * "); System.out.println("* 1--商品管理 *"); System.out.println("* 2--购物车 *"); System.out.println("* 0--退出 *"); System.out.println("**********************"); } //商品管理菜单 public void manage() { System.out.println("***********************"); System.out.println(" **商品管理** "); System.out.println(" 1--商品信息导入 "); System.out.println(" 2--显示所有商品信息 "); System.out.println(" 9--返回上一级菜单 "); System.out.println("***********************"); } //购物车管理菜单 public void cartManage() { 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("********************************"); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); TestDemo menuuse=new TestDemo(); Goods goods=null; ShoppingCart spc=new ShoppingCart(); //1--商品信息导入 GoodsManage gmuse=new GoodsManage(); //2--显示所有商品信息 gmuse.displayAllGoods(gmuse); //添加商品到购物车 System.out.println("请输入商品编号:"); spc.setStr(sc.next()); System.out.println("请输入商品数量:"); spc.setNum(sc.nextInt()); spc.addGoodsToCart(gmuse); //3--显示购物车中所有商品的信息 //spc.displayAllInCart();
} }
|
正在回答
addGoodsToCart 在这个方法中,你是通过
if (mapcart.containsKey(str)) { System.out.println("商品已在购物车中,不可重复添加"); // break; } else if (gm.getGoodsSet().contains(this.getStr())) { gis = new GoodsIntCart(goods, num); System.out.println("添加成功~"); mapcart.put(this.str, gis); for (Goods g : gm.getGoodsSet()) { if (g.getGoodsId().equals("s01")) { goods = g; break; } } } else if (!gm.goodsSet.contains(this.getStr())) { System.out.println("你输入的商品编号不存在"); }
进行商品是否存在的判断的,所以,在输入的商品不是列表中的第一个时,就会结束判断,输出商品不存在,此处应该通过循环遍历所有商品,直到最后一个,才能真正确定商品是否存在。
另外,“另外加上数量时,要怎么显示,”不明白你想表述的意思,是在哪里添加?
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星