Pregunta

I want to add and remove elements to an ArrayList called mainList by the menu which I created and with a switch statement in the PetTest class I designed.

However, when I add 2 dogs or cats (for example one named Garfield and another named kitten), and I call the listCats method by choosing option 6 from my switch statement, it shows that both of cats have the same name "kitten". Do you know why this is happening in my code?

Pet Class:

public class Pet
    {
        private static String name;



        public String getName()
        {
            return name;
        }

        protected void setName(String newName)
        {
            name = newName;
        }

        public Pet(String petName) {
            name = petName;
        }



    }

Dog Class:

public class Dog extends Pet
{

    private Double weight;

    public double getWeight()
    {
        return weight;
    }

    protected void setWeight(double newWeight)
    {
        weight = newWeight;
    }

    public String toString() {
        return String.format("%s , %f", getName(), getWeight());
    }

    public Dog(String petName, double dogWeight)
    {
        super(petName);
        weight = dogWeight;

    }
}

Cat Class:

 public class Cat extends Pet
{

    private String coatColor;

    protected String getColor()
    {
        return coatColor;
    }

    protected void setColor(String newColor)
    {
        coatColor = newColor;
    }

    public String toString() {
        return String.format("%s , %s", getName(), getColor());
    }


    public Cat(String petName, String coatColor)
    {
        super(petName);
        this.coatColor = coatColor;
    }
}

PetTest Class:

import java.util.*;

public class PetTest
{
    static ArrayList<Pet> mainList = new ArrayList<Pet>();
    static Iterator<Pet> mainIter = mainList.iterator();
    static Scanner keyboard = new Scanner(System.in);


    public static void listDogs()
    {
            for(Pet pet: mainList)
            {
                if (pet instanceof Dog)
                {
                    System.out.println(pet.toString());
                }
            }//end of for loop
    }//end of listDogs method

    public static void listCats()
    {
            for(Pet pet: mainList)
            {
                if(pet instanceof Cat)
                {
                    System.out.println(pet.toString());
                }
            }//end of for loop

    }//end of listCats method

    public static void addDog(String dogName,Double dogWeight)
    {
        Pet dog = new Dog(dogName, dogWeight);
        mainList.add(dog);
    }//end of addDog method

    public static void addCat(String catName, String furColor)
    {
        Pet cat = new Cat(catName, furColor);
        mainList.add(cat);
    }//end of addCat method

    public static void removeDog(String dogName)
    {
        for(Pet pet : mainList)
            {
                if(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)
    {//java.lang.IllegalStateException you must call next method of iterator
            for(Pet pet : mainList)
            {
                if(pet.getName().equals(catName))
                {
                    mainList.remove(pet);

                }//end of if statement

            }//end of for loop

    }//end of removeCat method

    public static void showMenu()
    {
        System.out.println("1. Add dog ");
        System.out.println("2. Add cat");
        System.out.println("3. Remove dog");
        System.out.println("4. Remove cat");
        System.out.println("5. List dogs");
        System.out.println("6. List cats");
        System.out.println("7. List all pets");
        System.out.println("8. Show min, max and average weight of dogs");
        System.out.println("0. Quit");
        int action = keyboard.nextInt();

        Scanner parameter1 = new Scanner(System.in);
        Scanner parameter2 = new Scanner(System.in);

        while(action != 0)
        {
        switch(action)
        {
        case 1: 
            System.out.println("Type in the name of the dog that you want to add.");
            String dogName = parameter1.next();
            System.out.println("Type in the weight of the dog that you want to add.");
            Double dogWeight = parameter2.nextDouble();
            addDog(dogName,dogWeight);
            showMenu();

            break;

        case 2:
            System.out.println("Type in the name of the cat that you want to add.");
            String catName = parameter1.next();
            System.out.println("Type in the color of the cat that you want to add.");
            String furColor = parameter2.next();
            addCat(catName,furColor);
            showMenu();

            break;

        case 3:
            System.out.println("Type in the name of dog that you want to remove.");
            String dogToRemove = parameter1.next();
            removeDog(dogToRemove);
            showMenu();

            break;

        case 4:
            System.out.println("Type in the name of dog that you want to remove.");
            String catToRemove = parameter1.next();
            removeDog(catToRemove);
            showMenu();

            break;

        case 5:
            listDogs();
            showMenu();

            break;

        case 6:
            listCats();
            showMenu();
            break;


        }//end of switch statement

        }//end of while loop
    }//end of showMenu method

    public static void main(String[] args)
    {

        showMenu();



        }//end of main method



}//end of the class
¿Fue útil?

Solución 2

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

Otros consejos

Your name variable is defined as static in the Pet class, which means it will be the same across all Pet instances. Remove the static modifier and you should be good.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top