package ru.job4j.start;

import ru.job4j.ConsoleInput;
import ru.job4j.Tracker;
import ru.job4j.model.Item;

/**
 * Class ConsoleInput.
 *
 * @author i.sliusar
 * @version $Id$
 * @since 0.1
 * 23.06.2017
 */
public class StartUI {
    /**
     * menu of our program.
     */
    private String[] doList;

    /**
     * Construct.
     */
    public StartUI() {
        createDoList();
    }

    /**
     * start of program.
     */
    public void start() {

        printList(true);

        Tracker tracker = new Tracker();
        ConsoleInput input = new ConsoleInput();
        boolean programOnline = true;
        Item[] items;
        int userAnswer;
        Item currentItem = null;

        while (programOnline) {

            userAnswer = Integer.parseInt(input.answer("Select"));

            switch (userAnswer) {
                case 0:
                    currentItem = new Item(
                            input.answer("Please input Name"),
                            input.answer("Please input description"),
                            Long.parseLong(input.answer("Please input create")));
                    tracker.add(currentItem);
                    break;
                case 1:
                    items = tracker.findAll();
                    for (Item item : items) {
                        if (item == null) {
                            break;
                        }
                        System.out.printf("ID:%s  NAME:%s  DESCRIPTION:%s  \n", item.getId(), item.getName(), String.valueOf(item.getCreate()));
                    }
                    System.out.println();
                    break;
                case 2:
                    if (currentItem == null) {
                        currentItem = tracker.findById(input.answer("input Id Item for update"));
                    }
                    tracker.update(currentItem);
                    break;
                case 3:
                    if (currentItem == null) {
                        currentItem = tracker.findById(input.answer("input Id Item for delete"));
                    }
                    tracker.delete(currentItem);
                    if (tracker.findById(currentItem.getId()) == null) {
                        System.out.println("delete successful");
                    } else {
                        System.out.println("delete failed");
                    }
                    break;
                case 4:
                    currentItem = tracker.findById(input.answer("input Id for search"));
                    if (currentItem == null) {
                        System.out.println("find failed");

                    } else {
                        System.out.println("find successful");
                    }
                    break;
                case 5:
                    items = tracker.findByName(input.answer("input Name for search"));
                    System.out.println("I found the following list Item:");
                    for (Item item : items) {
                        System.out.printf("ID:%s  NAME:%s  DESCRIPTION:%s  \n", item.getId(), item.getName(), item.getCreate());
                    }
                    System.out.println();
                    break;
                case 6:
                    String answer = input.answer("You want to close program? yes/no");
                    programOnline = !answer.equals("yes");
                    break;
                default:
                    System.out.println("Действие не предустмотренно");
                    break;
            }
            printList(programOnline);
        }

    }

    /**
     * Print menu of program.
     * @param programOnline boolean
     */
    private void printList(boolean programOnline) {
        if (programOnline) {
            for (int i = 0; i < doList.length; i++) {
                System.out.println("" + i + "." + doList[i]);
            }
        } else {
            System.out.println("Bye bye");
        }
    }

    /**
     * Create menu of program.
     */
    private void createDoList() {
        doList = new String[7];
        doList[0] = "Add new item";
        doList[1] = "Show all items";
        doList[2] = "Edit item";
        doList[3] = "Delete item";
        doList[4] = "Find item by id";
        doList[5] = "Find items by name";
        doList[6] = "Exit Program";
    }

    /**
     * psvm.
     * @param args String[]
     */
    public static void main(String[] args) {
        new StartUI().start();
    }
}



@@ -225,16 +225,532 @@
 artUI %7B%0A
+    /**%0A     * Add.%0A     */%0A    private static final int ADD = 0;%0A    /**%0A     * Show all.%0A     */%0A    private static final int SHOW_ALL = 1;%0A    /**%0A     * Edit.%0A     */%0A    private static final int EDIT = 2;%0A    /**%0A     * delete.%0A     */%0A    private static final int DELETE = 3;%0A    /**%0A     * find by id.%0A     */%0A    private static final int FIND_BY_ID = 4;%0A    /**%0A     * finde by name.%0A     */%0A    private static final int FIND_BY_NAME = 5;%0A    /**%0A     * exit.%0A     */%0A    private static final int EXIT = 6;%0A%0A
     /**%0A
@@ -769,32 +769,32 @@
 of our program.%0A
-
      */%0A    priv
@@ -1361,17 +1361,19 @@
    case 
-0
+ADD
 :%0A      
@@ -1719,17 +1719,24 @@
    case 
-1
+SHOW_ALL
 :%0A      
@@ -2184,17 +2184,20 @@
    case 
-2
+EDIT
 :%0A      
@@ -2454,17 +2454,22 @@
    case 
-3
+DELETE
 :%0A      
@@ -2976,17 +2976,26 @@
    case 
-4
+FIND_BY_ID
 :%0A      
@@ -3346,17 +3346,28 @@
    case 
-5
+FIND_BY_NAME
 :%0A      
@@ -3810,17 +3810,20 @@
    case 
-6
+EXIT
 :%0A      
@@ -4435,25 +4435,25 @@
 rint
-ln(%22%22 + i + %22.%22 +
+f(%22%25d. %25s %5Cn%22, i,
  doL

Fix StartUi (constant & String format)