(до этого оператором Switch не пользовался, всё обходился If/else)
Не пропускает валидатор по всем кейсам.... прям руки опускаются... По сути же мои методы проходятся по всем параметрам массива args?! в чём же тогда проблема???
package com.javarush.task.task17.task1711;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
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 {
switch (args [0]){
case "-c":
synchronized (allPeople) {
addPersons(args);
break;
}
case "-u":
synchronized (allPeople) {
updPerson(args);
break;
}
case "-d":
synchronized (allPeople) {
delPerson(args);
break;
}
case "-i":
synchronized (allPeople) {
infoPerson(args);
break;
}
}
}
private static ArrayList<String> createListArgs(String[] args) {
ArrayList <String> listArgs = new ArrayList<>();
for (int i = 1; i < args.length-1; i++){
listArgs.add(args [i]);
}
return listArgs;
}
private static void addPersons(String[] args) throws ParseException {
ArrayList <String> listArgs = createListArgs(args);
while (!listArgs.isEmpty()){
if (listArgs.get(1).equals("MALE")){
allPeople.add(Person.createMale(listArgs.get(0), new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).parse(listArgs.get(2))));
for (int i = 0; i < 3; i++){
listArgs.remove(0);
}
}else {
allPeople.add(Person.createFemale(listArgs.get(0), new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).parse(listArgs.get(2))));
for (int i = 0; i < 3; i++){
listArgs.remove(0);
}
}
}
}
private static void updPerson(String[] args) throws ParseException {
ArrayList <String> listArgs = createListArgs(args);
while (!listArgs.isEmpty()){
if (listArgs.get(2).equals("MALE")){
Person person = allPeople.get(Integer.parseInt(listArgs.get(0)));
person.setName(listArgs.get(1));
person.setSex(Sex.MALE);
person.setBirthDate(new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).parse(listArgs.get(2)));
for (int i = 0; i < 4; i++){
listArgs.remove(0);
}
}else {
Person person = allPeople.get(Integer.parseInt(listArgs.get(0)));
person.setName(listArgs.get(1));
person.setSex(Sex.FEMALE);
person.setBirthDate(new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).parse(listArgs.get(2)));
for (int i = 0; i < 4; i++){
listArgs.remove(0);
}
}
}
}
private static void delPerson(String[] args) {
ArrayList <String> listArgs = createListArgs(args);
while (!listArgs.isEmpty()){
Person person = allPeople.get(Integer.parseInt(listArgs.get(0)));
person.setName(null);
person.setSex(null);
person.setBirthDate(null);
listArgs.remove(0);
}
}
private static void infoPerson(String[] args) {
ArrayList <String> listArgs = createListArgs(args);
while (!listArgs.isEmpty()){
Person person = allPeople.get(Integer.parseInt(listArgs.get(0)));
String text = person.getName() + " ";
if (person.getSex() == Sex.MALE){
text = text + "м ";
}else {
text = text + "ж ";
}
text = text + new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).format(person.getBirthDate());
System.out.println(text);
listArgs.remove(0);
}
}
}