It says that on line 33 , 35, 38 args[1] is not an int. I have to change it to string.
But id argument given as int right?
I tried it with, Integer.parseInt(args[1]), It compiles but fails the tests.
Help is very much appreciated.
Thanks.
package com.codegym.task.task17.task1710;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/*
CRUD
*/
public class Solution {
public static List<Person> allPeople = new ArrayList<>();
static SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy", Locale.ENGLISH);
public static Date bd;
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 {
switch (args[0]) {
case "-c":
// parse the String to date object
bd = sdf.parse(args[3]);
addPerson(args[1], args[2], bd);
case "-u":
bd = sdf.parse(args[4]);
updatePerson(Integer.parseInt(args[1]), args[2], args[3], bd);
case "-d":
deletePerson(Integer.parseInt(args[1]));
case "-i":
displayPerson(Integer.parseInt(args[1]));
}
}
public static void addPerson(String name, String sex, Date bd) {
if (sex.equals("m")) {
allPeople.add(Person.createMale(name, bd));
} else
allPeople.add(Person.createFemale(name, bd));
System.out.println(allPeople.size());
}
public static void updatePerson(int id, String name, String sex, Date bd) {
allPeople.get(id).setName(name);
if (sex.equals("m")) {
allPeople.get(id).setSex(Sex.MALE);
} else allPeople.get(id).setSex(Sex.FEMALE);
allPeople.get(id).setBirthDate(bd);
}
public static void deletePerson(int id) {
// Replaces all data with null
allPeople.get(id).setName(null);
allPeople.get(id).setSex(null);
allPeople.get(id).setBirthDate(null);
}
public static void displayPerson(int id) {
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
String gender = "m";
if (allPeople.get(id).getSex().equals(Sex.FEMALE)) gender = "f";
//You have to format the birthday first..
System.out.println(allPeople.get(id).getName() + " " + gender + " " + outputFormat.format(allPeople.get(id).getBirthDate()));
}
}