Advertisement
jaVer404

level17.lesson10.bonus01(done)

Oct 27th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. package com.javarush.test.level17.lesson10.bonus01;
  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
  11. CrUD - Create, Update, Delete
  12. Программа запускается с одним из следующих наборов параметров:
  13. -c name sex bd
  14.  
  15. -u id name sex bd
  16. -d id
  17. -i id
  18.  
  19. Значения параметров:
  20. name - имя, String
  21. sex - пол, "м" или "ж", одна буква
  22. bd - дата рождения в следующем формате 15/04/1990
  23. -c  - добавляет человека с заданными параметрами в конец allPeople, выводит id (index) на экран
  24. -u  - обновляет данные человека с данным id
  25. -d  - производит логическое удаление человека с id
  26. -i  - выводит на экран информацию о человеке с id: name sex (м/ж) bd (формат 15-Apr-1990)
  27.  
  28. id соответствует индексу в списке
  29. Все люди должны храниться в allPeople
  30. Используйте Locale.ENGLISH в качестве второго параметра для SimpleDateFormat
  31.  
  32. Пример параметров: -c Миронов м 15/04/1990
  33. */
  34.  
  35. public class Solution {
  36.     public static List<Person> allPeople = new ArrayList<Person>();
  37.     static {
  38.         allPeople.add(Person.createMale("Иванов Иван", new Date()));  //сегодня родился    id=0
  39.         allPeople.add(Person.createMale("Петров Петр", new Date()));  //сегодня родился    id=1
  40.     }
  41.  
  42.     public static void main(String[] args) throws ParseException, NumberFormatException, NullPointerException{
  43.         //start here - начни тут
  44.         if (args[0].equals("-c")) {
  45.             create(args);
  46.         }
  47.        if (args[0].equals("-u")) {
  48.             update(args);
  49.         }
  50.         if (args[0].equals("-d")) {
  51.             delete(args);
  52.         }
  53.  
  54.         if (args[0].equals("-i")) {
  55.             inform(args);
  56.         }
  57.     }
  58.  
  59.     public static void create (String[]myArgs) throws ParseException, NumberFormatException{
  60.         Person tempPers=null;
  61.         if(myArgs[2].equals("м")) {
  62.             tempPers= Person.createMale(myArgs[1], new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(myArgs[3]));
  63.             allPeople.add(tempPers);
  64.         }
  65.  
  66.         if (myArgs[2].equals("ж")) {
  67.             tempPers=Person.createFemale(myArgs[1], new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(myArgs[3]));
  68.             allPeople.add(tempPers);
  69.         }
  70.         System.out.println(allPeople.indexOf(tempPers));
  71.     }
  72.  
  73.  
  74.  
  75.     public static void update (String[]myArgs) throws ParseException, NullPointerException, ArrayIndexOutOfBoundsException{
  76.         Person tempPers=allPeople.get(Integer.parseInt(myArgs[1]));
  77.         if(myArgs[3].equals("м")) {
  78.             tempPers.setName(myArgs[2]);
  79.             tempPers.setSex(Sex.MALE);
  80.             tempPers.setBirthDay(new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(myArgs[4]));
  81.         }
  82.  
  83.         else if(myArgs[3].equals("ж")) {
  84.             tempPers.setName(myArgs[2]);
  85.             tempPers.setSex(Sex.FEMALE);
  86.             tempPers.setBirthDay(new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(myArgs[4]));
  87.         }
  88.         allPeople.set(Integer.parseInt(myArgs[1]),tempPers);
  89.     }
  90.  
  91.     public static void delete (String[]myArgs) {
  92.         allPeople.get(Integer.parseInt(myArgs[1])).setName(null);
  93.         allPeople.get(Integer.parseInt(myArgs[1])).setSex(null);
  94.         allPeople.get(Integer.parseInt(myArgs[1])).setBirthDay(null);
  95.     }
  96.  
  97.     public static void inform (String[]myArgs) {
  98.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
  99.         String sex="";
  100.  
  101.         if (allPeople.get(Integer.parseInt(myArgs[1])).getSex().equals(Sex.MALE)){
  102.             sex= "м";
  103.         }
  104.         else if (allPeople.get(Integer.parseInt(myArgs[1])).getSex().equals(Sex.FEMALE)) {
  105.             sex="ж";
  106.         }
  107.         System.out.println((allPeople.get(Integer.parseInt(myArgs[1])).getName())+" "+sex+" "+ (simpleDateFormat.format(allPeople.get(Integer.parseInt(myArgs[1])).getBirthDay())));
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement