Java 无法从事务类迭代ItemList
这是我的Java 无法从事务类迭代ItemList,java,Java,这是我的项目课程: 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
项目
课程:
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;
}
}
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;
}
}
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);
}
}
这是我的项目列表
类,它将包含项目的列表
:
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;
}
}
这是我的main
课程:
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;
}
}
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;
}
}
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);
}
}
但我得到:
found java.lang.Object
Required ItemList
或者我必须从事务
类中获取列表
,然后进行迭代吗?您需要
class Transaction implements Iterable<ItemList>
类事务实现Iterable
正如您所说的,它可以由普通对象组成。您能将代码量减少到仅相关位吗?在所有这些课程中,很难说出这里要问什么。当人们看到你努力提出了一个好问题时,他们会感激你。编辑你的代码,这真是一团糟。尊重命名惯例。