Skip to content

Instantly share code, notes, and snippets.

@josinSbazin
Created July 6, 2016 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josinSbazin/8b43630505cdc0282249f0650fcf21a9 to your computer and use it in GitHub Desktop.
Save josinSbazin/8b43630505cdc0282249f0650fcf21a9 to your computer and use it in GitHub Desktop.
level17.lesson10.bonus02;
package com.javarush.test.level17.lesson10.bonus02;
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 2
CrUD Batch - multiple Creation, Updates, Deletion
!!!РЕКОМЕНДУЕТСЯ выполнить level17.lesson10.bonus01 перед этой задачей!!!
Программа запускается с одним из следующих наборов параметров:
-c name1 sex1 bd1 name2 sex2 bd2 ...
-u id1 name1 sex1 bd1 id2 name2 sex2 bd2 ...
-d id1 id2 id3 id4 ...
-i id1 id2 id3 id4 ...
Значения параметров:
name - имя, String
sex - пол, "м" или "ж", одна буква
bd - дата рождения в следующем формате 15/04/1990
-с - добавляет всех людей с заданными параметрами в конец allPeople, выводит id (index) на экран в соответствующем порядке
-u - обновляет соответствующие данные людей с заданными id
-d - производит логическое удаление всех людей с заданными id
-i - выводит на экран информацию о всех людях с заданными id: name sex bd
id соответствует индексу в списке
Формат вывода даты рождения 15-Apr-1990
Все люди должны храниться в allPeople
Порядок вывода данных соответствует вводу данных
Обеспечить корректную работу с данными для множества нитей (чтоб не было затирания данных)
Используйте Locale.ENGLISH в качестве второго параметра для SimpleDateFormat
*/
public class Solution {
public static volatile 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) throws Exception {
if (args.length>1)
{
String command = args[0];
if (command.equals("-c") && (args.length - 1) % 3 == 0)
{
for (String[] temp : separateC(args))
{
System.out.println(allPeople.indexOf(create(temp)));
}
} else if (command.equals(("-u")) && (args.length - 1) % 4 == 0)
{
for (String[] temp : separateU(args))
{
update(temp);
}
} else if (command.equals("-d"))
{
for (int i = 1; i < args.length; i++)
{
delete(args[i]);
}
} else if (command.equals("-i"))
{
for (int i = 1; i < args.length; i++)
{
info(args[i]);
}
}
}
}
public synchronized static Person create (String [] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
Date date = format.parse(args[2]);
Person person = args[1].equals("м") ? Person.createMale(args[0], date) : Person.createFemale(args[0], date);
allPeople.add(person);
return person;
}
public static synchronized void update (String [] args) throws ParseException {
int ID = Integer.parseInt(args[0]);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
String name = args[1];
String sex = args[2];
Sex SEX = sex.equals("м")?Sex.MALE:Sex.FEMALE;
Date date = format.parse(args[3]);
Person person = allPeople.get(ID);
person.setName(name);
person.setSex(SEX);
person.setBirthDay(date);
}
public static synchronized void delete (String id) {
int ID = Integer.parseInt(id);
Person person = allPeople.get(ID);
person.setName(null);
person.setSex(null);
person.setBirthDay(null);
}
public static synchronized void info (String id) {
SimpleDateFormat format = new SimpleDateFormat("d-MMM-yyyy", Locale.ENGLISH);
int ID = Integer.parseInt(id);
Person person = allPeople.get(ID);
String name = person.getName();
String sex = person.getSex()==Sex.MALE?"м":"ж";
String bd = format.format(person.getBirthDay());
System.out.println(name+" "+sex+" "+bd);
}
public static synchronized String [][] separateC (String [] args) {
int col = (args.length-1)/3;
String [][] result = new String[col][3];
for (int i = 1, j = 0; i < args.length; i+=3, j++) {
result[j][0] = args[i];
result[j][1] = args[i+1];
result[j][2] = args[i+2];
}
return result;
}
public static synchronized String [][] separateU (String [] args) {
int col = (args.length-1)/4;
String [][] result = new String[col][4];
for (int i = 1, j = 0; i < args.length; i+=4, j++) {
result[j][0] = args[i];
result[j][1] = args[i+1];
result[j][2] = args[i+2];
result[j][3] = args[i+3];
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment