package ru.job4j.tracker;

import java.text.SimpleDateFormat;

/**.
 * Class StartUI entry point
 * @author Mikhail Vlasov
 * @since 6.4.2017
 * @version 1
 */
public class StartUI {
    /**.
     * Variable for Interface
     */
    private Input input;
    /**.
     * Constructor for StartUI class.
     * @param input interface.
     */
    public StartUI(Input input) {
        this.input = input;
    }

    /**.
     * Entry point.
     * @param args string array
     */
    public static void main(String[] args) {
        /**.
         * ConsoleInput class object.
         */
        Input input = new ConsoleInput();
        /**.
         * Starts questionnaire function.
         */
        new StartUI(input).init();
    }

    /**.
     * Main menu
     */
    public void init() {
        /**.
         * Simple date and time of item creation pattern.
         */
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        /**.
         * Main menu string
         */
        String question = "0. Add new Item" + System.lineSeparator()
                        + "1. Show all items" + System.lineSeparator()
                        + "2. Edit item" + System.lineSeparator()
                        + "3. Delete item" + System.lineSeparator()
                        + "4. Find item by Id" + System.lineSeparator()
                        + "5. Find items by name" + System.lineSeparator()
                        + "6. Exit Program" + System.lineSeparator();
        /**.
         * Class Tracker object initialization.
         */
        Tracker tracker = new Tracker();
        /**.
         * Contains user's choice.
         */
        String choise = input.ask(question);

        while (!"6".equals(choise)) {
            if ("0".equals(choise)) {
                String name = input.ask("Enter name: ");
                String description = input.ask("Enter description: ");
                tracker.add(new Item(name, description));

            } else if ("1".equals(choise)) {
                Item[] items = tracker.findAll();
                for (Item item: items) {
                    System.out.println(String.format("Name of task: %s"
                                                    + System.lineSeparator()
                                                    + "Description: %s"
                                                    + System.lineSeparator()
                                                    + "ID: %s"
                                                    + System.lineSeparator()
                                                    + "Date of creation: %s",
                            item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                    ));
                    System.lineSeparator();
                }

            } else if ("2".equals(choise)) {
                String id = input.ask("Enter id of the task you want to edit: ");
                Item item = tracker.findById(id);

                if (item == null) {
                    System.out.println("No task found");

                } else {
                    System.out.println(String.format("Name of task: %s"
                                                    + System.lineSeparator()
                                                    + "Description: %s"
                                                    + System.lineSeparator()
                                                    + "ID: %s" + System.lineSeparator()
                                                    + "Date of creation: %s",
                            item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                    ));
                    String desc = input.ask("Enter new description: ");
                    item.setDescription(desc);
                    tracker.update(item);
                }

            } else if ("3".equals(choise)) {
                String id = input.ask("Enter id of the task you want to edit: ");
                Item item = tracker.findById(id);

                if (item == null) {
                    System.out.println("No task found");

                } else {
                    tracker.delete(item);
                    System.out.println("The task was deleted.");
                }

            } else if ("4".equals(choise)) {
                String id = input.ask("Enter id of the task you want to find: ");
                Item item = tracker.findById(id);

                if (item == null) {
                    System.out.println("No task found");

                } else {
                    System.out.println(String.format("Name of task: %s"
                                                    + System.lineSeparator()
                                                    + "Description: %s" + System.lineSeparator()
                                                    + "ID: %s" + System.lineSeparator()
                                                    + "Date of creation: %s",
                            item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                    ));
                }

            } else if ("5".equals(choise)) {
                String name = input.ask("Enter name: ");
                Item[] items = tracker.findByName(name);

                if (items == null) {
                    System.out.println("No task found");

                } else {
                    for (Item item : items) {
                        System.out.println(String.format("Name of task: %s"
                                                        + System.lineSeparator()
                                                        + "Description: %s" + System.lineSeparator()
                                                        + "ID: %s"
                                                        + System.lineSeparator()
                                                        + "Date of creation: %s",
                                item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                        ));
                    }
                }
            }
            choise = input.ask(question);
        }
    }
}

@@ -253,24 +253,98 @@
 t;%0A    /**.%0A
+     * Variable for Tracker%0A     */%0A    private Tracker tracker;%0A    /**.%0A
      * Const
@@ -369,16 +369,16 @@
  class.%0A
-
      * @
@@ -466,24 +466,62 @@
 ut = input;%0A
+        this.tracker = new Tracker();%0A
     %7D%0A%0A    /
@@ -1637,122 +1637,8 @@
 **.%0A
-         * Class Tracker object initialization.%0A         */%0A        Tracker tracker = new Tracker();%0A        /**.%0A
     
@@ -2080,24 +2080,153 @@
 .findAll();%0A
+                if (items == null) %7B%0A                    System.out.println(%22No tasks available.%22);%0A                %7D else %7B%0A    
             
@@ -2243,16 +2243,17 @@
 tem item
+ 
 : items)
@@ -2247,32 +2247,36 @@
 item : items) %7B%0A
+    
                 
@@ -2367,36 +2367,24 @@
             
-            
 + System.lin
@@ -2432,36 +2432,24 @@
             
-    
-        
 + %22Descripti
@@ -2452,36 +2452,24 @@
 iption: %25s%22%0A
-            
             
@@ -2557,36 +2557,24 @@
             
-          
-  
 + %22ID: %25s%22%0A 
@@ -2568,36 +2568,24 @@
  + %22ID: %25s%22%0A
-            
             
@@ -2673,36 +2673,24 @@
             
-            
 + %22Date of c
@@ -2695,32 +2695,36 @@
  creation: %25s%22,%0A
+    
                 
@@ -2843,36 +2843,44 @@
                 
-));%0A
+    ));%0A    
                 
@@ -2899,32 +2899,54 @@
 ineSeparator();%0A
+                    %7D%0A
                 

Task 784