My code works for all tests, but there is a problem.
According to requirements, the bd format is 04 15 1990 not "04 15 1990", so I had to go further with args[i+?].
Can anyone help me?
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 {
// Start here
switch (args[0]){
case "-c":
synchronized (allPeople){
for (int i = 1; i < args.length ; i=i+5) {
String data = args[i + 2] + " " + args[i + 3] + " " + args[i + 4];
c(args[i], args[i + 1], data);
}
}
break;
case "-u":
synchronized (allPeople){
for (int i = 1; i < args.length ; i=i+6) {
String data = args[i + 3] + " " + args[i + 4] + " " + args[i + 5];
u(Integer.parseInt(args[i]), args[i + 1], args[i + 2], data);
}
}
break;
case "-d":
synchronized (allPeople) {
for (int i = 1; i < args.length ; i++) {
d(Integer.parseInt(args[i]));
}
}
break;
case "-i":
synchronized (allPeople) {
for (int i = 1; i < args.length ; i++) {
i(Integer.parseInt(args[i]));
System.out.println("");
}
}
break;
}
}
public static void c(String name, String sex, String bd) throws ParseException {
if (sex.equals("m")) {
allPeople.add(Person.createMale(name, formater(bd)));
}else{
allPeople.add(Person.createFemale(name, formater(bd)));
}
System.out.println(allPeople.size()-1);
}
public static Date formater (String bd) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH);
Date semifinaldate = formatter.parse(bd);
return semifinaldate;
}
public static void u (int id, String name, String sex, String bd) throws ParseException {
allPeople.get(id).setName(name);
if (sex.equals("m")){allPeople.get(id).setSex(Sex.MALE);}
if (sex.equals("f")){allPeople.get(id).setSex(Sex.FEMALE);}
allPeople.get(id).setBirthDate(formater(bd));
}
public static void d (int id){
allPeople.get(id).setBirthDate(null);
allPeople.get(id).setName(null);
allPeople.get(id).setSex(null);
}
public static void i (int id){
System.out.print(allPeople.get(id).getName()+" ");
if(allPeople.get(id).getSex().equals(Sex.MALE)){
System.out.print("m ");
}
if(allPeople.get(id).getSex().equals(Sex.FEMALE)){
System.out.print("f ");
}
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy",Locale.ENGLISH);
String formatted = formatter.format(allPeople.get(id).getBirthDate());
System.out.print(formatted);
}
}