I've solver first CRUD ; The problem in second , I make the same logic with comment , but it not advantage;
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 {
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 ParseException {
SimpleDateFormat forParse = new SimpleDateFormat("MM dd yyyy" , Locale.ENGLISH);
SimpleDateFormat pattern = new SimpleDateFormat("MMM dd yyyy" , Locale.ENGLISH);
switch(args[0]){
case "-c" :
synchronized (allPeople) {
// Getting a name for first Person [args1]
String name1 = args[1];
// Getting a date for first Person [args3]
Date date1 = forParse.parse(args[3]);
// Checking sex of the Person
if (args[2].equals("m")){
// Add to the end of the list;
allPeople.add(Person.createMale(name1 , date1));
}
else{
// Add to the end of the list
allPeople.add(Person.createFemale(name1 , date1));
}
// Saving index for first guy;
int index1 = allPeople.size()-1;
// Display it
System.out.println(index1);
// Getting a name for second Person[args4]
String name2 = args[4];
// Getting a date for second Person[args4]
Date date2 = forParse.parse(args[6]);
// Checking sex of the Person
if (args[5].equals("m")){
// Add to the end of the list;
allPeople.add(Person.createMale(name2 , date2));
}
else{
allPeople.add(Person.createFemale(name2 , date2));
}
//Getting index of the last person
int index2 = allPeople.size()-1;
// Display it
System.out.println(index2);
}
break;
case "-u" :
synchronized (allPeople){
// Gettind the id of the first person;
int id1 = Integer.parseInt(args[1]);
// Getting this person by id
Person person1 = allPeople.get(id1);
// Set a name to this person
person1.setName(args[2]);
// Set a sex to this person
Sex sex1 = args[3] == "m" ? Sex.MALE : Sex.FEMALE;
person1.setSex(sex1);
// Set a birthday to this person
Date bd1 = forParse.parse(args[4]);
person1.setBirthDate(bd1);
// Getting the id of the second person;
int id2 = Integer.parseInt(args[5]);
// Getting second person by id
Person person2 = allPeople.get(id2);
// Set a name to this person
person2.setName(args[6]);
// Set a sex to second person
Sex sex2 = args[7] == "m" ? Sex.MALE : Sex.FEMALE;
person2.setSex(sex2);
// Set a birthday to second person
Date bd2 = forParse.parse(args[8]);
person2.setBirthDate(bd2);
}
break;
case "-d":
synchronized (allPeople){
for (int i = 1 ; i < args.length ; i++){
Person person = allPeople.get(i);
person.setName(null);
person.setSex(null);
person.setBirthDate(null);
}
}
break;
case "-i" :
synchronized (allPeople){
for (int i = 1 ; i < args.length ; i++){
Person person = allPeople.get(i);
System.out.print(person.getName() + " ");
String sex = person.getSex() == Sex.MALE ? "m" : "f";
System.out.print(sex + " ");
System.out.print(pattern.format(person.getBirthDate()));
}
}
break;
}
}
}