使用泛型类型为ArrayList创建菜单

时间:2014-05-13 20:23:13

标签: java inheritance arraylist switch-statement

我必须创建包含猫和狗的宠物列表,我必须使用继承。 Pet class wil包含name变量,Dog类将包含name和weight变量,Cat类将包含name和coatColor变量。

我必须创建一个包含下面菜单的Test类。

加狗 加猫 去掉狗 删除猫 列出狗 列出猫 列出所有宠物 显示狗的最小,最大和平均重量 退出

我的课程如下。我认为我的宠物,猫和狗课程运作良好,但我的PetTest课程中存在一些问题,我无法找出它是什么。如果你能帮助我,我会很高兴。

宠物等级:

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;
    }



}

狗类:

public class Dog extends Pet
{

    private Double weight;

    public double getWeight()
    {
        return weight;
    }

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


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

    }
}

猫类:

public class Cat extends Pet
{

    private String coatColor;

    protected String getColor()
    {
        return coatColor;
    }

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

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

PetTest课程:

import java.util.*;

public class PetTest
{
    //i made it static to make all method to be able to see it.
    static ArrayList<Pet> mainList = new ArrayList<Pet>();
    static Iterator<Pet> mainIter = mainList.iterator();
    static Scanner keyboard = new Scanner(System.in);

    static Pet cat2342 = new Cat("mirnav", "beyaz");
    public static void listDogs()
    {
            for(Pet pet: mainList)
            {
                if (pet instanceof Dog)
                {
                    System.out.println(pet);
                }
            }//end of for loop
    }//end of listDogs method

    public static void listCats()
    {
            for(Pet pet: mainList)
            {
                if(pet instanceof Cat)
                {
                    System.out.println(pet);
                }
            }//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))
                {
                    mainIter.remove();
                }//end of if statement
            }//end of for loop

    }//end of removeDog method


    public static void removeCat(String catName)
    {
        while (mainIter.hasNext())
        {
            for(Pet pet : mainList)
            {
                if(pet.getName().equals(catName))
                {
                    mainIter.remove();
                }//end of if statement
            }//end of for loop


        }//end of while 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();


        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();


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


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


        case 5:
            listDogs();
            showMenu();
        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

1 个答案:

答案 0 :(得分:0)

你所说的问题很简单就是你无法移除狗/宠物:它是由java.lang.IllegalStateException引起的 你想从迭代器中删除一个宠物(第52行),但你从未调用过next()方法 - 迭代器的索引位于-1。 你应该在每个foreach循环中调用next(),或者添加一个计数器并直接从ArrayList中删除该行。

但是你必须发布到codereview,并且请尝试更详尽。 “有一些问题”不够好,你应该给我一些更多的信息。也许你遇到了其他问题。

希望这会有所帮助。

再见