From 5796d89cd71b774c80b9cb2a526a629d0749c246 Mon Sep 17 00:00:00 2001 From: ALRT Date: 2024年9月18日 21:55:13 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=81=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5(=D0=B0=D1=80=D1=85=D0=B8=D0=B2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- .idea/other.xml | 318 ++++++++++++++++++++++++++++++++++++++++ Readme.md | 1 - src/main/kotlin/Main.kt | 33 ++++- 4 files changed, 349 insertions(+), 5 deletions(-) create mode 100644 .idea/other.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..a6e1098c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/other.xml b/.idea/other.xml new file mode 100644 index 00000000..94c96f63 --- /dev/null +++ b/.idea/other.xml @@ -0,0 +1,318 @@ + + + + + + \ No newline at end of file diff --git a/Readme.md b/Readme.md index 996ac25e..e69de29b 100644 --- a/Readme.md +++ b/Readme.md @@ -1 +0,0 @@ -# Пустой репозиторий для работы с Kotlin кодом в Android Studio diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..37092fff 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,30 @@ -fun main(args: Array) { - println("Hello World!") -} \ No newline at end of file +import java.util.Scanner + +fun main() { + val menuHandler = MenuHandler() + val archives = mutableListOf() + 1 + menuHandler.showMenu("Меню архивов", listOf("Создать архив" to { + println("Введите название архива:") + val name = readNonEmptyInput() + archives.add(Archive(name)) + println("Архив '$name' создан.") + }, "Просмотреть архивы" to { + if (archives.isEmpty()) { + println("Нет доступных архивов.") + } else { + menuHandler.showMenu( + "Выбор архива", + archives.map { it.name to { it.viewNotes(menuHandler) } }) + } + })) +} + + +fun readNonEmptyInput(): String { + while (true) { + val input = Scanner(System.`in`).nextLine() + if (input.isNotBlank()) return input + else println("Поле не может быть пустым. Попробуйте снова:") + } +} From 0e70f353dc5f0f2f811c86369c6383890b8f26bc Mon Sep 17 00:00:00 2001 From: ALRT Date: 2024年9月18日 21:58:09 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=81=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=90=D1=80=D1=85=D0=B8=D0=B2=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=BC=D0=B5=D1=82=D0=BE=D0=BA=20(=D0=98=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BD=D1=8B=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/Archive.kt | 38 ++++++++++++++++++++++++++++++++++ src/main/kotlin/Main.kt | 32 ++++++++++++++-------------- src/main/kotlin/MenuHandler.kt | 36 ++++++++++++++++++++++++++++++++ src/main/kotlin/Note.kt | 7 +++++++ 4 files changed, 97 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/Archive.kt create mode 100644 src/main/kotlin/MenuHandler.kt create mode 100644 src/main/kotlin/Note.kt diff --git a/src/main/kotlin/Archive.kt b/src/main/kotlin/Archive.kt new file mode 100644 index 00000000..227f9822 --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,38 @@ +import java.util.Scanner + +class Archive(val name: String) { + private val notes = mutableListOf() + + fun archiveMenu(menuHandler: MenuHandler) { + menuHandler.showMenu("Меню архива '$name'", listOf( + "Добавить заметку" to { addNote() }, + "Просмотреть заметки" to { viewNotes(menuHandler) } + )) + } + + fun addNote() { + println("Введите название заметки:") + val title = readNonEmptyInput() + println("Введите текст заметки:") + val content = readNonEmptyInput() + notes.add(Note(title, content)) + println("Заметка '$title' добавлена.") + } + + fun viewNotes(menuHandler: MenuHandler) { + if (notes.isEmpty()) { + println("Нет заметок в архиве.") + return + } + + menuHandler.showMenu("Выбор заметки", notes.map { it.title to { it.viewNote() } }) + } + + private fun readNonEmptyInput(): String { + while (true) { + val input = Scanner(System.`in`).nextLine() + if (input.isNotBlank()) return input + else println("Поле не может быть пустым. Попробуйте снова:") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 37092fff..3b25cc09 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -3,28 +3,28 @@ import java.util.Scanner fun main() { val menuHandler = MenuHandler() val archives = mutableListOf() - 1 - menuHandler.showMenu("Меню архивов", listOf("Создать архив" to { - println("Введите название архива:") - val name = readNonEmptyInput() - archives.add(Archive(name)) - println("Архив '$name' создан.") - }, "Просмотреть архивы" to { - if (archives.isEmpty()) { - println("Нет доступных архивов.") - } else { - menuHandler.showMenu( - "Выбор архива", - archives.map { it.name to { it.viewNotes(menuHandler) } }) + + menuHandler.showMenu("Меню архивов", listOf( + "Создать архив" to { + println("Введите название архива:") + val name = readNonEmptyInput() + archives.add(Archive(name)) + println("Архив '$name' создан.") + }, + "Просмотреть архивы" to { + if (archives.isEmpty()) { + println("Нет доступных архивов.") + } else { + menuHandler.showMenu("Выбор архива", archives.map { it.name to { it.archiveMenu(menuHandler) } }) + } } - })) + )) } - fun readNonEmptyInput(): String { while (true) { val input = Scanner(System.`in`).nextLine() if (input.isNotBlank()) return input else println("Поле не может быть пустым. Попробуйте снова:") } -} +} \ No newline at end of file diff --git a/src/main/kotlin/MenuHandler.kt b/src/main/kotlin/MenuHandler.kt new file mode 100644 index 00000000..e9352754 --- /dev/null +++ b/src/main/kotlin/MenuHandler.kt @@ -0,0 +1,36 @@ +import java.util.Scanner + +class MenuHandler { + private val scanner = Scanner(System.`in`) + + // Функция для вывода меню и обработки выбора + fun showMenu(title: String, options: List Unit>>) { + while (true) { + println("\n$title") + options.forEachIndexed { index, option -> + println("${index + 1}. ${option.first}") + } + println("0. Назад") + + val choice = readInput(options.size) + if (choice == 0) return // Выход из меню + options[choice - 1].second() // Вызов соответствующей лямбды + } + } + + // Функция для обработки пользовательского ввода + private fun readInput(max: Int): Int { + while (true) { + print("Введите номер пункта: ") + val input = scanner.nextLine() + + try { + val num = input.toInt() + if (num in 0..max) return num + else println("Нет такого пункта меню.") + } catch (e: NumberFormatException) { + println("Введите корректное число.") + } + } + } +} \ 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..145696b9 --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1,7 @@ +class Note(val title: String, private val content: String) { + + fun viewNote() { + println("\nЗаметка: $title") + println(content) + } +} \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /