Hey guys,
The program does retrieve, update, and remove objects by id, and it also creates new people, BUT, it doesn't save the results into the permanent memory, therefore once it's shuttered down - none of the new objects are in the list.
How to fix it?
TYIA !
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.*;
/*
CRUD
*/
public class Solution {
public volatile 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 IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] input = reader.readLine().split(" ");
String command = input[0];
String dateString = null;
if (command.equals("-c")) {
dateString = input[3].replaceAll("\"", "") + " " + input[4] + " " + input[5]
.replaceAll("\"", "");
}
if (command.equals("-u")) {
dateString = input[4].replaceAll("\"", "") + " " + input[5] + " " + input[6]
.replaceAll("\"", "");
}
Date date = null;
if (input.length > 2) {
try {
SimpleDateFormat format = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH);
date = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
}
if (command.equals("-c") && input[2].equals("m")) {
allPeople.add(Person.createMale(input[1], date));
}
if (command.equals("-c") && input[2].equals("f")) {
allPeople.add(Person.createFemale(input[1], date));
}
if (command.equals("-u")) {
int id = Integer.parseInt(input[1]);
Person temp = allPeople.get(id);
String sex = input[3];
if (sex.equals("m")) {
temp = Person.createMale(input[2], date);
allPeople.remove(id);
allPeople.add(id, temp);
}
if (sex.equals("f")) {
temp = Person.createFemale(input[2], date);
allPeople.remove(id);
allPeople.add(id, temp);
}
}
if (command.equals("-d")) {
allPeople.remove(allPeople.get(Integer.parseInt(input[1])));
}
if (command.equals("-i")) {
System.out.println(allPeople.get(Integer.parseInt(input[1])).toString());
}
}
}
/*
-c Washington m "04 15 1990"
-u 1 Washington f "04 15 1990"
*/