view raw
Peebl Peebl - 7 months ago 34
Java Question

Easy way to add 3 different types of Objects to one ArrayList with one method?

This is a copy from an assignment I've done, so apologizes for some of the Norwegian commenting.

My question is:

I have an items ArrayList which holds Objects from the classes Item, Swords, Potions. (Potions & Swords are subclasses of Items). And I have 3 different methods to add these Objects to the ArrayList.

My question is Simple actually. Is there a (simple, I'm new and learning still) way for me to add all Items and Subclasses of Item to the ArrayList without having 3 different methods which look all alike. (addItem, addSword, addPotion), all close to the bottom of the code.

I'd want something like this ( not thought through, just wrote something)

public void addItem(String typeofItem){
item = new typeofItem;

There is alot more to the code, but i feel like this is whats relevant.

public class Player
// Fields -------
private String name;
private String type;
private int health;
private ArrayList<Item> items = new ArrayList<>();
private Item item;
private Swords sword;
private Potions potion;
private Checker valid;
private int maxCarry;
private int gold;
private int currentWeight;
// ----------------

* Klassens konstruktør. Players health vil alltid settes til 100.
* @param name Player navn
* @param type Player type
* @param goldValue Players starting gold
* @param setMaxWeight Players max carry weight
public Player (String name, String type, int goldValue, int setMaxWeight) {
valid = new Checker(); // Creates a checker object to do String and integer checks = valid.checkString(name); // Setter player name, etter å ha sjekka at den ikke er tom
this.type = checkType(type); // Setter type, etter å ha processet den
health = 100; // Health skal alltid starte som 100
maxCarry = valid.checkInt(setMaxWeight); // Sets max carry weight
currentWeight = 0; // Start vekten til player
gold = valid.checkInt(goldValue); // setter goldbeholding etter å ha sjekka at den ikke er negativ

* En metode for å selge ett Item objekt, reduserer gold og øker weight ved kjøp.
* @param item Item object to sell
private void buyItem(Item item)
// Temporary values given from items methods getweight and getvalue to be used in a mutation
int tempWeight;
int tempValue;
tempWeight = item.getWeight();
tempValue = item.getValue();
// Checks if the item meets te conditions to be bought (enough gold, and can be carried)
if (checkConditions(tempValue, tempWeight)) {
// Adds the item to ArrayList if conditions met and updates gold and weight
currentWeight += tempWeight;
gold -= tempValue;

* Method to calculate if given value and weight is accepted to perform a purchase (total gold > 0 && total weight < maxWeight)
* @param value Gold value of item
* @param weight Weight of item
* @return boolean Returns true if conditions met
private boolean checkConditions(int value, int weight)
if (!(gold >= value))
System.out.println("You don't have enough gold!");
return false;
} else {
if (!(weight + currentWeight <= maxCarry)){
System.out.println("You'll be too heavy carry this item!");
return false;
} else {
// All conditions met
return true;

* Adds an item to player inventory (uses method buyItem to process the item as a purchase)
* @param itemName String to name item
* @param itemDesc String to describe item
* @param itemValue int to declare items value
* @param itemWeight int to declare items Weight
* @param itemAction String to give item an action
public void addItem(String itemName, String itemDesc, int itemValue, int itemWeight, String itemAction)
// A Player generated item, which needs all parameters to specify it
item = new Item(itemName, itemDesc, itemValue, itemWeight, itemAction);

* Randomly generates a Sword object and adds it to players inventory (uses buyItem to process)
* @see Swords#Swords
public void addSword()
sword = new Swords();

* Randomly generates a Potion object and adds it to players inventory (uses buyItem to process)
* @see Potions#Potions
public void addPotion()
potion = new Potions();


You can create something called a Factory class. A factory class's responsibility is to generate instances of a type of item for you.

Essentially you wrap the method you talk about in a class and return a new instance of that object.

public class ItemFactory{

    public static Item addItem(String typeofItem){
             case "Sword":
                return new Sword();
             case "Potion":
                return new Potion();
               //put any code here that is like the "else" of an if-else block
              return null;


Then when you need to add a new item of a specific type: