Generic ArrayList和一个自动将项添加到Array的菜单

时间:2014-05-12 17:40:39

标签: java inheritance arraylist

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

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

  1. 添加狗
  2. 添加猫
  3. 删除狗
  4. 删除猫
  5. 列出狗
  6. 列出猫
  7. 列出所有宠物
  8. 显示狗的最小,最大和平均重量
  9. 退出
  10. 我写过每节课,但我无法完成,我需要你的帮助。

    我的课程如下。 (如果我做错了,请编辑并告诉我,我认为我做错了什么)

    宠物等级:

    public class Pet{
        private static String name;
    
        protected 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;      
        protected double getWeight(){
            return weight;
        }
    
        protected void setWeight(double newWeight){
            weight = newWeight;
        }
    
        public Dog(double dogWeight, String petName){
            super(petName);
            weight = dogWeight;
    
        }
    }
    

    猫类:

    public class Cat extends Pet{       
        private String coatColor;       
        protected String getWeight(){
            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()
        {
            while(mainIter.hasNext())
            {
                for(Pet dog: mainList)
                {
                    System.out.println(dog);
                }//end of for loop
    
            }//end of while loop
        }//end of listDogs method
    
        public static void listCats()
        {
            while(mainIter.hasNext())
            {
                for(Pet cat: mainList)
                {
                    System.out.println(cat);
                }//end of for loop
    
            }//end of while 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)
        {
            while (mainIter.hasNext())
            {
                for(Pet pet : mainList)
                {
                    if(pet.getName().equals(dogName))
                    {
                        mainIter.remove();
                    }//end of if statement
                }//end of for loop
    
    
            }//end of while 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 main(String[] args)
        {
    
            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);
    
    
    
            case 3:
                System.out.println("Type in the name of dog that you want to remove.");
                String dogToRemove = parameter1.next();
                removeDog(dogToRemove);
    
    
    
            case 4:
                System.out.println("Type in the name of dog that you want to remove.");
                String catToRemove = parameter1.next();
                removeDog(catToRemove);
    
    
    
            case 5:
                listDogs();
    
            case 6:
                listCats();
    
    
    
    
            }//end of switch statement
    
            }//end of while loop
    
        }//end of main method
    
    
    
    }//end of the class
    

2 个答案:

答案 0 :(得分:0)

制作Cat&amp; Dog类继承自Pet类,如下所示:

public class Cat extends Pet {...}
public class Dog extends Pet {...}

如果您希望子类在没有getter / setter方法的情况下使用它,name类中的Pet变量也可以设置为protected。 (取决于您的要求)

答案 1 :(得分:0)

你所做的大部分是正确的。虽然照顾好一些事情:

  1. 您的cat类中存在错误(对于coatColor getter):错误的名称
  2. 您的方法addDog实际上会添加一只令人困惑的猫
  3. 现在,您需要的有趣的东西(工具):

    • 每个动作都需要一种方法(添加猫,添加狗等)
    • 您需要在主列表中添加您创建的宠物:mainList.add(...)
    • 您宁愿使用开关/案例语法来调用您的方法
    • 要删除猫或狗,您可能需要指明要删除哪一个(或者可能只添加最后一个)
    • 要列出狗或猫,您需要迭代mainList并仅保留那些类型合适的人:使用instanceof运算符
    • 要显示狗的最小/最大和平均重量,您需要在列表上进行迭代,并保留所有这些重量。

    无论如何,我可以更具体地指出每一点,但这对你来说并不好玩,因为你需要自己寻找解决方案。有更好的方法可以做到这一点(例如使用像Guava这样的框架),但这是你的下一步!