Hey guys, my program is working absolutely fine! I tried it using a while loop, so that I am able to do multiple inputs (e.g. creating a person, then updating the same one, in between displaying its information, deleting another person, etc.) and the output was correct.
But the last 4 requirements are still not fulfilled! What am I doing wrong?
Here is my console input and output:
-c Tim m 09 12 2000
2
-i 2
Tim m Sep 12 2000
-u 2 Tim f 05 14 2000
-i 2
Tim f May 14 2000
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 static List<Person> allPeople = new ArrayList<>();
public static List<String> parameters = new ArrayList<>();
public static SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH);
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 inputLine = "";
while (!(inputLine = reader.readLine()).equals("exit")) {
getInputParameters(inputLine);
action();
}
}
public static void getInputParameters(String inputLine) {
parameters = Arrays.asList(inputLine.split("[ ]"));
}
public static void action() {
switch (parameters.get(0)) {
case "-c":
createPerson();
break;
case "-u":
updatePerson();
break;
case "-d":
deletePerson();
break;
case "-i":
infoPerson();
break;
}
}
public static void createPerson() {
String name = parameters.get(1);
Date birthday = null;
try {
birthday = sdf.parse(parameters.get(3) + " " + parameters.get(4) + " " + parameters.get(5));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("Wrong format");
}
Person person;
if (parameters.get(2).equals("m")) person = Person.createMale(name, birthday);
else if (parameters.get(2).equals("f")) person = Person.createFemale(name, birthday);
else return;
allPeople.add(person);
System.out.println(allPeople.indexOf(person));
}
public static void updatePerson() {
int id = Integer.parseInt(parameters.get(1));
Person person = allPeople.get(id);
person.setName(parameters.get(2));
Date birthday = null;
try {
birthday = sdf.parse(parameters.get(4) + " " + parameters.get(5) + " " + parameters.get(6));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("Wrong format");
}
person.setBirthDate(birthday);
if (parameters.get(3).equals("m")) person.setSex(Sex.MALE);
else if (parameters.get(3).equals("f")) person.setSex(Sex.FEMALE);
else return;
allPeople.set(id, person);
}
public static void deletePerson() {
int id = Integer.parseInt(parameters.get(1));
Person person = allPeople.get(id);
person.setSex(null);
person.setName(null);
person.setBirthDate(null);
}
public static void infoPerson() {
int id = Integer.parseInt(parameters.get(1));
Person person = allPeople.get(id);
String output = "";
output += person.getName() + " ";
if (person.getSex() == Sex.MALE) output += "m ";
else output += "f ";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
output += dateFormat.format(person.getBirthDate());
System.out.println(output);
}
}