正在回答
3回答
Set<Goods> goodsSet= gm.getGoods();
for(Goods goods1:goodsSet) {
if(goods1.getGoodsId()==key) {
gm.gd=goods1;
}
}把出错的代码改成这样,在循环外获取到集合,另外集合必须加泛型才行。
最好把GoodsManage类的第一条语句 Set goodsList = new HashSet();也加上泛型 Set<Goods> goodsList = new HashSet<Goods>();
增强型for循环要在集合加上泛型的时候才能用,避免强制类型转换。另外,集合应该提前获取到,而不是在增强型for循环中获取。祝学习愉快!
北极猫_
2017-08-11 09:28:13
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GoodsManage {
Set goodsList = new HashSet();
boolean flag=false;
Goods a = new Goods("goods001","水杯",56d,"不锈钢水杯");
Goods b = new Goods("goods002","饮水机",299d,"带净化功能的饮水机");
Goods c = new Goods("goods003","笔记本电脑",4999d,"15寸笔记本电脑");
Goods d = new Goods("goods004","手机",2300d,"安卓手机");
Goods gd= new Goods();
public GoodsManage() {
}
public Set getGoods() {
return goodsList;
}
public void setGoods(Set goodsList) {
this.goodsList = goodsList;
}
public void importGoods() {
flag=true;
goodsList.add(a);
goodsList.add(b);
goodsList.add(c);
goodsList.add(d);
System.out.println("商品信息导入成功");
}
public void displayAllGoods() {
System.out.println("所有商品信息为:");
Iterator it = goodsList.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;
public class ShoppingCart {
Map<String, GoodsInCart> goodsItem = new HashMap<String,GoodsInCart>();
int num=1;
String key;
Scanner sc = new Scanner(System.in);
public void goodsGD(GoodsManage gm) {
Iterator xx=gm.goodsList.iterator();
while(xx.hasNext()) {
Goods a =(Goods)xx.next();
if(a.getGoodsId().equals(key)) {
gm.gd=a;
}
else {
gm.gd=null;
}
}
}
public void addGoodsToCart(GoodsManage gm) {
System.out.println("添加商品到购物车");
gm.displayAllGoods();
System.out.println("请输入要添加的商品编号");
key=sc.next();
goodsGD(gm);
Iterator it=gm.goodsList.iterator();
while(gm.gd==null) {
System.out.println("请输入正确的商品编号");
key=sc.next();
goodsGD(gm);
}
System.out.println("请输入要添加的商品数量");
num=sc.nextInt();
for(Goods goods1:(goodsList)gm.getGoods()) {
if(goods1.getGoodsId()==key) {
gm.gd=goods1;
}
}
while(num<=0) {
System.out.println("商品数量必须大于0,请重新输入");
num=sc.nextInt();
}
while(it.hasNext()) {
Goods a =(Goods)it.next();
if(a.getGoodsId().equals(key)) {
GoodsInCart value=new GoodsInCart(a,num);
goodsItem.put(key,value);
}
}
displayAllIntCart();
}
public void updateNumIntCart(GoodsManage gm) {
Iterator it=gm.goodsList.iterator();
System.out.println("修改购物车中的商品数量");
System.out.println("请输入要修改的商品编号:");
key=sc.next();
while(!goodsItem.containsKey(key)) {
System.out.println("购物车中无此商品,请输入正确的商品编号");
key=sc.next();
}
System.out.println("请输入该商品新的数量:");
num=sc.nextInt();
while(num<0) {
System.out.println("商品数量不能小于0,请重新输入");
num=sc.nextInt();
}
if(num==0) {
while(it.hasNext()) {
Goods a =(Goods)it.next();
if(a.getGoodsId().equals(key)) {
goodsItem.remove(key);
}
}
System.out.println("商品数量为0,该商品从购物车中移除");
}
else {
while(it.hasNext()) {
Goods a =(Goods)it.next();
if(a.getGoodsId().equals(key)) {
GoodsInCart value=new GoodsInCart(a,num);
goodsItem.put(key,value);
}
}
}
displayAllIntCart();
}
public void displayAllIntCart() {
if(goodsItem.isEmpty()) {
System.out.println("购物车为空,请先添加商品");
}else {
Set<Entry<String, GoodsInCart>> entrySet = goodsItem.entrySet();
for(Entry<String,GoodsInCart> entry:entrySet) {
System.out.println(entry.getValue().toString());
}
}
}
public void settleAccounts() {
System.out.println("结算:");
double sum=0;
Set<Entry<String, GoodsInCart>> entrySet = goodsItem.entrySet();
for(Entry<String,GoodsInCart> entry:entrySet) {
sum=sum+entry.getValue().getNum()*entry.getValue().getGoods().getPrice();
}
displayAllIntCart();
System.out.println("商品的总价为:"+sum);
goodsItem.clear();
}
public ShoppingCart() {
}
public Map<String, GoodsInCart> getGoodsInfo() {
return goodsItem;
}
public void setGoodsInfo(Map<String, GoodsInCart> goodsItem) {
this.goodsItem = goodsItem;
}
}相似问题
登录后可查看更多问答,登录/注册
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星