Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 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 ParseException, NumberFormatException, NullPointerException{
- //start here - начни тут
- if (args[0].equals("-c")) {
- create(args);
- }
- if (args[0].equals("-u")) {
- update(args);
- }
- if (args[0].equals("-d")) {
- delete(args);
- }
- if (args[0].equals("-i")) {
- inform(args);
- }
- }
- /*------------------------CREATE---------------------------------*/
- public static synchronized void create (String[]myArgs) throws ParseException, NumberFormatException{
- SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
- Person tempPers=null;
- int argIndex = 1;
- for (int i = 0; i < ((myArgs.length)-1)/3; i++) {
- if(myArgs[argIndex+1].equals("м")) {//был индекс 2
- tempPers= Person.createMale(myArgs[argIndex], sdf.parse(myArgs[argIndex+2]));//был индекс 3
- allPeople.add(tempPers);
- }
- if(myArgs[argIndex+1].equals("ж")) {//был индекс 2
- tempPers= Person.createFemale(myArgs[argIndex], sdf.parse(myArgs[argIndex+2]));//был индекс 3
- allPeople.add(tempPers);
- }
- System.out.println(allPeople.indexOf(tempPers));
- tempPers=null;
- argIndex=argIndex+3;
- }
- }
- /*------------------------UPDATE---------------------------------*/
- public static synchronized void update (String[]myArgs) throws ParseException, NullPointerException, ArrayIndexOutOfBoundsException{
- SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
- int argIndex = 1;
- for (int i = 0; i < ((myArgs.length)-1)/4; i++) {
- Person tempPers=allPeople.get(Integer.parseInt(myArgs[argIndex]));//1
- if(myArgs[argIndex+2].equals("м")) {//inex 3
- tempPers.setName(myArgs[argIndex+1]);//index2
- tempPers.setSex(Sex.MALE);
- tempPers.setBirthDay(sdf.parse(myArgs[argIndex+3]));//index 4
- }
- else if(myArgs[3].equals("ж")) {//3
- tempPers.setName(myArgs[argIndex+1]);//2
- tempPers.setSex(Sex.FEMALE);
- tempPers.setBirthDay(sdf.parse(myArgs[argIndex+3]));//4
- }
- allPeople.set(Integer.parseInt(myArgs[argIndex]),tempPers);//1
- argIndex=argIndex+4;
- }
- }
- /*------------------------DELETE---------------------------------*/
- public static synchronized void delete (String[]myArgs) {
- int argIndex = 1;
- for (int i = 0; i < ((myArgs.length)-1)/1; i++) {
- allPeople.get(Integer.parseInt(myArgs[argIndex])).setName(null);
- allPeople.get(Integer.parseInt(myArgs[argIndex])).setSex(null);
- allPeople.get(Integer.parseInt(myArgs[argIndex])).setBirthDay(null);
- argIndex = argIndex + 1;
- }
- }
- /*------------------------INFORM---------------------------------*/
- public static synchronized void inform (String[]myArgs) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
- String sex="";
- int argIndex = 1;
- for (int i = 0; i < ((myArgs.length)-1)/1; i++) {
- if (allPeople.get(Integer.parseInt(myArgs[argIndex])).getSex().equals(Sex.MALE)){
- sex= "м";
- }
- else if (allPeople.get(Integer.parseInt(myArgs[argIndex])).getSex().equals(Sex.FEMALE)) {
- sex="ж";
- }
- System.out.println((allPeople.get(Integer.parseInt(myArgs[argIndex])).getName())+" "+sex+" "+ (simpleDateFormat.format(allPeople.get(Integer.parseInt(myArgs[argIndex])).getBirthDay())));
- argIndex = argIndex + 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement