Advertisement
jaVer404

level17.lesson10.bonus02_done

Oct 29th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. package com.javarush.test.level17.lesson10.bonus02;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.List;
  8. import java.util.Locale;
  9.  
  10. /* CRUD 2
  11. CrUD Batch - multiple Creation, Updates, Deletion
  12. !!!РЕКОМЕНДУЕТСЯ выполнить level17.lesson10.bonus01 перед этой задачей!!!
  13.  
  14. Программа запускается с одним из следующих наборов параметров:
  15. -c name1 sex1 bd1 name2 sex2 bd2 ...
  16. -u id1 name1 sex1 bd1 id2 name2 sex2 bd2 ...
  17. -d id1 id2 id3 id4 ...
  18. -i id1 id2 id3 id4 ...
  19. Значения параметров:
  20. name - имя, String
  21. sex - пол, "м" или "ж", одна буква
  22. bd - дата рождения в следующем формате 15/04/1990
  23. -с  - добавляет всех людей с заданными параметрами в конец allPeople, выводит id (index) на экран в соответствующем порядке
  24. -u  - обновляет соответствующие данные людей с заданными id
  25. -d  - производит логическое удаление всех людей с заданными id
  26. -i  - выводит на экран информацию о всех людях с заданными id: name sex bd
  27.  
  28. id соответствует индексу в списке
  29. Формат вывода даты рождения 15-Apr-1990
  30. Все люди должны храниться в allPeople
  31. Порядок вывода данных соответствует вводу данных
  32.  
  33. Обеспечить корректную работу с данными для множества нитей (чтоб не было затирания данных)
  34. Используйте Locale.ENGLISH в качестве второго параметра для SimpleDateFormat
  35. */
  36.  
  37. public class Solution {
  38.     public static List<Person> allPeople = new ArrayList<Person>();
  39.     static {
  40.         allPeople.add(Person.createMale("Иванов Иван", new Date()));  //сегодня родился    id=0
  41.         allPeople.add(Person.createMale("Петров Петр", new Date()));  //сегодня родился    id=1
  42.     }
  43.  
  44.     public static void main(String[] args) throws ParseException, NumberFormatException, NullPointerException{
  45.         //start here - начни тут
  46.         if (args[0].equals("-c")) {
  47.             create(args);
  48.         }
  49.  
  50.        if (args[0].equals("-u")) {
  51.            update(args);
  52.         }
  53.  
  54.         if (args[0].equals("-d")) {
  55.             delete(args);
  56.         }
  57.  
  58.         if (args[0].equals("-i")) {
  59.             inform(args);
  60.         }
  61.     }
  62. /*------------------------CREATE---------------------------------*/
  63.     public static synchronized void create (String[]myArgs) throws ParseException, NumberFormatException{
  64.         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
  65.         Person tempPers=null;
  66.         int argIndex = 1;
  67.  
  68.         for (int i = 0; i < ((myArgs.length)-1)/3; i++) {
  69.             if(myArgs[argIndex+1].equals("м")) {//был индекс 2
  70.                 tempPers= Person.createMale(myArgs[argIndex], sdf.parse(myArgs[argIndex+2]));//был индекс 3
  71.                 allPeople.add(tempPers);
  72.             }
  73.             if(myArgs[argIndex+1].equals("ж")) {//был индекс 2
  74.                 tempPers= Person.createFemale(myArgs[argIndex], sdf.parse(myArgs[argIndex+2]));//был индекс 3
  75.                 allPeople.add(tempPers);
  76.             }
  77.             System.out.println(allPeople.indexOf(tempPers));
  78.             tempPers=null;
  79.             argIndex=argIndex+3;
  80.         }
  81.     }
  82.  
  83. /*------------------------UPDATE---------------------------------*/
  84.     public static synchronized void update (String[]myArgs) throws ParseException, NullPointerException, ArrayIndexOutOfBoundsException{
  85.         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
  86.         int argIndex = 1;
  87.         for (int i = 0; i < ((myArgs.length)-1)/4; i++) {
  88.             Person tempPers=allPeople.get(Integer.parseInt(myArgs[argIndex]));//1
  89.             if(myArgs[argIndex+2].equals("м")) {//inex 3
  90.                 tempPers.setName(myArgs[argIndex+1]);//index2
  91.                 tempPers.setSex(Sex.MALE);
  92.                 tempPers.setBirthDay(sdf.parse(myArgs[argIndex+3]));//index 4
  93.             }
  94.             else if(myArgs[3].equals("ж")) {//3
  95.                 tempPers.setName(myArgs[argIndex+1]);//2
  96.                 tempPers.setSex(Sex.FEMALE);
  97.                 tempPers.setBirthDay(sdf.parse(myArgs[argIndex+3]));//4
  98.             }
  99.             allPeople.set(Integer.parseInt(myArgs[argIndex]),tempPers);//1
  100.             argIndex=argIndex+4;
  101.         }
  102.     }
  103.  
  104. /*------------------------DELETE---------------------------------*/
  105. public static synchronized void delete (String[]myArgs) {
  106.     int argIndex = 1;
  107.     for (int i = 0; i < ((myArgs.length)-1)/1; i++) {
  108.     allPeople.get(Integer.parseInt(myArgs[argIndex])).setName(null);
  109.     allPeople.get(Integer.parseInt(myArgs[argIndex])).setSex(null);
  110.     allPeople.get(Integer.parseInt(myArgs[argIndex])).setBirthDay(null);
  111.     argIndex = argIndex + 1;
  112.     }
  113. }
  114.  
  115. /*------------------------INFORM---------------------------------*/
  116.     public static synchronized void inform (String[]myArgs) {
  117.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
  118.         String sex="";
  119.         int argIndex = 1;
  120.         for (int i = 0; i < ((myArgs.length)-1)/1; i++) {
  121.         if (allPeople.get(Integer.parseInt(myArgs[argIndex])).getSex().equals(Sex.MALE)){
  122.             sex= "м";
  123.         }
  124.         else if (allPeople.get(Integer.parseInt(myArgs[argIndex])).getSex().equals(Sex.FEMALE)) {
  125.             sex="ж";
  126.         }
  127.         System.out.println((allPeople.get(Integer.parseInt(myArgs[argIndex])).getName())+" "+sex+" "+ (simpleDateFormat.format(allPeople.get(Integer.parseInt(myArgs[argIndex])).getBirthDay())));
  128.         argIndex = argIndex + 1;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement