package ru.job4j.chat;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * ConsoleChat.
 * @author Ivan Belyaev
 * @since 08.09.2019
 * @version 1.0
 */
public class ConsoleChat {
    /** User input stream. */
    private Scanner input;

    /**
     * The constructor creates the object ConsoleChat.
     * @param in - user input stream.
     */
    public ConsoleChat(InputStream in) {
        this.input = new Scanner(in, StandardCharsets.UTF_8);
    }

    /**
     * Entry point.
     * @param args - command-line arguments.
     * @throws IOException - input/output exceptions.
     */
    public static void main(String[] args) throws IOException {
        System.out.println("Console chat started\n");
        System.out.println("exit - exit");
        System.out.println("stop - so that the robot stops responding");
        System.out.println("continue - the robot starts to respond again\n");
        new ConsoleChat(System.in).start();
    }

    /**
     * Method starts console chat.
     * @throws IOException - input/output exceptions.
     */
    public void start() throws IOException {
        List<String> answers = fillAnswers();

        Random random = new Random();
        String outputPath = System.getProperty("java.io.tmpdir") + File.separator + "log.txt";
        try (Scanner in = input;
             PrintWriter out = new PrintWriter(outputPath, StandardCharsets.UTF_8)) {

            boolean needAnswer = true;
            String userSay = in.nextLine();
            while (!userSay.equals("exit")) {
                out.println(userSay);
                if (userSay.equals("stop")) {
                    needAnswer = false;
                } else if (userSay.equals("continue")) {
                    needAnswer = true;
                }
                if (needAnswer) {
                    String answer = answers.get(random.nextInt(answers.size()));
                    System.out.println(answer);
                    out.println(answer);
                }
                userSay = in.nextLine();
            }
            out.println("exit");
        }
    }

    /**
     * The method returns a list of robot responses from the resource file.
     * @return  a list of robot responses.
     * @throws IOException - input/output exceptions.
     */
    private List<String> fillAnswers() throws IOException {
        String path = ClassLoader.getSystemResource("answers.txt").getPath();
        return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
    }
}
@@ -1735,31 +1735,30 @@
 while (!
-userSay
+%22exit%22
 .equals(
 %22exit%22))
@@ -1749,22 +1749,23 @@
 .equals(
-%22exit%22
+userSay
 )) %7B%0A   
@@ -1819,31 +1819,30 @@
     if (
-userSay
+%22stop%22
 .equals(
 %22stop%22))
@@ -1833,22 +1833,23 @@
 .equals(
-%22stop%22
+userSay
 )) %7B%0A   
@@ -1916,33 +1916,33 @@
 if (
-userSay.equals(%22continue%22
+%22continue%22.equals(userSay
 )) %7B

'Консольный чат'[#862]. Метод equals применяется к константам