无法从Transaction类迭代ItemList

时间:2012-08-31 20:51:47

标签: java

这是我的Item课程:

public class Item {

 private String id;
 private int count;
 private String  name;

public int getcount() {
   return this.count;
}

public Item(String name) {
     this.name=name;
     this.id = "";

 }

public Item(String id, String name) {
   this.name=name;
   this.id=id;
 }

public Item(int count) {
    this.count=count;
}

public String getItemName() {
    return this.name;
}

public String getItemId() {
    return this.id;
}

public Item returnItems(ItemList itemset) {
    Item item=null;
    return item;
  }
}

这是我的ItemList课程,其中包含List items

public  class ItemList implements Iterable<Item>  {

  private List<Item> hold=new ArrayList<Item>();



  ItemList(Item item) {
    this.hold.add(item);
  }

  ItemList() {
    //throw new UnsupportedOperationException("Not yet implemented");
  }


  public List<Item> getItemSet() {
    return this.hold;
  }


  public void addItems(Item item) {
    this.hold.add(item);
  }


  @Override
  public Iterator<Item> iterator() {
    Iterator<Item> item = hold.iterator();
    return item;
  }
}

这是Transaction类:

public class Transaction implements Iterable {

 private List<ItemList> trans=new ArrayList<ItemList>();

 public List<Item> getUniqueItems() {

    Database d = new Database();
    List<Item> unique = new ArrayList<Item>();

    String query="Select id,name from item";
    ResultSet rs = d.sendQuery(query);
    try {
        while(rs.next()) {
           // System.out.println(rs.getString(1)+"\t"+rs.getString(2));
            unique.add(new Item(rs.getString(1),rs.getString(2)));
        }
    }
    catch(Exception e){
        System.out.print(e.getMessage());
    }
    return unique;
}

public ItemList getUniqueItem() {
     ResultSet rs;
     Database d=new Database();
     ItemList unique=new ItemList();
      String query="Select id,name from item";
      rs=d.sendQuery(query);
        try{
        while(rs.next())
         {
           //System.out.println(rs.getString(1)+"\t"+rs.getString(2));
           Item item=new Item(rs.getString(1),rs.getString(2));
           unique.addItems(item);
    }

    } catch(Exception e) {
       System.out.print(e.getMessage());
    }

    return unique;
}


public void addTransaction(ItemList itemList) {
     this.trans.add(itemList);
}

public List<ItemList> getTransaction() {
     return this.trans;
}

@Override
public Iterator<ItemList> iterator() {
    Iterator<ItemList> itemList = trans.iterator();
    return itemList;
}

public int countItems(ItemList itemList) {
      ResultSet rs;
      Database d=new Database();
      String query="";
      int count=0;
      for(Item i:itemList)
     {
       System.out.println(i);
       String id=i.getItemId();
        System.out.print(id+"\t");
        query="SELECT count(*) FROM `item_transaction` where item_id="+id;
          rs=d.sendQuery(query);
          try{
           while(rs.next())
          {
              System.out.print(rs.getString(1));
              count=Integer.parseInt(rs.getString(1));
              System.out.print(count+"\t");
          }

          }catch(Exception e){}
   }

      return count;
 }

 }

这是我的main课程:

public class Ap {

public static void main(String args[])
{
   Transaction t=new Transaction();
    Transaction Ci=new Transaction();
   Transaction Li=new Transaction();

    List<Item> list = t.getUniqueItems();
    for(Item i:list)
    {
        ItemList itemList=new ItemList(i);
        Ci.addTransaction(itemList);
    }

}

itemList添加到Transaction后,我想迭代Ci并将itemList传递给countItems类中的Transaction方法

我试过了:

for(ItemList list:Ci) {
    int x=t.countItems(list);
}

但是我得到了:

found  java.lang.Object
      Required ItemList

或者我是否必须从List<ItemList>班级获取Transaction然后重复?

1 个答案:

答案 0 :(得分:0)

你需要制作

 class Transaction implements Iterable<ItemList>

正如你所说的那样,它是普通物体的可变物。