Решал задачу два дня. Запар был как правильно распарсить введенную строку по шаблонам: Иванов Иван Иванович м 15/04/1990, или: Иванов м 15/04/1990, или: Иванов Иван м 15/04/1990. Но валидатору не понравилось моё решение.
Коллеги, в чем моя ошибка?
package com.javarush.task.task17.task1710;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/*
CRUD
*/
public class Solution {
public static List<Person> allPeople = new ArrayList<Person>();
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static String textLine = "";
private static Date date;
static {
allPeople.add(Person.createMale("Иванов Иван", new Date())); //сегодня родился id=0
allPeople.add(Person.createMale("Петров Петр", new Date())); //сегодня родился id=1
}
public static void main(String[] args) {
int textLineInt = 0;
try {
switch (args[0]) {
case "-i":
try {
textLineInt = Integer.parseInt(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
outPerson(textLineInt);
break;
case "-c":
createPerson();
break;
case "-d":
try {
textLineInt = Integer.parseInt(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
deletePerson(textLineInt);
break;
case "-u":
//outAllPerson();
updatePerson();
break;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Программа запущена без аргументов");
}
//outAllPerson();
}
public static void updatePerson () {
try {
textLine = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
String[] words = textSplitCreate(textLine);
int index = 0;
try {
index = Integer.parseInt(words[0]);
} catch (NumberFormatException e) {
System.out.println("Не верный индекс");
}
try {
allPeople.get(index).setName(words[1]);
if (words[2].equals("м")) {
allPeople.get(index).setSex(Sex.MALE);
} else {
allPeople.get(index).setSex(Sex.FEMALE);
}
allPeople.get(index).setBirthDate(setDate(words[3]));
} catch (IndexOutOfBoundsException e) {
System.out.println("Не верный индекс!");
}
}
public static void outAllPerson () {
for (int i = 0; i < allPeople.size(); i++) {
outPerson(i);
}
}
//Deleted Person
private static void deletePerson (int index) {
try {
allPeople.get(index).setName(null);
allPeople.get(index).setSex(null);
allPeople.get(index).setBirthDate(null);
} catch (IndexOutOfBoundsException e) {
System.out.println("Не верный индекс!");
}
}
//Create Person
private static void createPerson() {
try {
textLine = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
String[] words = textSplitCreate(textLine);
if (words[2].equals("м")) {
allPeople.add(Person.createMale(words[1], setDate(words[3])));
System.out.println(allPeople.size());
} else {
allPeople.add(Person.createFemale(words[1], setDate(words[3])));
System.out.println(allPeople.size());
}
}
public static Date setDate(String str) {
try {
date = new SimpleDateFormat("dd/MM/yyyy").parse(str);
} catch (ParseException e) {
System.out.println("Не верный формат даты! Ожидается: ДД/ММ/ГГГГ");
}
return date;
}
//Если метод вызвали с параметром MALE, то отдаем "м", иначе - "ж"
public static String textGetSex(Sex str) {
if (str.equals(Sex.MALE)) {
return "м";
} else {
return "ж";
}
}
//Метод на вход получает id и выводит персону согласно задания
public static void outPerson(int id) {
String sex = null;
String datePeople = null;
String name = null;
try {
sex = textGetSex(allPeople.get(id).getSex());
datePeople = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).format(allPeople.get(id).getBirthDate());
name = allPeople.get(id).getName();
} catch (Exception e) {
}
System.out.println(name + " " + sex + " " + datePeople);
}
//Разбираем строку на массив слов. В качестве разделителя используем пробел!
public static String[] textSplitCreate(String str) {
String stringTemp;
String[] words = new String[4];
//words[1] = "нет";
//Вырезает всё, что после первого пробела
String[] temp = null;
try {
temp = str.split("\\s.+");
Integer.parseInt(temp[0]);
words[0] = temp[0];
words[1] = str.split("\\d ")[1];
words[2] = words[1].split(" +\\d")[0];
words[2] = words[2].split(".+\\s")[1];
words[1] = words[1].split(" [мж]")[0];
words[3] = str.split("\\s[мж]\\s")[1];
} catch (NumberFormatException e) {
words[1] = str.split(" [мж]")[0];
words[2] = str.split(" +\\d")[0];
words[2] = words[2].split(".+\\s")[1];
words[3] = str.split("\\s[мж]\\s")[1];
}
return words;
}
public static String[] textSplitId(String str) {
String[] words = new String[3];
return words;
}
}