为什么GoodsManage不能用增强型for循环?

为什么GoodsManage不能用增强型for循环?

http://img1.sycdn.imooc.com/climg//598c7b550001563006600160.jpg

http://img1.sycdn.imooc.com/climg//598c7b5500015d7205680439.jpg

一直报错,强制类型转换也不行

正在回答

登陆购买课程后可参与讨论,去登陆

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循环中获取。祝学习愉快!

  • 北极猫_ 提问者 #1
    太感谢了!
    2017-08-11 14:06:04
提问者 北极猫_ 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;
	}
	
	
}


好帮手慕珊 2017-08-11 09:21:10

你可以把完整的代码贴一下,不截图,直接贴代码,这样看不出来问题。你也可以自己确认一下getGoods()方法的返回值是什么,是否是HashSet类型。

  • 提问者 北极猫_ #1
    相关联的代码贴好了.getGoods()返回的是goodsList
    2017-08-11 09:29:17
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师