All pets share the same name, since the name
attribute is static in the Pet
class.
Every time the line name = petName;
runs on Pet
constructor, the name of all pets become the value of petName
. Remove the static
keyword from the attibute declaration.
Also, I couldn't help noticing an unrelated bug in the methods removeDog
and removeCat
. They will remove any pet with the given name from the mainList, while I believe you intended removeDog
to remove only dogs and removeCat
to remove the cats. The user can add a cat called "Garfield" and then successfully remove the Garfield dog.
You could fix that by checking if the pet is an instance of a dog or of a cat before checking its name using the instanceof operator.
public static void removeDog(String dogName)
{
for(Pet pet : mainList)
{
if((pet instanceof Dog) && pet.getName().equals(dogName))
{
mainList.remove(pet);
}//end of if statement
}//end of for loop
}//end of removeDog method
public static void removeCat(String catName)
{
for(Pet pet : mainList)
{
if((pet instanceof Cat) && pet.getName().equals(catName))
{
mainList.remove(pet);
}//end of if statement
}//end of for loop
}//end of removeCat method