gpt4 book ai didi

java - 项目寄存器迭代器

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:37 24 4
gpt4 key购买 nike

我正在尝试创建一个项目寄存器来注册房间内的项目。

在房间类(class)中,我有一个

  /**
* Gets item description
*/
public ArrayList<Item> getItem()
{
return items;
}

在项目寄存器中我目前有

public class ItemRegister
{
private HashMap<String,Item> items;
/**
* Constructor for objects of class ItemRegister
*/
public ItemRegister()
{
items = new HashMap<String,Item>();

}
/**
* register items in hashMap
*/
public void register(Item a)
{
items.put(a.getDescription(),a);


}

public void register(ArrayList<Item> a)
{
Iterator<Item> i = a.iterator();
while (i.hasNext())
{
Room room = i.next();
if (room.getItem() != null && room.getItem().equals(a))
{

}
else{

}
}

}
}

我正在尝试完成迭代器以循环房间,如果它在房间内找到一个项目,它将通过将其放入 hashMap 来注册它

最佳答案

希望这足以让您继续前进:

import java.util.*;

public class ItemRegister{

public static void main(String []args){
ItemRegister ir = new ItemRegister();
}

// storing items registered here
private HashMap<String,Item> items;

/**
* Constructor for objects of class ItemRegister
*/
public ItemRegister(){
items = new HashMap<String,Item>();

ArrayList<Room> roomList = new ArrayList<>();
roomList.add( new Room( new Item("Item A") ) );
roomList.add( new Room( new Item("Item B") ) );
roomList.add( new Room( new Item("Item C") ) );
roomList.add( new Room( new Item("Item D") ) );


System.out.println( "Size of Registered Items:" + items.size() );

// Register only A item
Item aItem = new Item("Item A");
register( roomList, aItem );

/// print whatever is in the register
System.out.println( "Size of Registered Items:" + items.size() );
System.out.println( "Registered Items:" );

for( Item item : items.values() ){
System.out.println( item.getDescription() );
}
System.out.println( "---- end ----" );

// Register B item too
Item bItem = new Item("Item B");
register( roomList, bItem );

/// print whatever is in the register
System.out.println( "Size of Registered Items:" + items.size() );
System.out.println( "Registered Items:" );

for( Item item : items.values() ){
System.out.println( item.getDescription() );
}
System.out.println( "---- end ----" );
}

/**
* register items in hashMap
*/
public void register(Item a){
items.put(a.getDescription(), a);
System.out.println( "Registered an Item" );
}

public void register(ArrayList<Room> roomList, Item a){
Iterator i = roomList.iterator();

while (i.hasNext()) {
Room room = (Room)i.next();

if (room.getItem() != null && room.getItem().equals(a)){
register( room.getItem() );
}
}
}

/* class Item */
protected class Item{
private String description;

Item(String description){
this.description = description;
}

public String getDescription(){
return this.description;
}

public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Item)) {
return false;
}

Item item = (Item) o;

if (!description.equals(item.description)) {
return false;
}

return true;
}
}

/* class Room */
protected class Room{
private Item item;

Room( Item item ){
this.item = item;
}

public Item getItem(){
return this.item;
}
}
}

关于java - 项目寄存器迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com