Пробовал по разному и через switch и через if. Не проходит.
package com.javarush.task.pro.task14.task1401;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static final String INPUT_NAME = "\nВведите имя: ";
public static final String INPUT_AGE = "Введите возраст пользователя '%s': ";
public static final String CANNOT_BE_NULL = "Имя не может быть null.";
public static final String CANNOT_BE_EMPTY = "Имя не может быть пустым.";
public static final String CANNOT_CONTAIN_DIGIT = "Имя не может содержать цифры.";
public static final String CANNOT_BE_NEGATIVE = "Возраст не может быть меньше 0.";
public static final String CANNOT_BE_TOO_BIG = "Возраст не может быть больше 150.";
public static final String UNKNOWN_ERROR = "Неизвестная ошибка.";
public static final String FOUND = "\nПользователь '%s' найден под индексом %d.\n";
public static final String NOT_FOUND = "\nПользователь '%s' не найден.\n";
static List<User> users = new ArrayList<>();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
addUser(new User());
}
User userToSearch = new User();
userToSearch.setName("Рафаэль");
findUserIndex(userToSearch);
}
static void addUser(User user) {
System.out.print(INPUT_NAME);
String name = scanner.nextLine();
System.out.printf(INPUT_AGE, name);
int age = Integer.parseInt(scanner.nextLine());
user = new User();
//напишите тут ваш код
int errorSetName=user.setName(name);
int errorSetAge=user.setAge(age);
if (errorSetName==-1) {System.out.println(CANNOT_BE_NULL);}
if (errorSetName==-2) {System.out.println(CANNOT_BE_EMPTY);}
if (errorSetName==-3) { System.out.println(CANNOT_CONTAIN_DIGIT);}
if((errorSetName<-3)&&(errorSetName>0)) {System.out.println(UNKNOWN_ERROR);}
if (errorSetAge==-1){System.out.println(CANNOT_BE_NEGATIVE);}
if (errorSetAge==-2){System.out.println(CANNOT_BE_TOO_BIG);}
if((errorSetAge<-2)&&(errorSetAge>0)) {System.out.println(UNKNOWN_ERROR);}
users.add(user);
}
// if(errorSetName!=0) {
// String nameResult = switch (errorSetName) {
// case -1 -> CANNOT_BE_NULL;
// case -2 -> CANNOT_BE_EMPTY;
// case -3 -> CANNOT_CONTAIN_DIGIT;
// default -> UNKNOWN_ERROR;
// };
// System.out.println(nameResult);return;
// }
// if (errorSetAge!=0){
//String ageResult = switch (errorSetAge) {
// case -1 -> CANNOT_BE_NEGATIVE;
// case -2 -> CANNOT_BE_TOO_BIG;
// default -> UNKNOWN_ERROR;
//};
// System.out.println(ageResult);return;
//}
// users.add(user);
// }
static void findUserIndex(User user) {
//напишите тут ваш код
if (users.indexOf(user)==-1){
System.out.printf(NOT_FOUND, user.getName());
}else
System.out.printf(FOUND, user.getName(), users.indexOf(user));
}
}