输出所有商品信息

输出所有商品信息

package imooc.work2;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class GoodsManage {
    Set <Goods>goodsSet=new HashSet<Goods>();
    Iterator<Goods> it=goodsSet.iterator();
    public void imoprtGoods() {
     Goods goods1=new Goods(1000,"耳机",1000,"让声音直接进入耳朵");
     Goods goods2=new Goods(4000,"手机",1001,"人工智能的产物");
     Goods goods3=new Goods(7000,"电视",1002,"广播工具");
     goodsSet.add(goods1);
     goodsSet.add(goods2);
     goodsSet.add(goods3);
     }
    //显示所有的商品
    public void displayAllGoods() {    
       System.out.println("善品信息为:");
       if(it.hasNext()) {
        System.out.println(it.next());
       }
       
     
     }
}

package imooc.work2;

public class Goods {
   private double price;
   private String name;
   private int id;
   private String desp;
  
public Goods(double price, String name, int id, String desp) {
 
 this.price = price;
 this.name = name;
 this.id = id;
 this.desp = desp;
}
public double getPrice() {
 return price;
}
public void setPrice(double price) {
 this.price = price;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public int getId() {
 return id;
}
public void setId(int id) {
 this.id = id;
}
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;
 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.getClass()==this.getClass()) {
    Goods goods=(Goods)obj;
   return goods.name.equals(name)&&goods.price==price&&goods.id==price&&goods.desp.equals(desp);
   }
   return false;
}
public String toString() {
 return "商品名称"+name+"价格"+price+"编号"+id+"属性"+desp;
}
}

package imooc.work2;

public class Test {

 public static void main(String[] args) {
  GoodsManage one=new GoodsManage();
  one.displayAllGoods();

 }

}

为什么只输出了一个"商品信息为"

正在回答

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

2回答

同学你好,if是判断语句,如果it.hasNext()判断集合中有元素存在,输出一条记录就结束,而while()是循环,如果it.hasNext()存在,就会输出,直到it.hasNext()判断集合中为空,就不再输出。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!

好帮手慕阿满 2018-12-28 11:00:33

同学你好,首先同学的商品信息没有导入,所以需要在Test类中先调用importGoods()方法导入商品,还有it迭代器在定义goodsSet属性的下边定义,这时的goodsSet为空,所以不会读取到任何信息,所以应该在displayAllGoods()方法中定义it。另外在使用迭代器的it.next()和hasNext()方法时,每next()一下,他将读取下一条信息,所以使用itnext()不会读取所有的导入的商品信息,同学可以使用for循环来遍历商品并输出。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!

  • 提问者 卡内基先生 #1
    刚刚试了一下,用while(it.hasNext())可以输出,用if就不行,还是想不懂是为什么啊?
    2018-12-28 12:06:21
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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