为什么导入商品信息成功,但是添加商品到购物车里时GoodsSet是空的???
package com.work; //商品类 public class Goods { private String goodsId; private String goodsName; private double price; private String goodsDesp; Goods(){ } Goods(String goodsId,String goodsName,double price,String goodsDesp){ super(); this.goodsId=goodsId; this.goodsName=goodsName; this.price=price; this.goodsDesp=goodsDesp; } public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsTd) { this.goodsId = goodsTd; } 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().equals(Goods.class)) { Goods goods=new Goods(); return goods.getGoodsId().equals(goodsId)&&goods.getGoodsName().equals(goodsName)&& goods.getPrice()==price&&goods.getGoodsDesp().equals(goodsDesp);} return true; } @Override public String toString() { return "商品: [商品编号=" + goodsId + ",商品名称 =" + goodsName + ", 商品价格=" + price + ",商品描述=" + goodsDesp + "]"; } } package com.work; import java.util.HashSet; import java.util.Iterator; //商品管理类 import java.util.Set; public class GoodsManage { private Set GoodsSet=new HashSet<Goods>();; GoodsManage(){ } public Set getGoodsSet() { return GoodsSet; } public void setGoodsSet(Set goodsSet) { GoodsSet = goodsSet; } //商品信息导入方法 public void importGoods() { Goods gd1=new Goods("goods001","水杯",56,"不锈钢杯子"); Goods gd2=new Goods("goods002","饮水机",299,"带净化功能的饮水机"); Goods gd3=new Goods("goods003","笔记本电脑",4999,"15寸笔记本电脑"); Goods gd4=new Goods("goods004","手机",2300,"android手机"); GoodsSet.add(gd1); GoodsSet.add(gd2); GoodsSet.add(gd3); GoodsSet.add(gd4); System.out.println("导入成功!!!"); } //显示商品名称 public void displayAllGoods() { Iterator GS=GoodsSet.iterator(); for(Object str:GoodsSet) { System.out.println(str); } } }
package com.work;
//购物车中的商品信息
public class GoodsInCart {
private Goods goods;
private int num;
GoodsInCart(){
}
GoodsInCart(Goods goods,int num){
super();
this.goods=goods;
this.num=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.work;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
//购物车类
public class ShoppingCart {
private Map<String, GoodsInCart> shoppingCart =new HashMap();
public void addGoodsToCart(GoodsManage gm) { // 添加商品到购物车
Set<Goods> goods =gm.getGoodsSet();
Scanner sc = new Scanner(System.in);
String id = null;
Goods gd1=null;
boolean flag = false;
String id1 = null;
if (goods.size() == 0) {
System.out.println("还没有商品记得导入商品信息哦!!!");
} else {
gm.displayAllGoods();
System.out.println("请输入商品编号");
id = sc.next();
for (Goods l : goods) {
if (l.getGoodsId().equals(id))
gd1=l;
id1=id;
flag=true;
}
}
if(flag==true) {
System.out.println("请输入商品数量");
int num1=sc.nextInt();
GoodsInCart val=new GoodsInCart(gd1,num1);
shoppingCart.put(id, val);
}
}
// 修改购物车的商品数量
public void updateNumInCart() {
Set<String> set=shoppingCart.keySet();
Scanner sc=new Scanner(System.in);
System.out.println("请输入商品的编号:");
String s=sc.next();
boolean flag=set.contains(s);
if(flag) {
System.out.println("请输入修改的数量:");
int shuliang=sc.nextInt();
if(shuliang==0) {
set.remove(s);
System.out.println("因为商品数量为0,已经移除了该商品");
}
else {
shoppingCart.get(s).setNum(shuliang);
}
}
else {
System.out.println("不存在此商品的id!!!");
}
Set<Entry<String,GoodsInCart>> set1=shoppingCart.entrySet();
for(Entry<String,GoodsInCart> entry: set1) {
System.out.println(entry.getValue());
}
}
// 显示购物车商品
public void displayAllInCart() {
Set<Entry<String,GoodsInCart>> set1=shoppingCart.entrySet();
if(set1.size()==0) {
System.out.println("购物车的商品是空滴,快去添加商品吧!!!");
}
else {
for(Entry<String,GoodsInCart> entry:set1) {
System.out.println(entry.getValue().getGoods()+","+entry.getValue().getNum()+"个");
}
}
}
// 结算总金额
public void SettleAccounts() {
double sum=0;
Set<Entry<String,GoodsInCart>> set1=shoppingCart.entrySet();
for(Entry<String,GoodsInCart> entry: set1) {
sum=sum+entry.getValue().getGoods().getPrice()*entry.getValue().getNum();
}
System.out.println("您的结算金额为:"+sum);
shoppingCart.clear();
}
}
package com.work;
import java.util.Scanner;
//测试类
public class TestDemo {
static GoodsManage gm = new GoodsManage();
public void mainInfo() {
System.out.println("***********************");
System.out.println(" **主菜单** ");
System.out.println(" 1--商品管理 ");
System.out.println(" 2--购物车 ");
System.out.println(" 0--退出 ");
System.out.println("***********************");
System.out.println("请输入对应的数字进行操作:");
}
public void info() {
System.out.println("**********************************");
System.out.println(" **商品管理** ");
System.out.println(" 1--商品信息导入 ");
System.out.println(" 2--显示所有商品信息 ");
System.out.println(" 9--返回上一级主菜单 ");
System.out.println("**********************************");
System.out.println("请输入对应的数字对商品进行管理:");
}
public void shoppinginfo() {
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("请输入对应的数字对购物车进行管理:");
}
public static void main(String[] args) {
boolean flag = true;
TestDemo td = new TestDemo();
while (flag) {
Scanner sc = new Scanner(System.in);
td.mainInfo();
int mainsc = sc.nextInt();
// 退出购物车管理系统
if (mainsc == 0) {
flag = false;
System.out.println("结束!!!");
}
// 进入商品管理
if (mainsc == 1) {
GoodsManage gm = new GoodsManage();
boolean flag1 = true;
while (flag1) {
td.info();
int cr=sc.nextInt();
if (cr == 1) {
gm.importGoods(); //导入商品信息
continue;
}
if (cr == 2) {
gm.displayAllGoods(); //显示商品信息
continue;
}
if (cr == 9) {
flag1 = false;
continue;
}
}
}
// 进入购物车管理
if (mainsc == 2) {
ShoppingCart Sc=new ShoppingCart();
boolean flag1=true;
while(flag1) {
td.shoppinginfo(); //添加商品到购物车
int cr=sc.nextInt();
if(cr==1) {
Sc.addGoodsToCart(gm);
continue;
}
if(cr==2) {
Sc.updateNumInCart();//修改商品数量
continue;
}
if(cr==3) {
Sc.displayAllInCart();
continue;
}
if(cr==4) {
Sc.SettleAccounts();
continue;
}
if(cr==9) {
flag1=false;
continue;
}
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星