I'm attempting to create a method that counts the number of Flower objects (created a class called flower btw) that returns the number of particular flowers in an object flower array.
I'm using a HashMap in order to map an integer (sum amount) to a key (key being the flower object). However, when I output the array, the memory address of the key in the heap alongside the key's location in the HashMap table.
My intention however is to print flower (it's name mainly) with the amount of flowers in that array that are of the same Flower Object type.
My code is as follows:
private void displayFlowers(Flower flowerPack[]) {
// TODO: Display only the unique flowers along with a count of any
// duplicates
/*
* For example it should say Roses - 7 Daffodils - 3 Violets - 5
*/
HashMap<Flower, Integer> flowerFrequency = new HashMap<Flower, Integer>();
for (Flower aFlower : flowerPack) {
if (flowerFrequency.containsKey(aFlower)) {
Integer i = flowerFrequency.get(aFlower);
i++;
} else {
flowerFrequency.put(aFlower, new Integer(1));
}
}
System.out.println(flowerFrequency);
}
The output is like this:
1: Add an item to the pack.
2: Remove an item from the pack.
3: Search for a flower.
4: Display the flowers in the pack.
0: Exit the flower pack interfact.
4
{null=1, Flower@6bc7c054=1, Flower@42a57993=1, Flower@75b84c92=1}
As instructed, I have also added the toString() and equals() methods to the Flower class as follows:
I added the toString() and equals() methods in the Flower class as follows:
public String toString() {
return this.color + " " + this.name + " smells like " + this.scentType + " is Thorny " + this.hasThorns;
}
@Override
public boolean equals(Object otherFlower) {
if (otherFlower == null) {
return false;
}
if (!Flower.class.isAssignableFrom(otherFlower.getClass())) {
return false;
}
final Flower other = (Flower) otherFlower;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (!(this.color.equals(other.color))) {
return false;
}
if (!(this.scentType.equals(other.scentType))) {
return false;
}
if (this.hasThorns != other.hasThorns) {
return false;
}
return true;
}