Advertisement
remote87

CRUD

Mar 23rd, 2021
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.codegym.task.task17.task1710;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6.  
  7. /*
  8. CRUD
  9.  
  10. */
  11.  
  12. public class Solution {
  13.     public static List<Person> allPeople = new ArrayList<>();
  14.  
  15.     static {
  16.         allPeople.add(Person.createMale("Donald Chump", new Date()));  // id=0
  17.         allPeople.add(Person.createMale("Larry Gates", new Date()));  // id=1
  18.     }
  19.  
  20.     public static void main(String[] args) {
  21.         // Start here
  22.         String[] argsStr = new String[args.length];
  23.         for (int i = 0; i < args.length; i++) {
  24.             argsStr[i] = args[i];
  25.         }
  26.         crud(argsStr);
  27.     }
  28.  
  29.     public static void crud(String[] input){
  30.         String command = input[0];
  31.         String name = "";
  32.         String sex = "";
  33.         Date date = null;
  34.  
  35.         int index = 0;
  36.  
  37.         if(command.equals("-c") || command.equals("-u")){
  38.             if(!command.equals("-u")){
  39.                 sex = input[2];
  40.                 name = input[1];
  41.             }else{
  42.                 sex = input[3];
  43.                 name = input[2];
  44.             }
  45.  
  46.             try {
  47.                 date = new SimpleDateFormat("MM dd yyyy").parse(input[input.length - 1]);
  48.             } catch (ParseException e) {
  49.                 e.printStackTrace();
  50.             }
  51.         }
  52.  
  53.         boolean udi = command.equals("-u") || command.equals("-d") || command.equals("-i");
  54.         if(udi) {
  55.             index = Integer.parseInt(input[1]);
  56.         }
  57.  
  58.         switch (command){
  59.             case "-c":
  60.                 create(name, sex, date);
  61.                 break;
  62.             case "-u":
  63.                 update(index, name, sex, date);
  64.                 break;
  65.             case "-d":
  66.                 Person delete = allPeople.get(index);
  67.                 delete.setName(null);
  68.                 delete.setSex(null);
  69.                 delete.setBirthDate(null);
  70.                 allPeople.set(index, delete);
  71.             case "-i":
  72.                 printPerson(allPeople.get(index));
  73.                 break;
  74.             default:
  75.                 System.out.println("Wrong command!");
  76.         }
  77.     }
  78.  
  79.     public static void create(String inName, String sex, Date date){
  80.         if(sex.equals("m")) allPeople.add(Person.createMale(inName, date));
  81.         if(sex.equals("f")) allPeople.add(Person.createFemale(inName, date));
  82.         System.out.println(allPeople.size() - 1);
  83.     }
  84.  
  85.     public static void update(int index, String name, String sex, Date date){
  86.         allPeople.get(index).setName(name);
  87.         allPeople.get(index).setSex((sex.equals("m")) ? Sex.MALE : Sex.FEMALE);
  88.         allPeople.get(index).setBirthDate(date);
  89.     }
  90.  
  91.     public static void printPerson(Person person){
  92.         StringBuilder sb = new StringBuilder();
  93.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd yyyy", Locale.ENGLISH);
  94.         String dateStr = simpleDateFormat.format(person.getBirthDate());
  95.         String sex = (person.getSex() == Sex.MALE) ? "m" : "f";
  96.         sb
  97.                 .append(person.getName())
  98.                 .append(" ")
  99.                 .append(sex)
  100.                 .append(" ")
  101.                 .append(dateStr);
  102.         System.out.println(sb.toString());
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement