From f851efce9e37a120c00e2f3c1ad3f74b68c95bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BD=D0=BD=D0=B0?= <67254752@gmail.com> Date: Thu, 23 Apr 2026 14:16:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BA=D0=BE=D0=BD=D1=81=D0=BE=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=BE=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20"=D0=97=D0=B0=D0=BC=D0=B5=D1=82=D0=BA=D0=B8"=20?= =?UTF-8?q?=D1=81=20=D0=B0=D1=80=D1=85=D0=B8=D0=B2=D0=B0=D0=BC=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=82=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Добавлены классы Archive и Note - Добавлен универсальный MenuScreen для меню - Добавлен CreateScreen для создания элементов - Добавлен ContentView для просмотра заметок - Добавлена навигация между экранами - Добавлена валидация пустых полей - Добавлена обработка ошибок ввода --- .idea/caches/deviceStreaming.xml | 1610 ++++++++++++++++++++++++++++++ .idea/markdown.xml | 8 + .idea/misc.xml | 2 +- src/main/kotlin/App.kt | 82 ++ src/main/kotlin/Archive.kt | 7 + src/main/kotlin/ContentView.kt | 14 + src/main/kotlin/CreateScreen.kt | 38 + src/main/kotlin/Main.kt | 4 +- src/main/kotlin/MenuScreen.kt | 63 ++ src/main/kotlin/Note.kt | 8 + 10 files changed, 1833 insertions(+), 3 deletions(-) create mode 100644 .idea/caches/deviceStreaming.xml create mode 100644 .idea/markdown.xml create mode 100644 src/main/kotlin/App.kt create mode 100644 src/main/kotlin/Archive.kt create mode 100644 src/main/kotlin/ContentView.kt create mode 100644 src/main/kotlin/CreateScreen.kt create mode 100644 src/main/kotlin/MenuScreen.kt create mode 100644 src/main/kotlin/Note.kt diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 00000000..05e089c1 --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,1610 @@ + + + + + + \ No newline at end of file diff --git a/.idea/markdown.xml b/.idea/markdown.xml new file mode 100644 index 00000000..c61ea334 --- /dev/null +++ b/.idea/markdown.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..2bfdeda2 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/App.kt b/src/main/kotlin/App.kt new file mode 100644 index 00000000..67f47202 --- /dev/null +++ b/src/main/kotlin/App.kt @@ -0,0 +1,82 @@ +class App { + private val archives = mutableListOf() + private var isRunning = true + + fun start() { + while (isRunning) { + showArchiveMenu() + + } + println("Программа завершена") + } + + private fun showArchiveMenu() { + val menu = MenuScreen( + title = "Архивы", + items = archives.toList(), + onItemSelected = { archive -> showNotesMenu(archive) }, + onCreateNew = { createArchive() }, + onExit = { exit() } + + ) + menu.show() + } + + private fun createArchive() { + val result = CreateScreen("архив").show() + when (result) { + is CreateResult.ArchiveCreated -> { + archives.add(Archive(result.name)) + println("Архив \"${result.name}\" создан!") + } + + is CreateResult.Cancelled -> { + println("Создание архива отменено") + } + + else -> {} + } + } + + private fun showNotesMenu(archive: Archive) { + var isInNotesMenu = true + while (isInNotesMenu) { + val menu = MenuScreen( + title = "Заметки в архиве \"${archive.name}\"", + items = archive.notes.toList(), + onItemSelected = { note -> showNote(note) }, + onCreateNew = { createNote(archive) }, + onExit = { isInNotesMenu = false }, + + ) + menu.show() + } + } + + + private fun createNote(archive: Archive) { + val result = CreateScreen("заметки").show() + + when (result) { + is CreateResult.NoteCreated -> { + archive.notes.add(Note(result.name, result.text)) + println("Заметка \"${result.name}\"создана!") + } + + is CreateResult.Cancelled -> { + println("Создание заметки отменено") + } + + else -> {} + } + } + + private fun showNote(note: Note) { + ContentView(note).show() + } + + private fun exit() { + isRunning = false + + } +} \ No newline at end of file diff --git a/src/main/kotlin/Archive.kt b/src/main/kotlin/Archive.kt new file mode 100644 index 00000000..6285ae36 --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,7 @@ +data class Archive( + val name: String, + val notes: MutableList = mutableListOf() + +) { + override fun toString(): String = name +} \ No newline at end of file diff --git a/src/main/kotlin/ContentView.kt b/src/main/kotlin/ContentView.kt new file mode 100644 index 00000000..14a47932 --- /dev/null +++ b/src/main/kotlin/ContentView.kt @@ -0,0 +1,14 @@ +import java.util.Scanner + +class ContentView( + private val note: Note +) { + fun show() { + println("\n===${note.nameNote}===") + println("-".repeat(40)) + println(note.textNote) + println("_".repeat(40)) + println("Нажмите Enter, что бы вернуться...") + Scanner(System.`in`).nextLine() + } +} \ No newline at end of file diff --git a/src/main/kotlin/CreateScreen.kt b/src/main/kotlin/CreateScreen.kt new file mode 100644 index 00000000..4c68de98 --- /dev/null +++ b/src/main/kotlin/CreateScreen.kt @@ -0,0 +1,38 @@ +import java.util.Scanner + + +class CreateScreen( + private val itemType: String +) { + companion object { + private val scanner = Scanner(System.`in`) + } + + fun show(): CreateResult { + println("\nСоздание $itemType") + print("Введите название $itemType: ") + val name = scanner.nextLine().trim() + if (name.isEmpty()) { + println("Ошибка: название не может быть пустым") + return CreateResult.Cancelled + } + if (itemType == "заметки") { + print("Введите текст заметки: ") + val text = scanner.nextLine().trim() + if (text.isEmpty()) { + println("Ошибка: текст заметки не может быть пустым") + return CreateResult.Cancelled + } + return CreateResult.NoteCreated(name, text) + } + return CreateResult.ArchiveCreated(name) + } + +} + +sealed class CreateResult { + object Cancelled : CreateResult() + data class ArchiveCreated(val name: String) : CreateResult() + data class NoteCreated(val name: String, val text: String) : CreateResult() + +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..3ed8f26b 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,3 @@ -fun main(args: Array) { - println("Hello World!") +fun main() { + App().start() } \ No newline at end of file diff --git a/src/main/kotlin/MenuScreen.kt b/src/main/kotlin/MenuScreen.kt new file mode 100644 index 00000000..f35e652b --- /dev/null +++ b/src/main/kotlin/MenuScreen.kt @@ -0,0 +1,63 @@ +import java.util.Scanner + + +import kotlin.collections.toList + +class MenuScreen( + private val title: String, + private val items: List, + private val onItemSelected: (T) -> Unit, + private val onCreateNew: () -> Unit, + private val onExit: () -> Unit +) { + companion object { + private val scanner = Scanner(System.`in`) + } + + fun show() { + val itemsSnapshot = items.toList() + + while (true) { + println("\n=== $title ===") + if (itemsSnapshot.isEmpty()) { + println("Список пуст. Чтобы создать элемент, выберите пункт 'Создать'") + } else { + itemsSnapshot.forEachIndexed { index, item -> + println("${index + 1}. $item") + } + } + println("${itemsSnapshot.size + 1}. Создать") + println("0. Выход") + print("Выберите пункт (0-${itemsSnapshot.size + 1}): ") + val input = scanner.nextLine().trim() + val choice = input.toIntOrNull() + if (choice == null) { + println("Ошибка: введите цифру.") + continue + } + when (choice) { + 0 -> { + onExit() + return + + } + + in 1..itemsSnapshot.size -> { + onItemSelected(itemsSnapshot[choice - 1]) + return + } + + itemsSnapshot.size + 1 -> { + onCreateNew() + return + } + + else -> { + println("Ошибка: нет пункта с номером $choice.") + println("Доступные номера: 0-${itemsSnapshot.size + 1}") + } + } + + } + } +} \ 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..cee16e5f --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1,8 @@ +data class Note( + val nameNote: String, + val textNote: String +) { + override fun toString(): String = nameNote + +} +