Please help.
I am totally lost. I checked everything in debugger and it seem to run as expected. incorrect arguments from the end of args table are ignored as incomplete to update/create person.
Where is the problem to pass the validation?
package com.codegym.task.task17.task1711;
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 Exception {
// Start here
SimpleDateFormat sdfOut = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
SimpleDateFormat sdfIn = new SimpleDateFormat("M d yyyy", Locale.ENGLISH);
int id;
if (args.length < 2) return;
switch (args[0]) {
case "-i":
synchronized (allPeople) {
for (int i = 1; i < args.length; i++) {
id = Integer.parseInt(args[i]);
if (id < allPeople.size())
System.out.println(allPeople.get(id).getName() + " " + (Sex.MALE.equals(allPeople.get(id).getSex()) ? "m" : "f") + " " + sdfOut.format(allPeople.get(id).getBirthDate()));
}
break;
}
case "-d":
synchronized (allPeople) {
for (int i = 1; i < args.length; i++) {
id = Integer.parseInt(args[i]);
if (id < allPeople.size()) {
Person person = allPeople.get(id);
person.setBirthDate(null);
person.setSex(null);
person.setName(null);
allPeople.set(id, person);
}
}
break;
}
case "-u":
synchronized (allPeople) {
if (args.length < 7) return;
for (int i = 1; i < args.length; i += 6) {
if (i + 6 > args.length) break;// to few arguments for last person
id = Integer.parseInt(args[i]);
if (id < allPeople.size()) {
if (args[i + 2].equals("m"))
allPeople.set(id, Person.createMale(args[i + 1], sdfIn.parse(args[i + 3] + " " + args[i+4] + " " + args[i+5])));
else allPeople.set(id, Person.createFemale(args[i + 1], sdfIn.parse
(args[i + 3] + " " + args[i+4] + " " + args[i+5])
));
}
}
break;
}
case "-c":
synchronized (allPeople) {
if (args.length < 6) return;
for (int i = 1; i < args.length; i += 5) {
if (i + 5 > args.length) break;// to few arguments for last person
if (args[i + 1].equals("m"))
allPeople.add(Person.createMale(args[i], sdfIn.parse(args[i + 2] + " " + args[i+3] + " " + args[i+4])));
else allPeople.add(Person.createFemale(args[i], sdfIn.parse(args[i + 2] + " " + args[i+3] + " " + args[i+4])));
System.out.println(allPeople.size() - 1);
}
break;
}
}
}
}