What is the purpose of using the synchronnized blocks here ? because i don't see multithreads . The program has only a one thread : the main thread !
possible explanation :
passing argument from the console for more than one person is considered as a multithreading and the excecution of code should be done for each of the persons , but i still don't see the need of doing that . a while loop instead would accomplish the purpose of the program :
package de.codegym.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<>();
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) {
// Hier beginnen
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
String name = "";
Sex sex = null;
Date bdd = null;
Person aktualisiertePerson = null;
int numberOfPeople = 0;
int counter = 0;
// Hier beginnen
switch (args[0]){
case "-c" :
numberOfPeople = (args.length-1) / 3;
while (counter<numberOfPeople){
name = args[counter+1];
sex = args[counter+2].charAt(0)== 'm' ? Sex.MALE : Sex.FEMALE ;
try {
bdd = simpleDateFormat.parse(args[counter+3]);
} catch (ParseException e){
e.printStackTrace();
}
allPeople.add(new Person(name,sex,bdd));
System.out.println(allPeople.size()-1);
counter++;
}
break;
case "-u" :
numberOfPeople = (args.length-1) / 4;
while (counter<numberOfPeople){
//Person personZuaktualisieren = allPeople.get(Integer.parseInt(args[1]));
name = args[counter+2];
sex = args[counter+2].charAt(0)== 'm' ? Sex.MALE : Sex.FEMALE ;
try {
bdd = simpleDateFormat.parse(args[counter+4]);
} catch (ParseException e){
e.printStackTrace();
}
aktualisiertePerson= new Person(name,sex,bdd);
allPeople.set(Integer.parseInt(args[counter+1]),aktualisiertePerson);
counter++;
}
break;
case "-d" :
numberOfPeople = (args.length-1) ;
while (counter<numberOfPeople){
name = null;
sex = null;
bdd = null;
aktualisiertePerson = new Person(name,sex,bdd);
allPeople.set(Integer.parseInt(args[counter+1]),aktualisiertePerson);
counter++;
}
break;
case "-i" :
numberOfPeople = (args.length-1) ;
while (counter<numberOfPeople){
Person person = allPeople.get(Integer.parseInt(args[counter+1]));
System.out.println(""+person.getName()+" "+(person.getSex() == Sex.MALE ? "m" : "f")+ " " +
simpleDateFormat2.format(person.getBirthDate()));
counter++;
}
break;
}
}
}