-1

I'm having issues removing class objects from an arraylist in a class.

package more_java;

import java.util.ArrayList;

public class PiggyBank {
    private static ArrayList<Coin> coins = new ArrayList<Coin>();
    
    public PiggyBank(ArrayList<Coin> coins) {
        PiggyBank.coins = coins;
    }
    
    public static void addNickel(){coins.add(new Nickel());}
    public static void addDime(){coins.add(new Dime());}
    public static void addQuarter(){coins.add(new Quarter());}
    public static void addLoonie(){coins.add(new Loonie());}
    public static void addToonie(){coins.add(new Toonie());}
    
    public static void removeNickel() {coins.remove(Nickel());}
    public static void removeDime(){coins.remove(Dime());}
}

Nickel, Dime, Quarter, Loonie, and Toonie are all class objects made as extensions of an abstract class Coin. Each of these coins contain nothing but an accessor method GetValue() which simply returns the value of each coin. The problem comes from the remove methods where I get the error: The method Nickel() is undefined for the type PiggyBank. Any help would be much appreciated.

Abra
  • 19,142
  • 7
  • 29
  • 41
FTeng
  • 19
  • 2
  • Shouldn't that be: `public static void removeNickel() {coins.remove(new Nickel());}` ? – Abra Apr 29 '21 at 02:04
  • If it's new Nickel wouldn't that create a new nickel object and delete it? Sorry I'm not sure because I'm not very familiar with arraylists, it's just this assignment. – FTeng Apr 29 '21 at 02:07
  • Nickel() this way used for declaring the constructor add new to it or add an object which you want to remove – Srikrishna Sharma Apr 29 '21 at 02:08
  • Would adding new to it search the arraylist for the first instance of a Nickel object and remove it? – FTeng Apr 29 '21 at 02:09
  • It fixes the error and you said you were getting an error. Obviously it won't work. You need to override `equals(Object)` method. After you fix the compile error and run your code, you would discover that method `removeNickel` doesn't remove anything from `coins`. Then you would have a different question altogether. – Abra Apr 29 '21 at 02:12
  • Possible dubpilcate of the answer https://stackoverflow.com/questions/8520808/how-to-remove-specific-object-from-arraylist-in-java – Srikrishna Sharma Apr 29 '21 at 02:14
  • What I want is to remove Nickels and subsequent coins from the arraylist, how would I go about doing that? – FTeng Apr 29 '21 at 02:32
  • One thing that might work: instead of constantly creating new Nickles and such, perhaps you should create static variables for nickle, dime, etc., and use them. That way you won't have to override equals() – NomadMaker Apr 29 '21 at 02:37

2 Answers2

2

Normally when you want to remove an object from a List you need to override equals and hashCode in the object's class declaration. Otherwise, the List won't know how to compare existing instances of the contents.

But in this case, nickels, dimes, etc don't change their value (no pun intended). So there is no requirement to override those methods if you always use the same singleton instance to add or remove values. Here is how it might be done. Only nickels and dimes are shown.

public class PiggyBank {
    
    private static ArrayList<Coin> coins = new ArrayList<Coin>();
    final static Nickel NICKEL = new Nickel();
    final static Dime DIME = new Dime();
    
    public static void main(String[] args) {
        addNickel();
        addNickel();
        addDime();
        addDime();
        System.out.println(coins);
        removeDime();
        System.out.println(coins);
        removeNickel();
        System.out.println(coins);
        coins.add(new Quarter());
        coins.remove(new Quarter());  // won't remove quarter as it is
                                      // a different instance.
        System.out.println(coins);
    }
    
    public static void addNickel() {
        coins.add(NICKEL);
    }
    
    public static void addDime() {
        coins.add(DIME);
    }
    
    public static void removeDime() {
        coins.remove(DIME);
    }
    
    public static void removeNickel() {
        coins.remove(NICKEL);
    }
    
}

abstract class Coin {
    public String toString() {
        return getClass().getSimpleName();
    }
}

class Nickel extends Coin {
}

class Dime extends Coin {
}
class Quarter extends Coin {}

So there is no need to keep creating instances of the various monetary values. If you try and remove new instances of them, they won't be found. Perhaps a better way would be to use an enum.

enum Coins { NICKEL, DIME, QUARTER}

You can check them out in more detail at The Java Tutorials.

WJS
  • 36,363
  • 4
  • 24
  • 39
0

try this instead ,you can use instanceof key in remove method:

for(Coin coin : coins){ if(coin instanceof Nickel){ coins.remove(coin); } }

xindanding
  • 39
  • 3