I use this is debug input : -c Washington m 04 15 1990 Jefferson f 05 03 1994 BAC m 12 12 2012
and see 3 records get added to allPeople and it prints out 2 3 4 but the verify step doesn't complete without an error.
Is there some other condition I am not taking into account? Threading somehow?
package com.codegym.task.task17.task1711;
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<>();
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 Exception {
SimpleDateFormat sdfOut = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
SimpleDateFormat sdfIn = new SimpleDateFormat("M d yyyy", Locale.ENGLISH);
int id;
if (args.length < 2) return;
switch (args[0]) {
case "-i":
synchronized (allPeople) {
for (int i = 1; i < args.length; i++) {
id = Integer.parseInt(args[i]);
if (id < allPeople.size())
System.out.println(allPeople.get(id).getName() + " " + (Sex.MALE.equals(allPeople.get(id).getSex()) ? "m" : "f") + " " + sdfOut.format(allPeople.get(id).getBirthDate()));
}
break;
}
case "-d":
synchronized (allPeople) {
for (int i = 1; i < args.length; i++) {
id = Integer.parseInt(args[i]);
if (id < allPeople.size()) {
Person person = allPeople.get(id);
person.setBirthDate(null);
person.setSex(null);
person.setName(null);
allPeople.set(id, person);
}
}
break;
}
case "-u":
synchronized (allPeople) {
int argCount = 1;
while (argCount <= args.length) {
id = argCount;
String name = args[argCount+1];
String sex = args[argCount+2];
Date bd = sdfIn.parse(args[argCount+2] + " " + args[argCount+3] + " " + args[argCount+4]);
if (sex.equals("m")) {
allPeople.set(id, Person.createMale(name, bd));
}
else {
allPeople.set(id, Person.createFemale(name, bd));
}
}
break;
}
case "-c":
synchronized (allPeople) {
int argCount = 1;
while (argCount < args.length)
{
String name = args[argCount];
String sex = args[argCount+1];
Date bd = sdfIn.parse(args[argCount+2] + " " + args[argCount+3] + " " + args[argCount+4]);
if (sex.equals("m")) {
allPeople.add(Person.createMale(name, bd));
} else {
allPeople.add(Person.createFemale(name, bd));
}
System.out.println(allPeople.size() - 1);
argCount +=5;
}
break;
}
}
}
}