textReader done

This commit is contained in:
ZhigalskiiIvan 2023-03-23 23:01:22 +03:00
parent 67c7a9dc6c
commit 38e29bcd52

View File

@ -1,3 +1,4 @@
import java.io.File
import java.util.Scanner import java.util.Scanner
import kotlin.system.exitProcess import kotlin.system.exitProcess
@ -5,21 +6,115 @@ val scanner = Scanner(System.`in`)
object StatisticBuilder { object StatisticBuilder {
fun buildGraphic() {} fun askAndExecuteSelfCommands() {}
fun printStatisticsInConsole() {} private fun buildGraphic() {}
private fun printStatisticsInConsole() {}
} }
object TextReader { object TextReader {
fun readFromConsole(): String { fun askAndExecuteSelfCommands() {
TODO()
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<Sentence>()
fun getSentencesCount() {}
data class Sentence(val wordsCount: Int) {}
}
object TextData {
private val textsList = mutableListOf<Text>() private val textsList = mutableListOf<Text>()
fun addNewText(textName: String, content: String) {} fun addNewText(textName: String, content: String) {}
@ -30,17 +125,6 @@ class TextData() {
fun returnListOfSentences(text: Text): List<Text.Sentence> { fun returnListOfSentences(text: Text): List<Text.Sentence> {
TODO() TODO()
} }
}
class Text(private val name: String, private val sentencesCount: Int) {
val sentencesList = listOf<Sentence>()
fun getSentencesCount() {}
class Sentence(val words_count: Int) {}
} }
} }
@ -53,8 +137,8 @@ object CommandCenter {
println("bye!") println("bye!")
exitProcess(0) exitProcess(0)
}), }),
ADD_TEXT({ println("add") }), ADD_TEXT({ TextReader.askAndExecuteSelfCommands() }),
SHOW_STATISTICS({ println("sow") }), SHOW_STATISTICS({ StatisticBuilder.askAndExecuteSelfCommands() }),
REMOVE_TEXT({ println("remove") }) REMOVE_TEXT({ println("remove") })
} }
@ -67,11 +151,8 @@ object CommandCenter {
else ::readCommandFromConsole else ::readCommandFromConsole
} }
fun executeCommand() {}
} }
fun main() { fun main() {
mainCycle@ while (true) { mainCycle@ while (true) {