diff --git a/src/main/kotlin/Archive.kt b/src/main/kotlin/Archive.kt new file mode 100644 index 00000000..072db7f5 --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,3 @@ +class Archive(override val name: String) : Selectable { + var notes = arrayListOf() +} \ No newline at end of file diff --git a/src/main/kotlin/Input.kt b/src/main/kotlin/Input.kt new file mode 100644 index 00000000..2fa11069 --- /dev/null +++ b/src/main/kotlin/Input.kt @@ -0,0 +1,45 @@ +import java.util.Scanner + +class Input { + companion object { + var scanner = Scanner(System.`in`) + + fun intInput(menuSize: Int): Int { + var input = -1 + + do { + try { + println("Введите номер пункта меню:") + input = scanner.nextLine().toInt() + if (input > menuSize) { + println("Такой пункт отсутствует") + input = -1 + } + } catch (e: Exception) { + println("Необоходимо ввести число.") + } + } while (input < 0) + return input + } + + fun textInput(prompt: String, maxLength: Int): String { + + var input = "" + val max = maxLength + + do { + println(prompt) + input = scanner.nextLine().trim() + when (input.length) { + in 1..max -> return input + in 0..0 -> println("Значение не может быть пустым") + else -> { + println("Введено слишком длинное значение") + input = "" + } + } + } while (input == "") + return input + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..b3f71076 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,8 @@ fun main(args: Array) { - println("Hello World!") + + val screen = MainScreen() + do { + screen.showMenu() + if (screen.isExit) break + } while (true) } \ No newline at end of file diff --git a/src/main/kotlin/MainScreen.kt b/src/main/kotlin/MainScreen.kt new file mode 100644 index 00000000..01260b40 --- /dev/null +++ b/src/main/kotlin/MainScreen.kt @@ -0,0 +1,103 @@ +class MainScreen() { + private var screenType = ARCHIVE + private val data = mutableMapOf>() + var isExit = false + + private lateinit var active: Archive //ссылка на архив для экрана с записями + + fun showMenu(screenType: List = this.screenType) { + val caption = "_______ Меню ${screenType[2].lowercase()} _______" + val firstItem = "Создать ${screenType[1].lowercase()}" + val lastItem = "Выйти" + var input = 0 + var num = 0 + val dataItems = if (screenType == ARCHIVE) data.keys.toList() else active.notes.toList() + + println(caption) + println("$num. $firstItem") + for (item in dataItems) { + println("${++num}. \"${item.name}\"") + } + println("${++num}. ${lastItem}") + println("---------------") + + input = Input.intInput(num) + select(dataItems, input, num) + } + + private fun select(dataItems: List, input: Int, menuSize: Int) { + if (menuSize > 1) { + when (input) { + 0 -> create() + in 1 until menuSize -> { + if (screenType == ARCHIVE) { + active = dataItems[input - 1] as Archive + screenType = NOTE + } else { + showNote(dataItems[input - 1] as Note) + } + } + + menuSize -> { + if (screenType == NOTE) { + screenType = ARCHIVE + } else { + isExit = true + } + } + } + } else { + when (input) { + 0 -> create() + 1 -> { + if (screenType == NOTE) { + screenType = ARCHIVE + } else { + isExit = true + } + } + } + } + } + + private fun create() { + println("Процедура создания ${screenType[2].lowercase()}") + + val name = Input.textInput( + "Введите название ${screenType[2].lowercase()}}:\n(не более $NAME_MAX_LENGTH символов)", + NAME_MAX_LENGTH + ) + + when (screenType) { + ARCHIVE -> { + val archive = Archive(name) + data.put(archive, archive.notes) + } + + NOTE -> { + val text = Input.textInput( + "Введите текст ${NOTE[2].lowercase()}:\n(не более $TEXT_MAX_LENGTH символа)", + TEXT_MAX_LENGTH + ) + val note = Note(name, text) + active.notes.add(note) + } + } + } + + private fun showNote(note: Note) { + println("====== Просмотр записи ${note.name} ======") + println("Содержимое записи:") + println("${note.text}") + println("====== Конец ======") + println("\nВведите любой символ, чтобы вернуться в меню...") + Input.scanner.nextLine() + } + + companion object { + const val NAME_MAX_LENGTH = 32 + const val TEXT_MAX_LENGTH = 1024 + val ARCHIVE = listOf("Архив", "Архива", "Архивов") + val NOTE = listOf("Запись", "Записи", "Записей") + } +} \ No newline at end of file diff --git a/src/main/kotlin/Note.kt b/src/main/kotlin/Note.kt new file mode 100644 index 00000000..d30403a5 --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1,2 @@ +class Note(override val name: String, val text: String) : Selectable { +} \ No newline at end of file diff --git a/src/main/kotlin/ScreenNote.kt b/src/main/kotlin/ScreenNote.kt new file mode 100644 index 00000000..881e3bd2 --- /dev/null +++ b/src/main/kotlin/ScreenNote.kt @@ -0,0 +1,3 @@ +class ScreenNote { + +} \ No newline at end of file diff --git a/src/main/kotlin/Selectable.kt b/src/main/kotlin/Selectable.kt new file mode 100644 index 00000000..1aa7a0fe --- /dev/null +++ b/src/main/kotlin/Selectable.kt @@ -0,0 +1,3 @@ +interface Selectable { + val name: String +} \ No newline at end of file