Я делаю консольное приложение для 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 () распечатывает все строки со своими секциями и каждую секцию со своими растениями, и после того, как пользователь добавляет растение в секцию, эти секции печатаются пустыми (без растений). Поэтому я прихожу к выводу, что либо растение не создано, либо не добавлено в массив. Может кто-нибудь подсказать мне, в чем проблема? Спасибо.