Hi,
Tried using different inputs to debug but all seems ok. Sometimes wish that codegym would show you what situations your code is failing at so that you can work on it
package com.codegym.task.task17.task1710;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
CRUD
Argument values:
name (String)
sex ("m" or "f")
bd (birth date in the following format: "04 15 1990")
-c (adds a person with the specified arguments to the end of allPeople; displays the id on the screen)
-u (updates the data of the person with the specified id)
-d (performs the logical deletion of the person with the specified id; replaces all of its data with null)
-i (displays information about the person with the specified id: name sex (m/f) bd (format Apr 15 1990))
The id corresponds to the index in the list.
All the people should be stored in allPeople.
Use Locale.ENGLISH as the second argument for SimpleDateFormat.
*/
public class Solution {
public static List<Person> allPeople = new ArrayList<>();
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static String input;
static {
allPeople.add(Person.createMale("Donald Chump", new Date())); // id=0
allPeople.add(Person.createMale("Larry Gates", new Date())); // id=1
try {
while ((input = reader.readLine()) != null){
String[] arr = input.split(" ");
switch (arr[0]) {
case "-c":
addPersonToList(arr);
break;
case "-u":
update(arr);
break;
case "-d":
logicalDeletion(arr);
break;
case "-i":
displayInfo(arr);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// Start here
}
public static void addPersonToList(String[] arr){ //arr[0] is command,[1 to x] is name, [sexIndex] is sex, [sexIndex+1] to [sexIndex+3] is DOB
int sexIndex = 2;
while (!(arr[sexIndex].equals("m") || arr[sexIndex].equals("f"))){
sexIndex++;
}
StringBuilder newName = new StringBuilder();
for (int i = 1; i < sexIndex; i++){
newName.append(arr[i]);
newName.append(" ");
}
newName.append("\b");
Sex sex = arr[sexIndex].equals("m") ? Sex.MALE : Sex.FEMALE;
String birthday = arr[sexIndex + 1] + " " + arr[sexIndex + 2] + " " + arr[sexIndex + 3];
birthday = birthday.replace("\"", "");
Date birthDate = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM dd yyyy");
try {
birthDate = simpleDateFormat.parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
Person person = Person.createMale(null,null);
person.setBirthDate(birthDate);
person.setSex(sex);
person.setName(newName.toString());
allPeople.add(person);
System.out.println(person);
}
public static void update(String[] arr){ //arr[0] is command, [1] is index, [2 - x] is name, [sexIndex] is sex, [sexIndex+1] to [sexIndex+3] is DOB
int index = Integer.parseInt(arr[1]);
Person person = allPeople.get(index);
int sexIndex = 3;
while (!(arr[sexIndex].equals("m") || arr[sexIndex].equals("f"))){
sexIndex++;
}
StringBuilder newName = new StringBuilder();
for (int i = 2; i < sexIndex; i++){
newName.append(arr[i]);
newName.append(" ");
}
newName.append("\b");
person.setName(newName.toString());
if (arr[sexIndex].equals("m")){
person.setSex(Sex.MALE);
} else {
person.setSex(Sex.FEMALE);
}
String birthday = arr[sexIndex + 1] + " " + arr[sexIndex + 2] + " " + arr[sexIndex + 3];
birthday = birthday.replace("\"", "");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM dd yyyy");
try {
person.setBirthDate(simpleDateFormat.parse(birthday));
} catch (ParseException e) {
e.printStackTrace();
}
allPeople.set(index,person);
}
public static void logicalDeletion(String[] arr){
int index = Integer.parseInt(arr[1]);
Person nullPerson = allPeople.get(index);
nullPerson.setName(null);
nullPerson.setSex(null);
nullPerson.setBirthDate(null);
allPeople.set(index, nullPerson);
}
public static void displayInfo(String[] arr){
int index = Integer.parseInt(arr[1]);
Person person = allPeople.get(index);
System.out.println(person);
}
}