Don't know why 3d and 4th conditions don't pass. Code is working ok.
package com.codegym.task.task17.task1710;
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
*/
public class Solution {
public static List<Person> allPeople = new ArrayList<>();
static String choice;
static String name = "";
static Sex sex;
static String date;
static int mfIndex = 0;
static Date birthDate;
static int id;
static Person person;
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) {
if (args[0].equals("-d") || args[0].equals("-i")){
id = Integer.parseInt(args[1]); // assign value to the id variable if param is -d or -i
} else {
parseNameAndSex(args); // I parse name and sex here from args and assign it to String name because
parseBD(args); // I parse birthday here
}
choice = args[0];
switch (choice) {
case "-c": {
if (sex.equals(Sex.MALE)) {person = Person.createMale(name, birthDate); allPeople.add(person);}
else { person = Person.createFemale(name, birthDate); allPeople.add(person);}
System.out.println(allPeople.indexOf(person));
break;
}
case "-u": {
person = allPeople.get(id);
person.setName(name);
person.setSex(sex);
person.setBirthDate(birthDate);
break;
}
case "-d": {
//allPeople.set(id, null);
(allPeople.get(id)).setName(null);
(allPeople.get(id)).setSex(null);
(allPeople.get(id)).setBirthDate(null);
break;
}
case "-i": {
allPeople.get(id).getInfo();
}
}
}
public static void parseNameAndSex(String[] params) {
for (int i = 0; i < params.length; i++) { //get position and value of sex and assign value to Sex sex
if (params[i].equals("m")) {
sex = Sex.MALE;
mfIndex = i;
break;
} else if (params[i].equals("f")) {
sex = Sex.FEMALE;
mfIndex = i;
break;
}
}
if (params[0].equals("-c")) {
for (int i = 1; i < mfIndex; i++) // compose String name value using sex position :))
name += params[i] + " ";
} else if (params[0].equals("-u")) {
id = Integer.parseInt(params[1]); // assign value to the id variable if param is -u
for (int i = 2; i < mfIndex; i++) // compose String name value using sex position :)) but shift 1 index
name += params[i] + " "; // because we have id if parameter is -u
}
name = name.trim();
}
public static void parseBD(String[] params) {
date = params[mfIndex+1];
try {
birthDate = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}