I am working on a Wallet and have started with the basics. I am adding Penny objects into a HashSet which stores the money. I initialise the capacity to 5 and start with a full wallet.
When trying to spend a Penny, null is returned (as expected) but then when calling the moneyCheck() method, there are still 5 coins.
I am not sure what to do as I am trying to remove a Penny using the.remove() method and the object is not being removed from the HashSet.
Any direction would be appreciated. Thank you
import java.util.HashSet;
public class Wallet{
private HashSet<Penny> walletPenny;
public Wallet(){
int walletCapacity = 5;
walletPenny = new HashSet<>();
int counter = 0;
while (counter < pocketCapacity){
pocketPenny.add(new Penny());
counter++;
}
}
public Penny removePenny(){
Penny spentPenny = null;
if (walletPenny.isEmpty()){
System.out.print("No more pennies!\n");
return spentPenny;
}
else {
pocketPenny.remove(spentPenny);
System.out.println(("Spent penny!"));
}
return spentPenny;
}
}
public int moneyCheck(){
System.out.print("Money remaining: " + walletPenny.size() + "\n");
return walletPenny.size();
}
Found an answer and thought I will update this in case anyone else has the same issue.
To remove the penny I initialised a Penny object with an iterator to find the next available object in the HashSet. I then returned that value to be used elsewhere in the program.
public Penny removePenny(){
Penny spentPenny;
if (walletPenny.isEmpty()){
System.out.print("No more pennies!\n");
spentPenny = null;
return spentPenny;
}
else {
spentPenny = walletPenny.iterator().next();
walletPenny.remove(spentPenny);
System.out.println(("Spent penny!"));
return spentPenny;
}
}
When you try to remove the penny with pocketPenny.remove(spentPenny)
, Java tries to find which penny to remove. In order to do that, it uses equals
and iterates the HashSet
until it finds a penny which is equal (identity wise) to your spentPenny
. More on how remove()
works . Since you provide null as spentPenny
, it will match nothing (your 5 pennies are initialized, so they are not null). You can verify this if you check the return value of .remove()
method - it will be false
.
In order to fix this, you need to:
remove()
methodequals()
in your Penny
classFull example:
public class Penny {
private final int serialNum;
public Penny(int serialNum) {
this.serialNum = serialNum;
}
@Override
public int hashCode() {
int hash = 5;
hash = 79 * hash + this.serialNum;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Penny other = (Penny) obj;
return this.serialNum == other.serialNum;
}
}
and then create your Pennies:
int counter = 0;
while (counter < pocketCapacity){
pocketPenny.add(new Penny(counter));
counter++;
}
Now you have 5 pennies, numbered 0 to 4. You can remove one, for example #3:
Penny spentPenny = new Penny(3);
pocketPenny.remove(spentPenny);
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.