I think the below code works, but to be fair, i have tested it with the below arguments
-c Christian "m" 04 15 1990
is anyoane knowing how the input for this task is?
Thanks
package com.codegym.task.task17.task1711;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
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
ArrayList<ArrayList<String>> personAttributesAsSublists = attributesToSublists(args);
switch (args[0]) {
case "-c":
synchronized (allPeople) {
create(personAttributesAsSublists);
break;
}
case "-u":
synchronized (allPeople) {
update(personAttributesAsSublists);
break;
}
case "-d":
synchronized (allPeople) {
delete(args);
break;
}
case "-i":
synchronized (allPeople) {
info(args);
break;
}
}
}
public static void create(ArrayList<ArrayList<String>> arguments) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH);
String name, sex, month, day, year;
Date birthdate;
for(ArrayList<String> personAttributes : arguments) {
Person newPerson = null;
name = personAttributes.get(0).replace("\"", "");
sex = personAttributes.get(1).replace("\"", "");
month = personAttributes.get(2).replace("\"", "");
day = personAttributes.get(3).replace("\"", "");
year = personAttributes.get(4).replace("\"", "");
birthdate = formatter.parse(month + day + year);
if (sex.equals("m"))
newPerson = Person.createMale(name, birthdate);
else
newPerson = newPerson.createFemale(name, birthdate);
allPeople.add(newPerson);
}
}
public static void update(ArrayList<ArrayList<String>> arguments) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH);
int personIndex;
String name, sex, month, day, year;
Date birthdate;
for(ArrayList<String> personAttributes : arguments) {
personIndex = Integer.parseInt(personAttributes.get(0));
name = personAttributes.get(1).replace("\"", "");;
sex =personAttributes.get(2).replace("\"", "");
month =personAttributes.get(3).replace("\"", "");
day =personAttributes.get(4).replace("\"", "");
year =personAttributes.get(5).replace("\"", "");
Person personToUpdate = allPeople.get(personIndex);
personToUpdate.setName(name);
personToUpdate.setSex(sex.equals("m") ? Sex.MALE : Sex.FEMALE);
personToUpdate.setBirthDate(formatter.parse(month + day + year));
}
}
public static void delete (String[] arguments) {
Person personToDelete;
int index;
for (int i = 1; i < arguments.length; i++) {
index = Integer.parseInt(arguments[i]);
personToDelete = allPeople.get(index);
personToDelete.setName(null);
personToDelete.setSex(null);
personToDelete.setBirthDate(null);
}
}
public static void info (String[] arguments) {
int index;
for(int i = 1; i < arguments.length; i++) {
index = Integer.parseInt(arguments[i]);
System.out.println(allPeople.get(index));
}
}
public static ArrayList<ArrayList<String>> attributesToSublists (String[] arguments) {
ArrayList<ArrayList<String>> argumentsAsSublists = new ArrayList<>();
ArrayList<String> personAttributes = new ArrayList<>();
if (arguments[0].equals("-c")) {
for (int i = 1; i < arguments.length; i++) {
if (i % 5 != 0) {
personAttributes.add(arguments[i]);
} else {
personAttributes.add(arguments[i]);
ArrayList<String> copy = new ArrayList<>(personAttributes);
argumentsAsSublists.add(copy);
personAttributes.clear();
}
}
}
else if (arguments[0].equals("-u")) {
for (int i = 1; i < arguments.length; i++) {
if (i % 6 != 0) {
personAttributes.add(arguments[i]);
} else {
personAttributes.add(arguments[i]);
ArrayList<String> copy = new ArrayList<>(personAttributes);
argumentsAsSublists.add(copy);
personAttributes.clear();
}
}
}
return argumentsAsSublists;
}
}