Ok, I think my code is working perfectly..
Can anyone help me and find why the conditions aren't met?
package com.codegym.task.task17.task1711;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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<>();
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static DateFormat formatIn = new SimpleDateFormat("MM d yyyy", Locale.ENGLISH);
public static DateFormat formatOut = new SimpleDateFormat("MMM d yyy", Locale.ENGLISH);
static {
allPeople.add(Person.createMale("Donald Chump", new Date())); // id=0
allPeople.add(Person.createMale("Larry Gates", new Date())); // id=1
}
public static void main(String[] args) throws IOException, ParseException {
String input = reader.readLine();
args = input.split(" ");
switch (args[0]) {
case "-c":
synchronized (allPeople) {
for (int i = 1; i < args.length; i = i + 5) {
String name = args[i];
Sex sex = (args[i + 1].equals("m")) ? Sex.MALE : Sex.FEMALE;
String dateString = (args[i + 2] + " " + args[i + 3] + " " + args[i + 4]).replace("\"", "");
Date dateObject = formatIn.parse(dateString);
if (sex == Sex.MALE) {
Person person = Person.createMale(name, dateObject);
allPeople.add(person);
System.out.println(allPeople.indexOf(person));
}
if (sex == Sex.FEMALE) {
Person person = Person.createFemale(name, dateObject);
allPeople.add(person);
System.out.println(allPeople.indexOf(person));
}
}
break;
}
case "-u":
synchronized (allPeople) {
for (int i = 1; i < args.length; i = i + 6) {
int id = Integer.valueOf(args[i]);
String name = args[i + 1];
Sex sex = (args[i + 2].equals("m")) ? Sex.MALE : Sex.FEMALE;
String dateString = (args[i + 3] + " " + args[i + 4] + " " + args[i + 5]).replace("\"", "");
Date dateObject = formatIn.parse(dateString);
allPeople.get(id).setName(name);
allPeople.get(id).setSex(sex);
allPeople.get(id).setBirthDate(dateObject);
}
break;
}
case "-d":
synchronized (allPeople) {
for (int i = 1; i < args.length; i++) {
Person person = allPeople.get(i);
person.setName(null);
person.setSex(null);
person.setBirthDate(null);
}
break;
}
case "-i":
synchronized (allPeople) {
for (int i = 1; i < args.length; i++) {
int id = Integer.valueOf(args[i]);
String name = allPeople.get(id).getName();
Sex sex = allPeople.get(id).getSex();
String sexString = (sex.equals(Sex.MALE)) ? "m" : "f";
Date dateObject = allPeople.get(id).getBirthDate();
String dateString = formatOut.format(dateObject);
System.out.println(name + " " + sexString + " " + dateString);
}
break;
}
}
}
}