通用ArrayList和其自动添加项目阵列菜单 [英] Generic ArrayList and a menu which adds items automatically to Array

查看:167
本文介绍了通用ArrayList和其自动添加项目阵列菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建宠物名单,其中包括猫,狗,我必须使用继承这一点。宠物类西港岛线包括姓名变量,狗类将包括姓名和重量变量和猫类将包括姓名和coatColor变量。

我必须创建一个测试类,它包含以下菜单。


  1. 添加狗

  2. 添加猫

  3. 删除狗

  4. 删除猫

  5. 列表狗

  6. 列表猫

  7. 列出所有的宠物

  8. 显示最大,最小和平均体重狗

  9. 退出

我写的每一个类,但我无法完成他们我需要你的帮助。

我的课程如下。 (如果我做错了什么,请编辑它,并告诉我,我觉得我做错了什么事)

宠物类:

 公共类宠物{
    私有静态字符串名称;    保护串的getName(){
        返回名称;
    }    保护无效的setName(字符串了newName){
        名称=了newName;
    }    公共PET(字符串petName){
        名称= petName;
    }
}

狗类:

 公共类犬扩展宠物{
    私人双权重;
    保护双getWeight(){
        返回重;
    }    保护无效setWeight(双newWeight){
        体重= newWeight;
    }    公共犬(双dogWeight,字符串petName){
        超(petName);
        体重= dogWeight;    }
}

Cat类:

 公共类猫宠物扩展{
    私人字符串coatColor;
    保护字符串getWeight(){
        返回coatColor;
    }    保护无效的setColor(字符串newColor){
        coatColor = newColor;
    }    公共猫(字符串petName,字符串coatColor){
        超(petName);
        this.coatColor = coatColor;
    }
}

PetTest类:

 进口的java.util。*;公共类PetTest
{
    //我做到了静态,使所有的方法才能够看到它。
    静态的ArrayList<宠物及GT; mainList =新的ArrayList<宠物及GT;();
    静态的Iterator<宠物及GT; mainIter = mainList.iterator();
    静态扫描键盘=新的扫描仪(System.in);    静态宠物cat2342 =新的猫(mirnav,beyaz);
    公共静态无效listDog​​s()
    {
        而(mainIter.hasNext())
        {
            对(宠物狗:mainList)
            {
                的System.out.println(狗);
            } // for循环结束        } // while循环结束
    } // listDog​​s方法结束    公共静态无效listCats()
    {
        而(mainIter.hasNext())
        {
            对(宠物猫:mainList)
            {
                的System.out.println(猫);
            } // for循环结束        } // while循环结束
    } // listCats方法结束    公共静态无效addDog(字符串dogName,双dogWeight)
    {
        宠物狗=新的狗(dogName,dogWeight);
        mainList.add(狗);
    } // addDog方法结束    公共静态无效用addCat(字符串catName,字符串furColor)
    {
        宠物猫=新的猫(catName,furColor);
        mainList.add(猫);
    } //用addCat方法结束    公共静态无效removeDog(字符串dogName)
    {
        而(mainIter.hasNext())
        {
            对于(宠:mainList)
            {
                如果(pet.getName()。等于(dogName))
                {
                    mainIter.remove();
                } //如果语句结束
            } // for循环结束
        } // while循环结束    } // removeDog方法结束
    公共静态无效removeCat(字符串catName)
    {
        而(mainIter.hasNext())
        {
            对于(宠:mainList)
            {
                如果(pet.getName()。等于(catName))
                {
                    mainIter.remove();
                } //如果语句结束
            } // for循环结束
        } // while循环结束    } // removeCat方法结束
    公共静态无效的主要(字串[] args)
    {        的System.out.println(1添加的狗。);
        的System.out.println(2添加的猫。);
        的System.out.println(3拆下的狗。);
        的System.out.println(4取出的猫。);
        的System.out.println(5名单的狗。);
        的System.out.println(6列表猫。);
        的System.out.println(7列出所有的宠物。);
        的System.out.println(8显示最小值,最大值和平均体重的狗。);
        的System.out.println(0退出。);
        INT行动= keyboard.nextInt();
        扫描参数1 =新的扫描仪(System.in);
        扫描参数2 =新的扫描仪(System.in);        而(动作!= 0)
        {
        开关(动作)
        {
        情况1:
            的System.out.println(在你要添加的狗的名字类型。);
            串dogName = parameter1.next();
            的System.out.println(在你要添加的狗的重量类型。);
            双dogWeight = parameter2.nextDouble();
            addDog(dogName,dogWeight);
            showMenu();
        案例2:
            的System.out.println(在你要添加的猫的名称类型。);
            串catName = parameter1.next();
            的System.out.println(在你要添加的猫的颜色类型。);
            串furColor = parameter2.next();
            用addCat(catName,furColor);        案例3:
            的System.out.println(在要删除狗的名字类型。);
            串dogToRemove = parameter1.next();
            removeDog(dogToRemove);        情况4:
            的System.out.println(在要删除狗的名字类型。);
            串catToRemove = parameter1.next();
            removeDog(catToRemove);        情况5:
            listDog​​s();        情况6:
            listCats();
        } // switch语句结束        } // while循环结束    主要方法} //结束} //类的结束


解决方案

你的所作所为是正确的大部分。虽然采取了一些后事:


  1. 您在你的猫类错误( coatColor 吸气):错误的名称

  2. 您的方法 addDog 实际上增加了一只猫,这是混淆了

现在,有趣的东西(工具),你需要:


  • 您需要一种方法为每个操作(添加猫,狗加等)

  • 您需要添加您在mainList创建宠物: mainList.add(...)

  • 您宁愿使用一个开关/箱语法来调用你的方法

  • 要删除猫或狗,你可能需要指明要哪一个删除(或者只有最后的加)

  • 要列出狗或猫,你会需要遍历你的 mainList ,只保留那些具有良好类型:使用的instanceof 运营商

  • 要显示的最小/最大和平均体重为狗,你需要在你的列表迭代为好,并保持所有的计数。

不管怎样,我可以上的每个点更具体的但不会是你的乐趣,因为你需要寻找自己的解决方案。有更好的方法来做到这一点(如使用像番石榴例如框架),但是这对你下一步!

I must create list of pets that includes cats and dogs and I must use inheritance for this. Pet class wil include name variable, Dog class will include name and weight variables and Cat class will include name and coatColor variables.

And I must create a Test class which includes that menu below.

  1. Add dog
  2. Add cat
  3. Remove dog
  4. Remove cat
  5. List dogs
  6. List cats
  7. List all pets
  8. Show min, max and average weight of dogs
  9. Quit

I've written every class but I couldn't complete them I need your help.

My classes are below. (If I did something wrong please edit it and tell me, I think I did something wrong)

Pet Class:

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

Dog Class:

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;

    }
}

Cat Class:

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 Class:

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

解决方案

What you did is mostly right. Though take care of some things :

  1. You have an error in your cat class (for coatColor getter) : wrong name
  2. Your method addDog actually adds a cat, which is confusing

Now, the fun stuff (the tools) you need :

  • You'll need one method for each action (add cat, add dog, etc)
  • You'll need to add the pets you create in the mainList : mainList.add(...)
  • You'd rather use a switch/case syntax to call your methods
  • To remove a cat or a dog, you may need to indicate which one you want to remove (or maybe only the last on added)
  • To list the dogs or the cats, you"ll need to iterate over your mainList and keep only those with a good type : use the instanceof operator
  • To shown min/max and average weight for dog, you'll need to iterate as well over your list, and keep a count of all those.

Anyway, I could be more specific on each point but that would not be fun for you, as you need to look for solutions by yourself. There are better ways to do that (like using a framework like Guava for example), but that's the next step for you !

这篇关于通用ArrayList和其自动添加项目阵列菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆