该对象未添加到ArrayList中。如何解决这个问题?

时间:2019-05-16 01:51:11

标签: java inheritance arraylist

我正在为Planting house制作控制台应用程序。房子有6行,每行有25个部分。我使用行和节的数组列表构建了这种结构。现在,我想从用户输入中将植物添加到该部分。我

我尝试了多种方法来创建不同的方法,您可以在注释中看到这些方法,但是没有任何效果,对象工厂要么未创建,要么未添加到ArrayList工厂。

public class Section {
private int number;
private List<TomatoPlant> plants;

public int getNumber() {
    return number;
}

public Section(int number, List<TomatoPlant> plants) {
    this.number = number;
    this.plants = plants;
}

public Section(int number){
   this.number=number;
    //this.addPlants();
   this.plants= new ArrayList<>();
}

public List getPlants() {
    return this.plants;
}

public void addPlants(Cocktailtomatos cocktailtomato) {
    //this.plants= new ArrayList<>();
    this.plants.add(cocktailtomato);
    this.plants.add(Cocktailtomatos.createCocktailtomato("ss", "A", 1));
}

@Override
public String toString() {
    return "Section{" +
            "number=" + number +
            ", plants=" + plants +
            '}';
}

public void addPlant(String id, String row, int sectionnumber) {
    Cocktailtomatos cocktailtomato = new Cocktailtomatos(id, row, sectionnumber, true, false, false, 0, 10);
    this.addPlants(cocktailtomato);
}
}







public class Manager {
private static Manager ourInstance = new Manager();

public static Manager getInstance() {
    return ourInstance;
}

private Manager() {
}

Planthouse planthouse = new Planthouse();

public void addCocktailTomato(){
   Scanner scanner = new Scanner(System.in);

   System.out.println("Input Id:");
   String id = scanner.nextLine();
   System.out.println("Input Row");
   String row = scanner.nextLine();
   System.out.println("Input sectionnumber");
   Integer sectionnumber = scanner.nextInt();
   //Cocktailtomatos cocktailtomato = Cocktailtomatos.createCocktailtomato(id, row, sectionnumber);
   //cocktailtomato.setId(row + sectionnumber + "_" + id);
   //planthouse.getRow(row).getSection(sectionnumber).getPlants().add(cocktailtomato);
   //System.out.println("ID: ");
   //Cocktailtomatos cocktailtomato = new Cocktailtomatos(id, row, sectionnumber, true, false, false, 0, 10);
   //planthouse.getRow(row).getSection(sectionnumber).getPlants().add(cocktailtomato);

   //Cocktailtomatos cocktailtomato = Cocktailtomatos.createCocktailtomato(id, row, sectionnumber);
   //planthouse.getRow(row).getSection(sectionnumber).addPlants(cocktailtomato);

    planthouse.getRow(row).getSection(sectionnumber).addPlant(id, row, sectionnumber);
   //this.getPlantlist();
}

public int getPlantlist() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Input row:");
    String row = scanner.nextLine();
    System.out.println("Input sectionnumber:");
    Integer sectionnumber = scanner.nextInt();
   //return planthouse.getRow(row).getSection(sectionnumber).getPlants();
   //System.out.println(planthouse.getRow(row).getSection(sectionnumber).getPlants().size());
   System.out.println(planthouse.getRows());


       //System.out.println(planthouse.getRow("A").getSections().size());
       //for(int i=0; i<planthouse.getRow("A").getSections().size(); i++){
           //System.out.println(planthouse.getRow("A").getSections().get(i).getPlants());
       //}
       //for(int i=0; i<planthouse.getRows().size(); i++){


           // System.out.println(planthouse.getRows().get(i));
      // }

     return planthouse.getRow(row).getSection(sectionnumber).getPlants().size();
   }

  }





 public class Planthouse {
    private ArrayList<Row> rows = new ArrayList<>();

public Planthouse(){
    this.initRows();

}

public Planthouse(ArrayList<Row> rows) {
    this.setRows(rows);
}

public ArrayList<Row> getRows() {
    return rows;
}

private void initRows() {
    this.rows= new ArrayList<>();
    this.rows.add(new Row("A"));
    this.rows.add(new Row("B"));
    this.rows.add(new Row("C"));
    this.rows.add(new Row("D"));
    this.rows.add(new Row("E"));
    this.rows.add(new Row("F"));




}

public void setRows(ArrayList<Row> rows) {
    this.rows = rows;
}

public void printPlanthouse(){
    for(Row row: rows) {
        System.out.println(row.getId());
        row.printSections();
    }

}


public Row getRow(String rowId) {
    Row row = new Row(rowId);
    return row;

}

    public void addPlant(){

    }

    public Section getSection(Row row, Section section) {
    Planthouse planthouse = new Planthouse();
    planthouse.getRow(row.getId()).getSection(section.getNumber());
    return section;
    }
}

 public class Row {
private String id;
private ArrayList<Section> sections;

public Row(String id) {
    this.id = id;
    this.initSections();

}

public Row(String id, ArrayList<Section> sections) {
    this.id = id;
    this.setSections(sections);

}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public ArrayList<Section> getSections() {
    return sections;
}

public void setSections(ArrayList<Section> sections) {
    this.sections = sections;
}


public void initSections() {
    this.sections = new ArrayList<>();
        for (int i = 1; i <=25; i++) {
           this.sections.add(new Section(i));


        }

   }

    public void printSections() {
        for (Section section : sections) {
            System.out.println(section.getNumber());
        }
    }
    public Row(){

    }

@Override
public String toString() {
    return "Row{" +
            "id='" + id + '\'' +
            ", sections=" + sections +
            '}';
}

public Section getSection(int sectionnumber){

    //Section section = new Section(sectionnumber);
    Section section = sections.get(sectionnumber-1);

    return section;

}
}

方法getPlantList()打印出包含其节的所有行,以及包含其植物的每个节,并且在用户将植物添加到节中之后,这些节将被打印为空(没有植物)。因此,我得出的结论是,未创建任何工厂或未将其添加到数组列表。有人可以建议我问题出在哪里吗?谢谢。

1 个答案:

答案 0 :(得分:0)

当前的getRow每次都会创建一个未放置在整个数据层次结构中的新行。

public Row getRow(String rowId) {
    for (Row row : rows) {
        if (row.getId().equals(rowId)) {
            return row;
        }
    }
    // Either throw new IllegalArgumentException(); or:
    Row row = new Row(rowId);
    rows.add(row);
    return row;
}

相反,或者另外,您可能想要使用从rowId到Row的映射。

Map<String, Row> rowMap = new HashMap<>();
Map<String, Row> rowMap = new TreeMap<>(); // Sorted
Map<String, Row> rowMap = new LinkHashMap<>(); // In order of insertion, chronological

String rowId = "A";
rowMap.put(rowId, row);
Row row = rowMap.get(rowId);