The only thing I can understand is when I "-d " a person and then ask for its infos, it "crashes" cuz of Birthday is null (and even there, I tried to make my program to ignore it but...it doesn't) .
So if someone can help me, it will be soooo great!
package com.codegym.task.task17.task1710;
import java.text.ParseException;
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 ParseException {
// Start here
Scanner sc = new Scanner(System.in);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH);
sdf.format(date);
// while(true) {
String commandLine = sc.nextLine();
/** params
* [0] = command
* [1] = name || id
* [2] = sex || name
* [3] = bd ||sex
* [4] = bd (si [0]==-u )
*/
String[] params = commandLine.split(" ");
String command = params[0];
if (command.equals("-c")) {
params[3] = params[3].replaceAll("\"", "");
params[3] += " " + params[4] + " " + params[5]; //Merge for get a valid format of date
}
else if (command.equals("-u")) {
params[4] = params[4].replaceAll("\"", "");
params[4] += " " + params[5] + " " + params[6];
}
switch (command) {
case "-c":
date = sdf.parse(params[3]);
Person newPerson = create(params[1], params[2].charAt(0), date);
allPeople.add(newPerson);
System.out.println(allPeople.size()-1);
// System.err.println(allPeople.get(2).toString());
break;
case "-u":
date = sdf.parse(params[4]);
update(Integer.parseInt(params[1]), params[2], Sex.valueOf(params[3]), date);
break;
case "-d":
update(Integer.parseInt(params[1]), null, null, null);
System.err.println(allPeople.size());
break;
case "-i":
giveInfos(Integer.parseInt(params[1]));
System.err.println("Infos données");
break;
default:
return;
}
//}
}
public static Person create(String name, char sex, Date bd){
Person personne=null;
switch (sex){
case 'm': return Person.createMale(name, bd);
case 'f': return Person.createFemale(name, bd);
}
return personne;
}
public static void update(int id, String name, Sex sex, Date bd){
Person personToUpdate = allPeople.get(id);
//Update Name
personToUpdate.setName(name);
//Update sex
if (sex != null) {
char s = sex.toString().toLowerCase().charAt(0);
if (s == 'm')
personToUpdate.setSex(Sex.MALE);
else if (s =='f')
personToUpdate.setSex(Sex.FEMALE);
}else personToUpdate.setSex(null);
//Update BirthDate
personToUpdate.setBirthDate(bd);
allPeople.set(id,personToUpdate);
System.err.println(allPeople);
// allPeople.remove(id);
}
public static void giveInfos(int id){
try {
Person target = allPeople.get(id);
String[] birthDay=null;
String infos;
if (target.getBirthDate()!=null) {
birthDay = target.getBirthDate().toString().split(" ");
birthDay[0] = birthDay[1] + " " + birthDay[2] + " " + birthDay[5];
}
else
System.err.println("Birthday is null");
infos= target.getName() + " " + target.getSex().toString().toLowerCase().charAt(0) + " " + (target.getBirthDate() != null ? Objects.requireNonNull(birthDay)[0] : "null");
System.out.println(infos);
System.out.println("= Washington m Apr 15 1990 ? " + infos.equals("Washington m Apr 15 1990"));
//System.out.println(allPeople.get(id).toString());
}catch (IndexOutOfBoundsException e){
System.err.println("There's nobody at this index yet.");
}
}
}