У меня есть учебная задачка, столкнулся с такой проблемой, вроде все написал нормально но в методе changeEmployee возникла проблема, весь код внизу:
public class Employee {
private String name;
private String lastname;
private String age;
private String gender;
private String education;
private String post;
private String department;
public Employee(String name, String lastname, String age, String gender, String education, String post, String department) {
this.name = name;
this.lastname = lastname;
this.age = age;
this.gender = gender;
this.education = education;
this.post = post;
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", lastname='" + lastname + '\'' +
", age='" + age + '\'' +
", gender='" + gender + '\'' +
", education='" + education + '\'' +
", post='" + post + '\'' +
", department='" + department + '\'' +
'}';
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Employee> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
addEmployee(list);
System.out.println("Введите end для выхода");
String input = scanner.nextLine();
if (input.equals("end")) {
showEmployee(list);
break;
}
changeEmployee(list);
}
}
public static void addEmployee(List<Employee> list) {
System.out.println("Введите инфоормацию о сотруднике (имя, фамилия, возраст, пол, образование, должность, отдел):");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String lastname = scanner.nextLine();
String age = scanner.nextLine();
String gender = scanner.nextLine();
String education = scanner.nextLine();
String post = scanner.nextLine();
String department = scanner.nextLine();
Employee employee = new Employee(name, lastname, age, gender, education, post, department);
list.add(employee);
}
public static void showEmployee(List<Employee> list) {
for (Employee l : list) {
System.out.println(l.toString());
}
}
public static void changeEmployee(List<Employee> list) {
Employee employee;
System.out.println("Введите фамилию и имя работника, для которого хотите изменить данные");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String lastname = scanner.nextLine();
int empIndex = searchBySurname(name, lastname, list);
if (empIndex != -1) {
System.out.println("Меняем " + lastname + " " + name + "?");
} else if (lastname.equals(employee.getLastname()) && name.equals(employee.getName())) {
System.out.println("Введите новую фамилию и имя: ");
employee.setLastname(scanner.next());
employee.setName(scanner.next());
for (Employee value : list) {
System.out.println(value);
}
} else {
System.out.println("Данный работник не найден");
}
}
public static int searchBySurname(String name, String lastname, List<Employee> list) {
if (list.equals(name) || list.equals(lastname))
return 1;
else
return -1;
}
}