I have a problem with this:
1. After running the program with the -c argument, the program should add the person with the specified arguments to the end of the allPeople list and display the id (index).
Suggestion: Make sure the person is actually added to the allPeople list
2. After running the program with the -u argument, the program must update the data of a person with the specified id in the allPeople list.
Suggestion: Make sure the size of the allPeople list does not change when updating a person's details.
package pl.codegym.task.task17.task1710;
import java.io.*;
import java.net.*;
import java.util.*;
import java.nio.*;
import java.rmi.*;
import java.text.*;
import java.util.Map.Entry;
import java.io.FileInputStream;
/*
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
{
//Zacznij tutaj
// try
// {
//SimpleDateFormat date = new SimpleDateFormat("d/MM/y");
switch (args[0])
{
case "-c":
//coś tam napisane
/* args[1] = name;
args[2] = sex;
args[3] = bd; */
addPerson(args);
break;
case "-u":
//coś tam napisane
/* args[1] = id;
args[2] = name;
args[3] = sex;
args[4] = bd; */
// if(args[3].equals("m"))
// {
// allPeople.set(Integer.parseInt(args[1]), Person.createMale(args[2], date.parse(args[4])));
// }
// else
// {
// allPeople.set(Integer.parseInt(args[1]), Person.createFemale(args[2], date.parse(args[4])));
// }
updatePerson(args);
break;
case "-d":
//coś tam napisane
// args[1] = id;
delPerson(args);
break;
case "-i":
//coś tam napisane
// args[1] = id;
printPerson(args);
break;
}
// }
// catch(ArrayIndexOutOfBoundsException e)
// {
// e.getMessage();
// }
}
public static void addPerson(String[] args) throws ParseException
{
String name = args[1];
String sex = args[2];
String date = args[3];
Date data = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(date);
if(sex.equals("m"))
{
allPeople.add(Person.createMale(name, data));
}
else if(sex.equals("f"))
{
allPeople.add(Person.createFemale(name, data));
}
System.out.println(allPeople.size() - 1);
}
public static void updatePerson(String[] args) throws ParseException
{
int id = Integer.parseInt(args[1]);
String name = args[2];
String sex = args[3];
Date data = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(args[4]);
allPeople.get(id).setName(name);
if (sex.equals("m"))
allPeople.get(id).setSex(Sex.MALE);
else if (sex.equals("f"))
allPeople.get(id).setSex(Sex.FEMALE);
allPeople.get(id).setBirthDate(data);
// SimpleDateFormat date=new SimpleDateFormat("MMM dd yyyy");
// if(sex.equals("m"))
// {
// allPeople.set(Integer.parseInt(id), Person.createMale(name, date.parse(bd)));
// }
// else
// {
// allPeople.set(Integer.parseInt(id), Person.createFemale(name, date.parse(bd)));
// }
}
public static void delPerson(String[] args)
{
int id = Integer.parseInt(args[1]);
//Person osoba = allPeople.get(Integer.parseInt(id));
allPeople.get(id).setBirthDate(null);
allPeople.get(id).setName(null);
allPeople.get(id).setSex(null);
}
public static void printPerson(String[] args)
{
//SimpleDateFormat newDate = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
DateFormat newDate = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
Person osoba1 = allPeople.get(Integer.parseInt(args[1]));
String plec = "";
if(osoba1.getSex().equals(Sex.MALE))
{
plec = "m";
}
else
{
plec = "f";
}
System.out.println(osoba1.getName() + " " + plec + " " + newDate.format(osoba1.getBirthDate()));
}
}