deleted unnecessary classes

This commit is contained in:
ZhigalskiiIvan 2023-03-30 22:00:19 +03:00
parent a8b42a7d42
commit ea792a3d0d

View File

@ -6,16 +6,12 @@ import org.jetbrains.letsPlot.geom.*
import kotlin.reflect.typeOf import kotlin.reflect.typeOf
/** Gives statistics of saved texts. /** Recognizes the name of the text, which
*/
class StatisticBuilder {
/** Recognizes the name of the text, which
* user want to see statistics about and type of output. * user want to see statistics about and type of output.
* It calls methods of graphic building or printing data in console, * It calls methods of graphic building or printing data in console,
* build required for it objects. * build required for it objects.
*/ */
fun getStatistics(textData: TextData) { fun getStatistics(textData: TextData) {
if (!textData.haveText()) { if (!textData.haveText()) {
println("No saved texts") println("No saved texts")
@ -41,13 +37,13 @@ class StatisticBuilder {
} }
} }
} }
/** Builds bar chart with data of mapOfSentenceNumToItsSize and saves image in file. /** Builds bar chart with data of mapOfSentenceNumToItsSize and saves image in file.
* @param textName name of texts, which user want to see statistics about. * @param textName name of texts, which user want to see statistics about.
* @param mapOfSentenceNumToItsSize map of pairs of sentence numbers and their words count. * @param mapOfSentenceNumToItsSize map of pairs of sentence numbers and their words count.
*/ */
private fun buildGraphic(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) { private fun buildGraphic(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) {
val data = mapOf( val data = mapOf(
"words count" to mapOfSentenceNumToItsSize.map { it.value.toFloat() / mapOfSentenceNumToItsSize.size }, "words count" to mapOfSentenceNumToItsSize.map { it.value.toFloat() / mapOfSentenceNumToItsSize.size },
@ -69,13 +65,13 @@ class StatisticBuilder {
println("Graphic was save in ${ggsave(fig, "$textName.png")}") println("Graphic was save in ${ggsave(fig, "$textName.png")}")
fig.show() fig.show()
} }
/** Prints statistics according to data from mapOfSentenceNumToItsSize. /** Prints statistics according to data from mapOfSentenceNumToItsSize.
* @param textName name of texts, which user want to see statistics about. * @param textName name of texts, which user want to see statistics about.
* @param mapOfSentenceNumToItsSize map of pairs of sentence numbers and their words count. * @param mapOfSentenceNumToItsSize map of pairs of sentence numbers and their words count.
*/ */
private fun printStatisticsInConsole(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) { private fun printStatisticsInConsole(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) {
val sectionTitle = "Text name: $textName" val sectionTitle = "Text name: $textName"
println("-".repeat(sectionTitle.length)) println("-".repeat(sectionTitle.length))
@ -102,17 +98,13 @@ class StatisticBuilder {
) )
println("-".repeat(sectionTitle.length)) println("-".repeat(sectionTitle.length))
println("Done!\n") println("Done!\n")
}
} }
/** Used for reading text from a file or console. */ /** Asks the user which where they want to read the text from and
class TextReader {
/** Asks the user which where they want to read the text from and
* calls special for file- and console- reading methods according the answer. * calls special for file- and console- reading methods according the answer.
*/ */
fun readNewText(textData: TextData) { fun readNewText(textData: TextData) {
println("Where do you want to add the text: from the console or a file?") println("Where do you want to add the text: from the console or a file?")
@ -123,14 +115,13 @@ class TextReader {
"console" -> readFromConsole(textData) "console" -> readFromConsole(textData)
"file" -> readFromFile(textData) "file" -> readFromFile(textData)
} }
} }
/** Read from console the name of text, checks that it hasn't saved yet and its contents,
/** Read from console the name of text, checks that it hasn't saved yet and its contents,
* asks if entered text is not correct and re-calls itself or * asks if entered text is not correct and re-calls itself or
* calls method of adding received text to data. * calls method of adding received text to data.
*/ */
private fun readFromConsole(textData: TextData) { private fun readFromConsole(textData: TextData) {
println("Input name of a text:") println("Input name of a text:")
if (textData.haveText()) println("Unavailable(existing) names: ${textData.getTextNamesInString()}.") if (textData.haveText()) println("Unavailable(existing) names: ${textData.getTextNamesInString()}.")
@ -164,14 +155,14 @@ class TextReader {
if (correctInputRequest.second == "yes") addTextToData(textData, name, content) if (correctInputRequest.second == "yes") addTextToData(textData, name, content)
else readFromConsole(textData) else readFromConsole(textData)
} }
/** Asks for the name of text, path to file to read text from, /** Asks for the name of text, path to file to read text from,
* checks for its existing, reads contents and, * checks for its existing, reads contents and,
* asks if entered names is not correct and re-calls itself or * asks if entered names is not correct and re-calls itself or
* calls method of adding received text to data. * calls method of adding received text to data.
*/ */
private fun readFromFile(textData: TextData) { private fun readFromFile(textData: TextData) {
println("Input a name of a text:") println("Input a name of a text:")
val name = readln() val name = readln()
@ -205,18 +196,17 @@ class TextReader {
"yes" -> addTextToData(textData, name, content) "yes" -> addTextToData(textData, name, content)
"no" -> readFromFile(textData) "no" -> readFromFile(textData)
} }
} }
/** /**
* Calls method of TextData class to add new text in set * Calls method of TextData class to add new text in set
* of tracked texts. * of tracked texts.
* @param textName name of new text. * @param textName name of new text.
* @param content content of new text. * @param content content of new text.
*/ */
private fun addTextToData(textData: TextData, textName: String, content: String) = private fun addTextToData(textData: TextData, textName: String, content: String) =
textData.addNewText(textName, content) textData.addNewText(textName, content)
}
/** Stores data about tracking texts, /** Stores data about tracking texts,
* includes methods for work with them due the adding. * includes methods for work with them due the adding.
@ -226,7 +216,6 @@ class TextData {
/** list of monitored texts. */ /** list of monitored texts. */
private val textsList = mutableListOf<Text>() private val textsList = mutableListOf<Text>()
fun haveText(): Boolean = textsList.isNotEmpty() fun haveText(): Boolean = textsList.isNotEmpty()
/** Method for removing text from watch list. Asks for a name of a text /** Method for removing text from watch list. Asks for a name of a text
@ -234,7 +223,6 @@ class TextData {
*/ */
fun removeText() { fun removeText() {
if (!haveText()) { if (!haveText()) {
println("No saved texts") println("No saved texts")
return return
@ -365,13 +353,11 @@ fun exit(): Nothing {
*/ */
class CommandCenter( class CommandCenter(
private val textData: TextData, private val textData: TextData,
private val textReader: TextReader,
private val statisticBuilder: StatisticBuilder
) { ) {
private val exitCommand = Command("exit", ::exit) private val exitCommand = Command("exit", ::exit)
private val addCommand = Command("add text") { textReader.readNewText(textData) } private val addCommand = Command("add text") { readNewText(textData) }
private val showStatisticsCommand = Command("show statistics") { statisticBuilder.getStatistics(textData) } private val showStatisticsCommand = Command("show statistics") { getStatistics(textData) }
private val removeTextCommand = Command("remove text") { textData.removeText() } private val removeTextCommand = Command("remove text") { textData.removeText() }
private val commandsList = listOf(exitCommand, addCommand, showStatisticsCommand, removeTextCommand) private val commandsList = listOf(exitCommand, addCommand, showStatisticsCommand, removeTextCommand)
@ -531,13 +517,12 @@ class ReturnCommand : InputOutcomeCommand {
override val exe: () -> Unit = throw ReturnException() override val exe: () -> Unit = throw ReturnException()
} }
fun main() { fun main() {
val textData = TextData() val textData = TextData()
val statisticBuilder = StatisticBuilder()
val textReader = TextReader()
val commandCenter = CommandCenter(textData, textReader, statisticBuilder) val commandCenter = CommandCenter(textData)
workCycle(commandCenter) workCycle(commandCenter)
} }