diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 4b72dbb..c9f8c4a 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,4 @@ +import java.io.File import java.util.Scanner import kotlin.system.exitProcess @@ -5,21 +6,115 @@ val scanner = Scanner(System.`in`) object StatisticBuilder { - fun buildGraphic() {} + fun askAndExecuteSelfCommands() {} - fun printStatisticsInConsole() {} + private fun buildGraphic() {} + + private fun printStatisticsInConsole() {} } object TextReader { - fun readFromConsole(): String { - TODO() + fun askAndExecuteSelfCommands() { + + println("Where do you want to add the text: from the console of a file?") + when (readln().lowercase()) { + "console" -> readFromConsole() + "file" -> readFromFile() + "return" -> return + else -> { + println("Repeat input or print \"return\" to return in main menu:") + askAndExecuteSelfCommands() + } + } } + + private fun readFromConsole() { + + println("Input name of a text:") + val name = readln() + + println("Input a text content:") + var content: String = "" + readCycle@ while (true) { + val nextPart = scanner.next() + if (nextPart.isEmpty()) content += nextPart + else break@readCycle + } + + correctInputQuestion@ while (true) { + print("Input was correct?[yes, no]: ") + when (readln()) { + "yes" -> addTextToData(name, content) + "no" -> readFromConsole() + "return" -> return + else -> { + println("Input only \"yes\" or \"no\" or \"return\" if you want to exit in main menu:") + } + } + } + } + + private fun readFromFile() { + + println("Input a name of a text:") + val name = readln() + + val contentsFile: File + pathReadCycle@ while (true) { + println("Input a path to file:") + val filePath = readln() + val testFile = File(filePath) + + when { + filePath == "return" -> return + !testFile.exists() -> { + println("File path incorrect. Repeat input or enter \"return\" to return in main menu:") + continue@pathReadCycle + } + + else -> { + contentsFile = testFile + break@pathReadCycle + } + } + } + val content = contentsFile.readText() + + correctInputQuestion@ while (true) { + print("Input was correct?[yes, no]: ") + when (readln()) { + "yes" -> addTextToData(name, content) + "no" -> readFromFile() + "return" -> return + else -> { + println("Input only \"yes\" or \"no\" or \"return\" if you want to exit in main menu:") + } + } + } + + } + + private fun addTextToData(textName: String, content: String) { + TextData.addNewText(textName, content) + } + } -class TextData() { +data class Text(private val name: String, private val sentencesCount: Int) { + + val sentencesList = listOf() + + fun getSentencesCount() {} + + data class Sentence(val wordsCount: Int) {} + +} + + +object TextData { private val textsList = mutableListOf() fun addNewText(textName: String, content: String) {} @@ -30,17 +125,6 @@ class TextData() { fun returnListOfSentences(text: Text): List { TODO() } - - } - - class Text(private val name: String, private val sentencesCount: Int) { - - val sentencesList = listOf() - - fun getSentencesCount() {} - - class Sentence(val words_count: Int) {} - } } @@ -53,8 +137,8 @@ object CommandCenter { println("bye!") exitProcess(0) }), - ADD_TEXT({ println("add") }), - SHOW_STATISTICS({ println("sow") }), + ADD_TEXT({ TextReader.askAndExecuteSelfCommands() }), + SHOW_STATISTICS({ StatisticBuilder.askAndExecuteSelfCommands() }), REMOVE_TEXT({ println("remove") }) } @@ -67,11 +151,8 @@ object CommandCenter { else ::readCommandFromConsole } - fun executeCommand() {} - } - fun main() { mainCycle@ while (true) {