I can't attach my solution, as it's already been completed, but I have a question about synchronization. I made my synchronized blocks include only the lines that somehow referred to the allPeople list (and they were present in every case label of the switch block, according to the task). But in the correct solution all the code in each case label is wrapped into a synchronization block (not only the chosen lines). My solution was accepted by the validator, but I'm still in doubt how much of the code I'd need to include into such blocks ideally.
For instance, what is the difference between number 1 and number 2:
number 1
case "-c":
for (int i = 1; i < args.length; i += 3) {
Person newby = null;
String name = args[i];
String gender = args[i+1].toLowerCase();
Date birthDate = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(args[i+2]);
if (gender.startsWith("m")) newby = Person.createMale(name, birthDate);
else newby = Person.createFemale(name, birthDate);
synchronized (allPeople) {
allPeople.add(newby);
System.out.println(allPeople.indexOf(newby));
}
}
break;
number 2
case "-c":
synchronized (allPeople) {
for (int i = 1; i < args.length; i += 3) {
Person newby = null;
String name = args[i];
String gender = args[i+1].toLowerCase();
Date birthDate = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(args[i+2]);
if (gender.startsWith("m")) newby = Person.createMale(name, birthDate);
else newby = Person.createFemale(name, birthDate);
allPeople.add(newby);
System.out.println(allPeople.indexOf(newby));
}
break;