为什么我的数据没有保存在数组中

时间:2015-11-22 12:46:35

标签: java

当我添加动物时,你能告诉我为什么它不起作用吗? 我之前工作过,当我将它添加到我在菜单中创建的ArrayList时。但我想将BLL和UI分开。

期待收到你们的来信:)

undefined

菜单类

public class Animal
{

    private String name; //Name of animal
    private String kindOfAnimal; //Kind of animal
    private int currentAge; //Current age of animal
    private int expectedAge; //Expected lifetime

    private List<Animal> animals = new ArrayList<>();

    public Animal(String name, String kindOfAnimal, int currentAge, int expectedAge)
    {
        this.name = name;
        this.kindOfAnimal = kindOfAnimal;
        this.currentAge = currentAge;
        this.expectedAge = expectedAge;
        this.animals = new ArrayList<>();
    }

    public int getExpectedAge()
    {
        return expectedAge;
    }

    public void setExpectedAge(int expectedAge)
    {
        this.expectedAge = expectedAge;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getKindOfAnimal()
    {
        return kindOfAnimal;
    }

    public void setKindOfAnimal(String kindOfAnimal)
    {
        this.kindOfAnimal = kindOfAnimal;
    }

    public int getCurrentAge()
    {
        return currentAge;
    }

    public void setCurrentAge(int currentAge)
    {
        this.currentAge = currentAge;
    }

    public String toString()
    {
        return "Type: " + getKindOfAnimal() + '\n'
                + "Name: " + getName() + '\n'
                + "Age: " + getCurrentAge() + '\n'
                + "Lifetime: " + getExpectedAge();
    }

    public void setAnimalList(Animal animal)
    {
        animals.add(animal);
    }

    public List<Animal> getAnimals()
    {
        return animals;
    }
}

2 个答案:

答案 0 :(得分:1)

batchModule.factory('routeTemplateMonitor', ['$route', 'batchLog', '$rootScope', function($route, batchLog, $rootScope) { var factory = { doSomething: function () { // do something } }; return factory; }); }]); 课程的每个实例中,您都有一个单独的动物列表,因此每个列表中只有一个动物。

Animal的列表保存在不同的类(可能是Animal类?)会更有意义。

答案 1 :(得分:1)

你的问题出在showAnimals()

的这一行
Animal animal = new Animal(null, null, 1, 1);

问题在于,每次要显示动物列表时,都要使用该行代码创建新列表。所以,它总是空洞的。

尝试将您的动物列表从Animal班级移至Menu班级。这样你就只有一份清单,你可以在哪里添加和展示动物。

public class Menu {

private List<Animal> animals = new ArrayList<>();

  public Menu()
  {
    menu();
  }


etc...


  private void addAnimal()
  {
    System.out.println("ADD ANIMAL");
    System.out.println();

    Scanner sc = new Scanner(System.in);

    try
    {
      System.out.println("Name: ");
      String name = sc.nextLine();

      System.out.println("Kind of animal: ");
      String kindOfAnimal = sc.nextLine();


      System.out.println("Current age: ");
      int currentAge = sc.nextInt();

      System.out.println("Expected age: ");
      int expectedAge = sc.nextInt();

      Animal animal = new Animal(name, kindOfAnimal, currentAge, expectedAge);

      animals.add(animal);
    }

    catch (Exception e)
    {
      System.out.println("Please enter the correct information");
    }

    System.out.println();
    menu();
}

  public void showAnimals()
  {
    if (animals.isEmpty())
    {
        System.out.println("There is no animals yet!");
        menu();
    }
    else
    {
      for (int i = 0; i < animals.size(); i++)
      {
        System.out.println(animals.get(i));
      }
      menu();
    }
}