Cannot verify 3-6, i think i did it right tho?
package com.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 volatile int index = 1;
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) {
// Start here
try {
String pattern1 = "MM dd yyyy";
SimpleDateFormat sf1 = new SimpleDateFormat(pattern1, Locale.US);
String pattern2 = "MMM dd yyyy";
SimpleDateFormat sf2 = new SimpleDateFormat(pattern2, Locale.US);
switch (args[0]){
case "-c":
synchronized (allPeople){
while(index+3 <args.length+1) {
if (args[index + 1].equals("m")) {
Person newPerson = Person.createMale(args[index], sf1.parse(args[index+2]));
allPeople.add(newPerson);
System.out.println(allPeople.indexOf(newPerson));
index +=3;
} else if (args[index+1].equals("f")) {
Person newPerson = Person.createFemale(args[index], sf1.parse(args[index+2]));
allPeople.add(newPerson);
System.out.println(allPeople.indexOf(newPerson));
index +=3;
}
}}
break;
case "-u":
synchronized(allPeople) {
while (index + 3 < args.length + 1) {
if (args[index+2].equals("m")) {
allPeople.set(Integer.valueOf(args[index]),
Person.createMale(args[index+1], sf1.parse(args[index+3])));
index += 3;
} else if (args[index+2].equals("f")) {
allPeople.set(Integer.valueOf(args[index]),
Person.createFemale(args[index+1], sf1.parse(args[index+3])));
index += 3;
}
}
}
break;
case "-d":
synchronized (allPeople) {
for (String s : args) {
allPeople.get(Integer.valueOf(s)).setBirthDate(null);
allPeople.get(Integer.valueOf(s)).setName(null);
allPeople.get(Integer.valueOf(s)).setSex(null);
}
}
break;
case "-i":
synchronized (allPeople) {
for (String s : args) {
Person p = allPeople.get(Integer.valueOf(s));
System.out.print(p.getName() + " " + (p.getSex() == (Sex.MALE) ? "m" : "f") + " " + sf2.format(p.getBirthDate()));
}
}
break;
}
}catch(ParseException e){
e.printStackTrace();
}
}
}