简体   繁体   中英

how to add/remove multiples of objects from an array list

I am trying to build an ArrayList that will contain objects. when i add an object to the list i want it to first check the array list for that object. and if it finds it i want it to increase a quantity variable in that object and not create a new object in the list. and then vice versa when removing objects. I have accomplished a way that works when removing an object. But i dont think i fully understand the methods in the arraylist or the logic when creating and arraylist of objects. as when i use .contains or .equals im not getting the desired effect.

public class ItemBag {


    private ArrayList<Item> inventory = new ArrayList<Item>();


    public ItemBag() {
    }

    public void addItem(Item objName, int quantity) {
        if (inventory.contains(objName)) {
            System.out.println("if statement is true!");
            int i = inventory.indexOf(objName);
            inventory.get(i).setQuantity(inventory.get(i).getQuantity() + quantity);
        } else {
            inventory.add(objName);
            objName.setQuantity(quantity);
        }
    }

    public void removeItems(String itemName, int quantiy) {
        for (int i = 0; i < inventory.size(); i++) {
            if (inventory.get(i).name() == itemName) {
                inventory.get(i).setQuantity(inventory.get(i).getQuantity() - quantiy);
                if (inventory.get(i).getQuantity() <= 0) {
                    inventory.remove(inventory.get(i));
                }

            }
        }
    }


    public void showInventory() {
        for (int i = 0; i < inventory.size(); i++) {
            System.out.println(inventory.get(i).name() + " : " + inventory.get(i).getQuantity());
        }

    }

then when creating the itemBag in another object i am writing

ItemBag merchantItems = new ItemBag();

 public void merchantBob() {
        merchantItems.addItem(new HealthPotion() ,3);
        merchantItems.showInventory();
        System.out.println("add 1");
        merchantItems.addItem(new HealthPotion(),1);
        merchantItems.showInventory();

Items class

package Items;


public abstract class Item {

    private int quantity = 0;

    public Item() {
    }

    public abstract String name();

    public abstract int cost();

    public abstract String type();

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

HealthPotion Class


public class HealthPotion extends Potions {

    protected int addHealth = 10;


    @Override
    public int drinkPotion() {
        return addHealth;
    }


    @Override
    public String name() {
        return "Health Potion";
    }

    @Override
    public int cost() {
        return 5;
    }

    @Override
    public String type() {
        return "Potion";
    }


}

The .contains() method would iterate through the list and use .equals() method to compare each element and check if the provided object exists in the list.

.equals() method would compare the object reference (unless .equals() is overridden) to check if the objects are same.

For reference: https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#contains-java.lang.Object-

You can override the .equals() method to compare the values of the provided object in the following way:

public abstract class Item {

    private int quantity = 0;

    public Item() {
    }

    public abstract String name();

    public abstract int cost();

    public abstract String type();

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;

        Item providedItem = (Item) object;
        return name == providedItem.name 
                && cost == providedItem.cost
                && type == providedItem.type;
    }

}

This should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM