Reputation: 212
I have an inventory class that has an array of the class 'Item', called items. Within the Item class, I have an int called quantity to represent how many of the item that you get with each pickup. I haven't been able to figure out how to get the number of duplicate items within the array, so that I can multiply that by the getQuantity() method for that item and get the total quantity of the item that the player is carrying.
public class Inventory {
private Item[] items; //A private Item array, called items
private int firstFree;
private int quantity;
private WorldRenderer world;
/**
* CREATES THE INVENTORY AT A SIZE SPECIFIED IN THE PARATHENESIS
*/
public Inventory(int size) {
items = new Item[size];
firstFree = 0;
}
public int getItemCount() {
for (int i = firstFree; i < items.length; i++) {
if (items[i] != null) {
return items.length;
}
}
return 0;
}
public boolean add(Item item) {
if (firstFree == items.length) {
return false;
}
items[firstFree] = item;
for (int i = firstFree; i < items.length; i++)
if (items[i] == null) {
firstFree = i;
return true;
}
firstFree = items.length;
return true;
/**for (int i = 0; i < items.length; i++)
if (items[i] == null){
items[i] = item;
System.out.println("Item " + item.getName() + " added to inventory at index " + i); // TESTING
return true;
}
return false;
}**/
}
public Item get(int index) {
return items[index];
}
public void setQuantity(Item item, int quantity) {
for (int i = 0; i < items.length; i++) {
if (items[i] == item) {
items[i].setQuantity(quantity);
}
}
}
public void removeQuantity(Item item, int quantity) {
for (int i = 0; i < items.length; i++) {
if (items[i] == item) {
items[i].setQuantity(item.getQuantity() - quantity);
}
}
}
public int getQuantity(Item item) {
int quantity = 0;
for (int i = 0; i < items.length; i++) {
if (items[i] == item) {
quantity = items[i].getQuantity();
}
}
return quantity;
}
}
Perhaps there is a better way to go about creating an inventory for my particular problem?
EDIT: Trying the HashMap and getting an NPE at this line
if (items.containsKey(item)){
Integer previousQuantity = items.get(items);
items.put(item, ++previousQuantity); // NPE this line.
} else {
items.put(item, 1);
}
Upvotes: 0
Views: 247
Reputation: 20179
Rather than Item[] items
you might consider HashMap<Item, Integer> items
where the Integer refers to quantity.
This would make sure that there are no duplicate Items as well as simplify finding the quantity.
Example:
import java.util.HashMap;
public class Inventory {
private HashMap<Item, Integer> items;
private int maxSize;
public Inventory(int maxSize) {
this.maxSize = maxSize;
items = new HashMap<Item, Integer>();
}
public int getItemCount() {
int count = 0;
for (Item item: items.keySet()){
count += items.get(item);
}
return count;
}
public boolean add(Item item) {
if (items.size() >= maxSize){
return false;
}
if (items.containsKey(item)){
Integer previousQuantity = items.get(items);
items.put(item, ++previousQuantity);
} else {
items.put(item, 1);
}
return true;
}
}
Generic Implementation of equals() and hashCode():
public class Item {
int itemType;
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
Item item = (Item) o;
if (itemType != item.itemType) { return false; }
return true;
}
@Override
public int hashCode() {
return itemType;
}
}
Upvotes: 3
Reputation: 775
You probably need a unique ID to differentiate between items in you inventory, you can then put that in a Map which can just take care of counting duplicates for you . In your case the name of the item can be the unique identifier (Which would be the key in the Map) .
Upvotes: 0
Reputation: 11085
you can find out number of unique item by using Set. In Set no item is repeated. So if you can convert your array to set then you can find the number of unique item. And you can get the number of repeated item by subtracting from the total item. to convet array to Set you have to do
Set<Integer> uniqueItem = new HashSet<Integer>(Arrays.asList(items));
int total_repeated_item=items.length-uniqueItem.size();
Upvotes: 0