-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.

8
  • Shouldn't that be: public static void removeNickel() {coins.remove(new Nickel());} ? Commented Apr 29, 2021 at 2: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. Commented Apr 29, 2021 at 2:07
  • Nickel() this way used for declaring the constructor add new to it or add an object which you want to remove Commented Apr 29, 2021 at 2:08
  • Would adding new to it search the arraylist for the first instance of a Nickel object and remove it? Commented Apr 29, 2021 at 2: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. Commented Apr 29, 2021 at 2:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.