Коллеги, помогите. Весь день убил на эту задачу.
package com.javarush.task.task17.task1711;
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 {
//args = new String[]{"-c", "Петя", "м", "15/04/1991", "Кадя", "ж", "15/04/1992"};
//args = new String[]{"-u", "0", "Вася", "sex1", "15/04/1990", "1", "Толя", "sex2", "15/04/1990"};
//args = new String[]{"-d", "0", "1"};
args = new String[]{"-i", "0", "1"};
switch (args[0]) {
case "-c":
synchronized (allPeople) {
add(args);
break;
}
case "-u":
synchronized (allPeople) {
update(args);
break;
}
case "-d":
synchronized (allPeople) {
delete(args);
break;
}
case "-i":
synchronized (allPeople) {
information(args);
break;
}
default:
System.out.println("Error");
}
System.out.println(allPeople);
}
static public void add(String[] args) throws ParseException {
if (!args[0].equals("-c")) return;
for (int i = 0; i < (args.length - 1) / 3; i++) {
int index = i * 3;
String name = args[index + 1];
Sex sex = args[index + 2].equals("м") ? Sex.MALE : Sex.FEMALE;
String dateStr = args[index + 3];
SimpleDateFormat sDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = sDateFormat.parse(dateStr);
if (sex == Sex.FEMALE)
allPeople.add(Person.createFemale(name, date));
else
allPeople.add(Person.createMale(name, date));
System.out.println(allPeople.size() - 1);
}
}
static public void update(String[] args) throws ParseException {
if (!args[0].equals("-u")) return;
for (int i = 0; i < (args.length - 1) / 4; i++) {
int index = i * 4;
String id = args[index + 1];
String name = args[index + 2];
Sex sex = args[index + 3].equals("м") ? Sex.MALE : Sex.FEMALE;
String dateStr = args[index + 4];
SimpleDateFormat sDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = sDateFormat.parse(dateStr);
allPeople.get(Integer.parseInt(id)).setName(name);
allPeople.get(Integer.parseInt(id)).setBirthDate(date);
allPeople.get(Integer.parseInt(id)).setSex(sex);
}
}
static public void delete(String[] args) {
if (!args[0].equals("-d")) return;
ArrayList<Person> removePersons = new ArrayList<>();
for (int i = 1; i < args.length; i++) {
String id = args[i];
removePersons.add(allPeople.get(Integer.parseInt(id)));
}
for (Person removePerson : removePersons) {
allPeople.remove(removePerson);
}
}
static public void information(String[] args) {
if (!args[0].equals("-i")) return;
for (int i = 1; i < args.length; i++) {
String id = args[i];
Person person = allPeople.get(Integer.parseInt(id));
SimpleDateFormat sDataFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String dataStr = sDataFormat.format(person.getBirthDate());
System.out.println(String.format("%s %s %s", person.getName(), person.getSex() == Sex.MALE ? "м" : "ж", dataStr));
}
}
}