package com.javarush.test.level17.lesson10.bonus01;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
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
CrUD - Create, Update, Delete
Программа запускается с одним из следующих наборов параметров:
-c name sex bd
-u id name sex bd
-d id
-i id
Значения параметров:
name - имя, String
sex - пол, "м" или "ж", одна буква
bd - дата рождения в следующем формате 15/04/1990
-с - добавляет человека с заданными параметрами в конец allPeople, выводит id (index) на экран
-u - обновляет данные человека с данным id
-d - производит логическое удаление человека с id
-i - выводит на экран информацию о человеке с id: name sex (м/ж) bd (формат 15-Apr-1990)
id соответствует индексу в списке
Все люди должны храниться в allPeople
Используйте Locale.ENGLISH в качестве второго параметра для SimpleDateFormat
Пример параметров: -c Миронов м 15/04/1990
*/
public class Solution {
public static List<Person> allPeople = new ArrayList<Person>();
static {
allPeople.add(Person.createMale("Иванов Иван", new Date())); //сегодня родился id=0
allPeople.add(Person.createMale("Петров Петр", new Date())); //сегодня родился id=1
}
public static void main(String[] args)
{
if (args.length < 2 || args.length == 3 || args.length > 6)
{
return;
}
String code = args[0];
try
{
if (code.equals("-c"))
{
create(args);
}
if (code.equals("-u"))
{
update(args);
}
if (code.equals("-d"))
{
delete(args);
}
if (code.equals("-i"))
{
info(args);
}
}catch (ParseException ignore) {/*NOP*/}
}
static void create(String[] args) throws ParseException
{
String name;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
if (args.length == 5) {
name = args[1] + " " + args[2];
}
else {
name = args[1];
}
if (args[args.length-2].equals("ж")){
allPeople.add(Person.createFemale(name, sdf.parse(args[args.length-1])));
}
if (args[args.length-2].equals("м")){
allPeople.add(Person.createMale(name, sdf.parse(args[args.length-1])));
}
System.out.println(allPeople.size()-1);
}
static void update(String[] args) throws ParseException
{
String name;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
int id = Integer.parseInt(args[1]);
if (args.length == 6) {
name = args[2] + " " + args[3];
}
else {
name = args[2];
}
if (args[args.length-2].equals("ж")){
allPeople.get(id).setName(name);
allPeople.get(id).setSex(Sex.FEMALE);
allPeople.get(id).setBirthDay(sdf.parse(args[args.length-1]));
}
if (args[args.length-2].equals("м")){
allPeople.get(id).setName(name);
allPeople.get(id).setSex(Sex.MALE);
allPeople.get(id).setBirthDay(sdf.parse(args[args.length-1]));
}
}
static void delete(String[] args)
{
int id = Integer.parseInt(args[1]);
allPeople.get(id).setName(null);
allPeople.get(id).setSex(null);
allPeople.get(id).setBirthDay(null);
}
static void info(String[] args)
{
SimpleDateFormat sdfPrint = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
int id = Integer.parseInt(args[1]);
String sex = "";
if (allPeople.get(id).getSex() == Sex.FEMALE)
{
sex = "ж";
}
if (allPeople.get(id).getSex() == Sex.MALE)
{
sex = "м";
}
System.out.println(allPeople.get(id).getName() + " " + sex + " " + sdfPrint.format(allPeople.get(id).getBirthDay()));
}
}