The output seems correct if I use "-i 1" in the Program arguments.
If I add a new person with "-c" etc etc, I get "2" as output, which is its index and what is supposed to happen, as far as I understood from the conditions.
Don't new people get added to the end of the list automatically?
package com.codegym.task.task17.task1710;
import java.text.*;
import java.util.*;
/*
CRUD
*/
public class Solution {
private static Person person;
private static Date bd;
private static int id;
static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
public static 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
if (args.length != 0) {
switch (args[0]) {
case "-c":
create(args[1], args[2], args[3]);
break;
case "-u":
update(args[1], args[2], args[3], args[4]);
break;
case "-d":
delete(args[1]);
break;
case "-i":
inform(args[1]);
break;
}
}
}
public static void create(String name, String sex, String date) throws Exception {
bd = sdf.parse(date);
if (sex.equals("m")) {
allPeople.add(person = Person.createMale(name, bd));
} else if (sex.equals("f")) {
allPeople.add(person = Person.createFemale(name, bd));
}
if (person != null) {
System.out.println(allPeople.indexOf(person));
}
}
public static void update(String ID, String name, String sex, String date) throws Exception {
id = Integer.parseInt(ID);
person = allPeople.get(id);
person.setName(name);
Sex s = sex.equals("m") ? Sex.MALE : Sex.FEMALE;
person.setSex(s);
person.setBirthDate(sdf.parse(date));
}
public static void delete(String ID) {
id = Integer.parseInt(ID);
person = allPeople.get(id);
person.setBirthDate(null);
person.setSex(null);
person.setName(null);
}
public static void inform(String ID) {
id = Integer.parseInt(ID);
person = allPeople.get(id);
sdf = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
String sex = person.getSex().equals(Sex.MALE) ? "m" : "f";
System.out.printf("%s %s %s\n", person.getName(), sex, sdf.format(person.getBirthDate()));
}
}