I try to run it with arguments and it basically gives me an array out of bounds exception on the switch statement. I don't know what else to do.
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;
/*
CRUD 2
*/
public class Solution {
public static volatile List<Person> allPeople = new ArrayList<>();
public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM dd yyyy");
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) {
int nameIndex = 1;
while (nameIndex < args.length) {
Person person;
if (args[nameIndex + 1].equals("m")) person = Person.createMale(args[nameIndex],
simpleDateFormat.parse(args[nameIndex + 2]));
else person = Person.createFemale(args[nameIndex], simpleDateFormat.parse(args[nameIndex + 2]));
allPeople.add(allPeople.size() - 1, person);
System.out.println(allPeople.indexOf(person));
nameIndex += 3;
}
}
case "-u":
synchronized (allPeople) {
int idIndex = 1;
while (idIndex < args.length) {
Person person;
int index = Integer.parseInt(args[idIndex]);
if (args[idIndex + 2].equals("m")) person = Person.createMale(args[idIndex + 1],
simpleDateFormat.parse(args[idIndex + 3]));
else person = Person.createFemale(args[idIndex + 1], simpleDateFormat.parse(args[idIndex + 3]));
allPeople.set(index, person);
idIndex += 4;
}
break;
}
case "-d":
synchronized (allPeople) {
int idIndex = 1;
while (idIndex < args.length) {
Person person;
int index = Integer.parseInt(args[idIndex]);
person = allPeople.get(index);
person.setBirthDate(null);
person.setName(null);
person.setSex(null);
allPeople.set(index, person);
idIndex++;
}
break;
}
case "-i":
synchronized (allPeople) {
simpleDateFormat.applyPattern("MMM dd yyyy");
int idIndex = 1;
while (idIndex < args.length) {
Person person;
int index = Integer.parseInt(args[idIndex]);
person = allPeople.get(index);
if (person.getSex() == Sex.MALE) System.out.println(person.getName() + " m "
+ simpleDateFormat.format(person.getBirthDate()));
else
System.out.println(person.getName() + " f " + simpleDateFormat.format(person.getBirthDate()));
idIndex++;
}
}
}
}
}