From d8e51de0b6aea5cad1f0ac2116d8d7313d5d19b7 Mon Sep 17 00:00:00 2001 From: Katerina Date: Mon, 8 Dec 2025 16:16:37 +0400 Subject: [PATCH 1/2] =?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=D0=9A?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D1=80=D0=BA=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- src/main/kotlin/DataClasses.kt | 14 ++++ src/main/kotlin/Main.kt | 8 ++- src/main/kotlin/Menu.kt | 61 ++++++++++++++++ src/main/kotlin/NotesApp.kt | 128 +++++++++++++++++++++++++++++++++ 5 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/DataClasses.kt create mode 100644 src/main/kotlin/Menu.kt create mode 100644 src/main/kotlin/NotesApp.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..47478b91 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/DataClasses.kt b/src/main/kotlin/DataClasses.kt new file mode 100644 index 00000000..6826f508 --- /dev/null +++ b/src/main/kotlin/DataClasses.kt @@ -0,0 +1,14 @@ +import java.time.LocalDateTime // + +class Archive(val name: String) { + private val _notes = mutableListOf() + val notes: List get() = _notes + + fun addNote(note: Note) { + _notes.add(note) + } +} + +class Note(val title: String, val content: String) { + val createdAt: LocalDateTime = LocalDateTime.now() // +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..64626057 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,7 @@ -fun main(args: Array) { - println("Hello World!") +import java.util.Scanner + +fun main() { + val scanner = Scanner(System.`in`) + val app = NotesApp(scanner) + app.start() } \ No newline at end of file diff --git a/src/main/kotlin/Menu.kt b/src/main/kotlin/Menu.kt new file mode 100644 index 00000000..ec6f45e0 --- /dev/null +++ b/src/main/kotlin/Menu.kt @@ -0,0 +1,61 @@ +import java.util.Scanner + +data class MenuItem( + val name: String, + val action: () -> Unit +) + +class Menu( + private val title: String, + private val scanner: Scanner, + private val items: MutableList, + private val exitAction: () -> Unit = {} +) { + fun show() { + var shouldContinue = true + + while (shouldContinue) { + printMenu() + shouldContinue = handleInput() + } + } + + private fun printMenu() { + println("\n=== $title ===") + + if (items.isEmpty()) { + println("Список пуст") + } else { + items.forEachIndexed { index, item -> + println("${index + 1}. ${item.name}") + } + } + + println("0. ${if (title == "Список архивов") "Выход" else "Назад"}") + print("Выберите пункт меню: ") + } + + private fun handleInput(): Boolean { + val input = scanner.nextLine().trim() + val choice = input.toIntOrNull() + + return when { + choice == null -> { + println("Ошибка: необходимо ввести число. Получено: '$input'") + true // Продолжаем показ меню + } + choice == 0 -> { + exitAction() + false // Завершаем показ этого меню + } + choice > 0 && choice <= items.size -> { + items[choice - 1].action() + false // После выполнения действия выходим из этого меню + } + else -> { + println("Ошибка: пункта с номером $choice не существует") + true // Продолжаем показ меню + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/NotesApp.kt b/src/main/kotlin/NotesApp.kt new file mode 100644 index 00000000..3e64693b --- /dev/null +++ b/src/main/kotlin/NotesApp.kt @@ -0,0 +1,128 @@ +import java.util.Scanner + +class NotesApp(private val scanner: Scanner) { + private val archives = mutableListOf() + + fun start() { + showArchivesMenu() + } + + private fun showArchivesMenu() { + var shouldShowArchivesMenu = true + + while (shouldShowArchivesMenu) { + val menuItems = mutableListOf() + + // Пункт "Создать архив" + menuItems.add(MenuItem("Создать архив") { + createArchive() + // После создания архива просто выходим - меню перезапустится + }) + + // Существующие архивы + archives.forEach { archive -> + menuItems.add(MenuItem(archive.name) { + showNotesMenu(archive) + }) + } + + val archivesMenu = Menu( + title = "Список архивов", + scanner = scanner, + items = menuItems, + exitAction = { + println("Выход из программы.") + System.exit(0) + } + ) + + archivesMenu.show() + } + } + + private fun createArchive() { + println("\n=== Создание архива ===") + + while (true) { + print("Введите название архива: ") + val name = scanner.nextLine().trim() + + when { + name.isEmpty() -> println("Название архива не может быть пустым.") + archives.any { it.name == name } -> println("Архив с таким названием уже существует.") + else -> { + archives.add(Archive(name)) + println("Архив '$name' успешно создан!") + return + } + } + } + } + + private fun showNotesMenu(archive: Archive) { + var shouldShowNotesMenu = true + + while (shouldShowNotesMenu) { + val menuItems = mutableListOf() + + // Пункт "Создать заметку" + menuItems.add(MenuItem("Создать заметку") { + createNote(archive) + // После создания заметки меню перезапустится автоматически + }) + + // Существующие заметки + archive.notes.forEach { note -> + menuItems.add(MenuItem(note.title) { + showNoteContent(note) + // После просмотра заметки меню перезапустится автоматически + }) + } + + val notesMenu = Menu( + title = "Архив: ${archive.name}", + scanner = scanner, + items = menuItems, + exitAction = { + // Выходим из цикла while, возвращаемся в меню архивов + shouldShowNotesMenu = false + } + ) + + notesMenu.show() + } + } + + private fun createNote(archive: Archive) { + println("\n=== Создание заметки ===") + + val title = getNonEmptyInput("Введите заголовок заметки: ", "Заголовок не может быть пустым.") + val content = getNonEmptyInput("Введите текст заметки: ", "Текст заметки не может быть пустым.") + + val note = Note(title, content) + archive.addNote(note) + println("Заметка '$title' успешно создана!") + } + + private fun showNoteContent(note: Note) { + println("\n${"=".repeat(50)}") + println("Заголовок: ${note.title}") + println("Дата создания: ${note.createdAt}") + println("${"-".repeat(50)}") + println(note.content) + println("${"=".repeat(50)}") + println("\nНажмите Enter для продолжения...") + scanner.nextLine() + } + + private fun getNonEmptyInput(prompt: String, errorMessage: String): String { + while (true) { + print(prompt) + val input = scanner.nextLine().trim() + if (input.isNotEmpty()) { + return input + } + println(errorMessage) + } + } +} \ No newline at end of file From ca16b6180e8b62f8cb50bccac5897eb278de3c12 Mon Sep 17 00:00:00 2001 From: Katerina Date: Mon, 8 Dec 2025 16:22:50 +0400 Subject: [PATCH 2/2] =?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=D0=9A?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D1=80=D0=BA=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/deviceStreaming.xml | 978 +++++++++++++++++++++++++++++++ 1 file changed, 978 insertions(+) create mode 100644 .idea/caches/deviceStreaming.xml diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 00000000..4f522242 --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,978 @@ + + + + + + \ No newline at end of file