Слишком долго проверяется и не проходит валидацию.
После решения прошлой просто зациклил да и поменял if else на switch. должно же все работать, а идея берет и секунд 25 проверяет.
Что делать, друзья?)
package com.javarush.task.task17.task1711;
import com.javarush.task.task17.task1711.Sex;
import java.io.IOException;
import java.text.DateFormat;
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 2
*/
public class Solution {
public static volatile List<Person> allPeople = new ArrayList<Person>();
static {
allPeople.add(Person.createMale("Иванов Иван", new Date())); //сегодня родился id=0
allPeople.add(Person.createMale("Петров Петр", new Date())); //сегодня родился id=1
}
public static void main(String[] args) throws ParseException, IOException {
String name;
Date bd;
try {
switch (args[0]) {
case "-c":
synchronized (allPeople) {
try {
for (int i = 1; i < args.length; i = i + 3) {
name = args[i];
bd = returnDate(args[i + 2]);
if (args[i + 1].equals("м")) {
allPeople.add(Person.createMale(name, bd));
System.out.println(allPeople.size() - 1);
}
if (args[i + 1].equals("ж")) {
allPeople.add(Person.createFemale(name, bd));
System.out.println(allPeople.size() - 1);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
}
}
break;
case "-u":
synchronized (allPeople) {
try {
for (int i = 1; i < args.length; i = i + 4) {
int id = Integer.parseInt(args[i]);
Person person = allPeople.get(id);
person.setName(args[i + 1]);
if ((args[i + 2]).equals("м"))
person.setSex(Sex.MALE);
else
person.setSex(Sex.FEMALE);
person.setBirthDay(returnDate(args[i + 3]));
}
} catch (ArrayIndexOutOfBoundsException e) {
}
break;
}
case "-d":
synchronized (allPeople) {
try {
for (int i = 1; i < args.length; i = i++) {
int id = Integer.parseInt(args[i]);
Person person = allPeople.get(id);
person.setName(null);
person.setBirthDay(null);
person.setSex(null);
}
break;
} catch (ArrayIndexOutOfBoundsException e) {
}
}
case "-i":
synchronized (allPeople) {
try {
for (int i = 1; i < args.length; i = i++) {
int id = Integer.parseInt(args[i]);
Person person = allPeople.get(id);
String personName = person.getName();
String personSex = null;
if (person.getSex() == Sex.MALE)
personSex = "м";
else personSex = "ж";
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String personBd = df1.format(person.getBirthDay());
System.out.println(personName + " " + personSex + " " + personBd);
}
break;
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
} catch (ArrayIndexOutOfBoundsException e) {
}
}
public static Date returnDate(String string) {
Date date = null;
String m = null;
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
try {
date = df.parse(string);
m = df1.format(date);
date = df1.parse(m);
} catch (Exception e) {
}
return date;
}
}