Нужно в третьем блоке case изменить поля объектов динамического массива arraylist
Думал, через индексы get`рами и set'терами всё поизменять, но не смог обратиться к объекту по индексу.
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main {
static Scanner input =new Scanner(System.in);
public static void main(String[] args) {
ArrayList<record> records = new ArrayList<record>();
for (int i=0;i!=1;){
System.out.println("Что ты хочешь?\n1)Добавить запись\n2)Удалить запись\n3)Редактировать запись\n4)Показать запись\n5)Закончить программу");
int op = input.nextInt();
switch (op){
case 1:
records.add(new record());
break;
case 2:
System.out.println("Введите индек записи, которую хотите удалить:");
int b = input.nextInt();
records.remove(b);
break;
//---------------------------------------
//c этим блоком case 3 нужна помощь
case 3:
System.out.println("Введите индекс записи:");
int d = input.nextInt();
System.out.println("Что добрый молодец хочет отредачить?\n1)Имя записи\n2)дату\n3)автора\n4)саму запись\n5)задачу");
int c=input.nextInt();
switch (c){
case 1:
System.out.println("Введите новое имя записи:");
//тут нужно изменить поле name объекта типа record
break;
case 2:
System.out.println("Введите новую дату:");
// тут новую дату поставить
break;
case 3:
System.out.println("Введите новое имя автора:");
//тут нужно изменить поле author объекта типа record
break;
case 4:
System.out.println("Введите запись:");
//тут нужно изменить поле record объекта типа record
break;
case 5:
System.out.println("Введите новую задачу:");
//тут нужно изменить поле tasks объекта типа record
break;
}
break;
//=-----------------------------------------------------------------
case 4:
System.out.println("Введите индекс записи:");
int a = input.nextInt();
System.out.println("Название записи:");
System.out.println(records.get(a).name);
System.out.println("Дата записи:");
System.out.println(records.get(a).date);
System.out.println("Автор записи:");
System.out.println(records.get(a).author);
System.out.println("Сама Запись:");
System.out.println(records.get(a).record);
System.out.println("Задачи:");
System.out.println(records.get(a).tasks);
break;
case 5: i=1;
}}
//
}
}
public class record {
Scanner input =new Scanner(System.in);
// name
String name;
// date
Date date = new Date();
//author
String author;
//record
String record;
//tasks
String tasks;
record (){
System.out.println("Input name:");
name = input.nextLine();
System.out.println(date);
System.out.println("Input author:");
author = input.nextLine();
System.out.println("Input record:");
record = input.nextLine();
System.out.println("Input tasks:");
tasks = input.nextLine();
};
public Date getDate() {
return date;
}
public Scanner getInput() {
return input;
}
public String getAuthor() {
return author;
}
public String getName() {
return name;
}
public String getRecord() {
return record;
}
public String getTasks() {
return tasks;
}
public void setAuthor(String author) {
this.author = author;
}
public void setDate(Date date) {
this.date = date;
}
public void setInput(Scanner input) {
this.input = input;
}
public void setName(String name) {
this.name = name;
}
public void setRecord(String record) {
this.record = record;
}
public void setTasks(String tasks) {
this.tasks = tasks;
}
}
package com.company;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main {
static Scanner input =new Scanner(System.in);
public static void main(String[] args) {
ArrayList<record> records = new ArrayList<record>();
for (int i=0;i!=1;){
System.out.println("Что ты хочешь?\n1)Добавить запись\n2)Удалить запись\n3)Редактировать запись\n4)Показать запись\n5)Закончить программу");
int op = input.nextInt();
switch (op){
case 1:
records.add(new record());
break;
case 2:
System.out.println("Введите индек записи, которую хотите удалить:");
int b = input.nextInt();
records.remove(b);
break;
//---------------------------------------
//c этим блоком case 3 нужна помощь
case 3:
System.out.println("Введите индекс записи:");
int d = input.nextInt();
System.out.println("Что добрый молодец хочет отредачить?\n1)Имя записи\n2)дату\n3)автора\n4)саму запись\n5)задачу");
int c=input.nextInt();
switch (c){
case 1:
System.out.println("Введите новое имя записи:");
//тут нужно изменить поле name объекта типа record
break;
case 2:
System.out.println("Введите новую дату:");
// тут новую дату поставить
break;
case 3:
System.out.println("Введите новое имя автора:");
//тут нужно изменить поле author объекта типа record
break;
case 4:
System.out.println("Введите запись:");
//тут нужно изменить поле record объекта типа record
break;
case 5:
System.out.println("Введите новую задачу:");
//тут нужно изменить поле tasks объекта типа record
break;
}
break;
//=-----------------------------------------------------------------
case 4:
System.out.println("Введите индекс записи:");
int a = input.nextInt();
System.out.println("Название записи:");
System.out.println(records.get(a).name);
System.out.println("Дата записи:");
System.out.println(records.get(a).date);
System.out.println("Автор записи:");
System.out.println(records.get(a).author);
System.out.println("Сама Запись:");
System.out.println(records.get(a).record);
System.out.println("Задачи:");
System.out.println(records.get(a).tasks);
break;
case 5: i=1;
}}
//
}
}
public class record {
Scanner input =new Scanner(System.in);
// name
String name;
// date
Date date = new Date();
//author
String author;
//record
String record;
//tasks
String tasks;
record (){
System.out.println("Input name:");
name = input.nextLine();
System.out.println(date);
System.out.println("Input author:");
author = input.nextLine();
System.out.println("Input record:");
record = input.nextLine();
System.out.println("Input tasks:");
tasks = input.nextLine();
};
public Date getDate() {
return date;
}
public Scanner getInput() {
return input;
}
public String getAuthor() {
return author;
}
public String getName() {
return name;
}
public String getRecord() {
return record;
}
public String getTasks() {
return tasks;
}
public void setAuthor(String author) {
this.author = author;
}
public void setDate(Date date) {
this.date = date;
}
public void setInput(Scanner input) {
this.input = input;
}
public void setName(String name) {
this.name = name;
}
public void setRecord(String record) {
this.record = record;
}
public void setTasks(String tasks) {
this.tasks = tasks;
}
}
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
case 1:
System.out.println("Введите новое имя записи:");
//тут нужно изменить поле name объекта типа record
input.nextLine();
records.get(d).setName(input.nextLine());
break;