-c: Be sure that the ids of the added people are displayed on the screen.
-u: Be sure that the size of the allPeople list does not change when people's data is updated.
-i: Be sure that the person's birth date (format: Apr 15 1990) is displayed.
package com.codegym.task.task17.task1711;
import java.io.IOException;
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 SimpleDateFormat sdf = new SimpleDateFormat("Mmm DD YYYY", 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 Exception {
// Start here
switch(args[0]){
case("-c"):
synchronized (allPeople){
for (int i = 1; i < args.length-2; i+=3){
String name = args[i];
String sex = (args[i+1]);
Date bd = sdf.parse(args[i+2]);
if (sex.equals("f")) {
allPeople.add(Person.createFemale(name, bd));
}else if (sex.equals("m")){
allPeople.add(Person.createMale(name, bd));
}
System.out.println(allPeople.get(allPeople.size()-1));
}
}
break;
case("-u"):
synchronized (allPeople){
for (int i = 1; i < args.length-3; i+=4) {
int id = Integer.parseInt(args[i]);
String name = args[i + 1];
String sex = (args[i + 2]);
Date bd = sdf.parse(args[i + 3]);
allPeople.get(i + 1).setName(name);
allPeople.get(i + 2).setSex(sex.equals("f") ? Sex.FEMALE : Sex.MALE);
allPeople.get(i + 3).setBirthDate(bd);
}
}
break;
case("-d"):
synchronized (allPeople){
for (int i = 1; i < args.length; i++){
int number = Integer.parseInt(args[i]);
allPeople.get(number).setName(null);
allPeople.get(number).setSex(null);
allPeople.get(number).setBirthDate(null);
}
}
break;
case("-i"):
synchronized (allPeople){
for (int i = 1; i < args.length; i++) {
int number = Integer.parseInt(args[i]);
System.out.println(allPeople.get(number).getName() + " " + (allPeople.get(number).getSex()==Sex.FEMALE?"f" : "m") + " " + allPeople.get(number).getBirthDate());
}
}
break;
}
}
}