Advertisement
jaVer404

level17.lesson10.bonus01_with tests

Oct 27th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 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.  
  45.         if (args[0].equals("-c")) {
  46.             create(args);
  47.         }
  48.         else if (args[0].equals("-u")) {
  49.             update(args);
  50.         }
  51.  
  52.  
  53.         else if (args[0].equals("-d")) {
  54.             delete(args);
  55.         }
  56.  
  57.         else if (args[0].equals("-i")) {
  58.             inform(args);
  59.         }
  60.  
  61.  
  62.  
  63.     }
  64.  
  65.     public static void create (String[]myArgs) throws ParseException, NumberFormatException{
  66.         printBefore();//to delete
  67.         Person tempPers=null;
  68.         if(myArgs[2].equals("м")) {
  69.             tempPers = Person.createMale(myArgs[1], new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(myArgs[3]));
  70.         }
  71.         else if (myArgs[2].equals("ж")) {
  72.             tempPers = Person.createFemale(myArgs[1], new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(myArgs[3]));
  73.         }
  74.         allPeople.add(tempPers);
  75.         printAfter();//to delete
  76.         System.out.println(allPeople.indexOf(tempPers));
  77.     }
  78.  
  79.  
  80.  
  81.     public static void update (String[]myArgs) throws ParseException, NullPointerException{
  82.         printBefore();
  83.         Person tempPers=allPeople.get(Integer.parseInt(myArgs[1]));
  84.         if(myArgs[3].equals("м")) {
  85.             tempPers.setName(myArgs[2]);
  86.             tempPers.setSex(Sex.MALE);
  87.             tempPers.setBirthDay(new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(myArgs[4]));
  88.         }
  89.  
  90.         else if(myArgs[3].equals("ж")) {
  91.             tempPers.setName(myArgs[2]);
  92.             tempPers.setSex(Sex.FEMALE);
  93.             tempPers.setBirthDay(new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(myArgs[4]));
  94.         }
  95.         allPeople.set(Integer.parseInt(myArgs[1]),tempPers);
  96.         printAfter();
  97.     }
  98.  
  99.     public static void delete (String[]myArgs) {
  100.         printBefore();
  101.         Person tempPerson= allPeople.get(Integer.parseInt(myArgs[1]));
  102.         tempPerson.setName(null);
  103.         tempPerson.setSex(null);
  104.         tempPerson.setBirthDay(null);
  105.         allPeople.set(Integer.parseInt(myArgs[1]),tempPerson);
  106.         printAfter();
  107.     }
  108.  
  109.     public static void inform (String[]myArgs) {
  110.         printBefore();
  111.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
  112.         String sex="";
  113.  
  114.         if (allPeople.get(Integer.parseInt(myArgs[1])).getSex().equals(Sex.MALE)){
  115.             sex= "м";
  116.         }
  117.         else if (allPeople.get(Integer.parseInt(myArgs[1])).getSex().equals(Sex.FEMALE)) {
  118.             sex="ж";
  119.         }
  120.         System.out.println((allPeople.get(Integer.parseInt(myArgs[1])).getName())+" "+sex+" "+ (simpleDateFormat.format(allPeople.get(Integer.parseInt(myArgs[1])).getBirthDay())));
  121.         printAfter();
  122.     }
  123.  
  124.     public static void printBefore () {
  125.         System.out.println(Thread.currentThread().getStackTrace()[2].getMethodName());
  126.         System.out.println("/*------------------------*/");
  127.         System.out.println("Before create size of allPeople: "+allPeople.size());
  128.         for (Person p : allPeople) {
  129.             System.out.println("/*------------------------*/");
  130.             System.out.println("id = " + allPeople.indexOf(p));
  131.             System.out.println("name is: "+ p.getName());
  132.             System.out.println("sex is: "+p.getSex());
  133.             System.out.println("birthday is " + p.getBirthDay());
  134.         }
  135.     }
  136.  
  137.  
  138.     public static void printAfter () {
  139.         System.out.println(Thread.currentThread().getStackTrace()[2].getMethodName());
  140.         System.out.println("/*------------------------*/");
  141.         System.out.println("AFTER create size of allPeople: "+allPeople.size());
  142.         for (Person p : allPeople) {
  143.             System.out.println("/*------------------------*/");
  144.             System.out.println("id = " + allPeople.indexOf(p));
  145.             System.out.println("name is: "+ p.getName());
  146.             System.out.println("sex is: "+p.getSex());
  147.             System.out.println("birthday is " + p.getBirthDay());
  148.         }
  149.     }
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement