From 9d06071a2f61ac137bc580eb5dffd00df2b84d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D1=80=D0=B5=D0=BC=D0=B8=D0=BD=D0=B0=20=D0=9F=D0=BE?= =?UTF-8?q?=D0=BB=D0=B8=D0=BD=D1=8B?= Date: Sun, 14 Dec 2025 20:24:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=E2=84=96?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- src/main/kotlin/Main.kt | 141 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 3 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..52b96137 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..22571423 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,140 @@ -fun main(args: Array) { - println("Hello World!") +import java.util.Scanner + +data class Archive(val name: String, val notes: MutableList = mutableListOf()) +data class Note(val title: String, val content: String) + +open class Menu(private val title: String) { + protected val scanner = Scanner(System.`in`) + protected val items = mutableListOf>() + + data class MenuItem(val name: String, val action: () -> T) + + fun show(): T { + while (true) { + println("\n$title") + items.forEachIndexed { index, item -> + println("$index. ${item.name}") + } + + print("Выберите пункт меню: ") + val input = scanner.nextLine() + + try { + val choice = input.toInt() + if (choice in items.indices) { + return items[choice].action() + } else { + println("Ошибка: пункт $choice не существует. Пожалуйста, выберите из списка.") + } + } catch (e: NumberFormatException) { + println("Ошибка: пожалуйста, введите число.") + } + } + } + + protected fun readNonEmptyString(prompt: String): String { + while (true) { + print(prompt) + val input = scanner.nextLine().trim() + if (input.isNotEmpty()) { + return input + } + println("Ошибка: значение не может быть пустым. Попробуйте еще раз.") + } + } +} + +class NoteMenu(private val archive: Archive) : Menu("Архив: ${archive.name}") { + + init { + updateMenuItems() + } + + private fun updateMenuItems() { + items.clear() + + items.add(MenuItem("Создать заметку") { + createNote() + updateMenuItems() + Unit + }) + + archive.notes.forEach { note -> + items.add(MenuItem(note.title) { + viewNote(note) + updateMenuItems() + Unit + }) + } + + items.add(MenuItem("Назад") { + Unit + }) + } + + private fun createNote() { + val title = readNonEmptyString("Введите заголовок заметки: ") + val content = readNonEmptyString("Введите текст заметки: ") + archive.notes.add(Note(title, content)) + println("Заметка '$title' успешно создана!") + } + + private fun viewNote(note: Note) { + println("\nЗаголовок: ${note.title}") + println("Текст заметки:") + println(note.content) + print("\nНажмите Enter для возврата...") + scanner.nextLine() + } +} + +class ArchiveMenu(private val archives: MutableList) : Menu("Список архивов") { + + init { + updateMenuItems() + } + + private fun updateMenuItems() { + items.clear() + + items.add(MenuItem("Создать архив") { + createArchive() + updateMenuItems() + Unit + }) + + archives.forEach { archive -> + items.add(MenuItem(archive.name) { + NoteMenu(archive).show() + updateMenuItems() + Unit + }) + } + + items.add(MenuItem("Выход") { + println("До свидания!") + kotlin.system.exitProcess(0) + }) + } + + private fun createArchive() { + val name = readNonEmptyString("Введите имя архива: ") + archives.add(Archive(name)) + println("Архив '$name' успешно создан!") + } + + fun start() { + while (true) { + show() + } + } +} + +fun main() { + println("Приложение Заметки") + + val archives = mutableListOf() + val archiveMenu = ArchiveMenu(archives) + + archiveMenu.start() } \ No newline at end of file