I do not know why I fail the test.
The output is correct just not very clean code but it does work. So how to go from here?
package com.codegym.task.task17.task1710;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.*;
/*
CRUD
*/
public class Solution {
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 {
/*-c name sex bd
-u id name sex bd
-d id
-i id*/
String name;
Sex sex;
Date bd;
int id;
Scanner scanner = new Scanner(System.in);
String s = "";
String rest = "";
if (scanner.hasNext()) {
s = scanner.next();//reads the first word
rest = scanner.nextLine();//reads the rest of the line
}
if (s.equals("-c")) {
//how to split up in separate arguments?
String restTrimmed = rest.trim().replace("\"", "");//get rid of leading space and ""
String[] arguments = restTrimmed.split("\\s", 3);//split into separate arguments
//the third string should be transformed into a Date
bd = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(arguments[2]);
if (arguments[1].equals("m")) {
Person m1 = Person.createMale(arguments[0], bd);
allPeople.add(m1);
System.out.println(allPeople.indexOf(m1));
} else {
allPeople.add(Person.createFemale(arguments[0], bd));
Person v1 = Person.createFemale(arguments[0], bd);
allPeople.add(v1);
System.out.println(allPeople.indexOf(v1));
}
} else if (s.equals("-u")) {
/*-u id name sex bd*/
String restTrimmed = rest.trim().replace("\"", "");//get rid of leading space and ""
String[] arguments = restTrimmed.split("\\s", 4);//split into separate arguments
id = Integer.parseInt(arguments[0]);
name = arguments[1];
sex = Sex.valueOf(arguments[2]);
bd = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH).parse(arguments[3]);
Person p = allPeople.get(id);
p.setName(name);
p.setSex(sex);
p.setBirthDate(bd);
} else if (s.equals("-d")) {
id = Integer.parseInt(rest.trim());
allPeople.remove(id);
} else if (s.equals("-i")) {
id = Integer.parseInt(rest.trim());
System.out.println(allPeople.get(id).toString());
}
}
}