From 89183afe29d10cd3848ded82621aa04b1484b331 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 8 Feb 2025 21:28:56 +0300 Subject: [PATCH 1/3] ss --- src/main/kotlin/Main.kt | 128 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..f2f3973b 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,127 @@ -fun main(args: Array) { - println("Hello World!") +import java.util.Scanner +fun main() { + val archives = mutableListOf() + while (true){ + val option = MenuHelper.getUserOption( + """ + Введите 0, чтобы создать архив + Введите 1, чтобы посмотреть созданные архивы + Введите 2, чтобы выйти + """.trimIndent(), 0, 2 + ) + + + when (option){ + 0 -> createArchive(archives) + 1 -> showArchives(archives) + 2 -> { + println("Завершаем программу") + break + } + else -> println("Некоректный ввод, введите цифру от 0 до 2") + + } + } +} +fun createArchive(archives: MutableList){ + + while (true){ + println("Введите имя архива") + val name = readLine() ?: "" + if (archives.any { it.name == name }) { + println("Архив с таким именем уже существует. Выберите другое имя.") + continue + } + if (name.isEmpty()){ + println("Имя архива не может быть пустым") + continue + }else{ + archives.add(Archive(name)) + println("Архив $name создан") + break + } + + } + +} +fun showArchives(archives: MutableList){ + if (archives.isEmpty()) { + println("У вас пока нет созданных архивов.") + return + } + println("Ваши архивы:") + archives.forEach{ + i -> println(i.name) + } + val put = MenuHelper.getUserOption( + """ +Введите номер архива, чтобы управлять им, или введите -1 для возврата: +""".trimIndent(), -1, archives.size - 1 + ) + if (put == -1){ + return + } + val archive = archives[put] + manage(archive) + +} +fun manage(archive: Archive) { + while (true) { + val option = MenuHelper.getUserOption( + """ + Введите 0, чтобы добавить заметку + Введите 1, чтобы просмотреть заметки + Введите 2, чтобы вернуться назад + """.trimIndent(), 0, 2 + ) + when (option) { + 0 -> createNote(archive) + 1 -> showNotes(archive) + 2 -> return + } + } +} +fun createNote(archive: Archive) { + var title: String + while (true) { + println("Введите название заметки") + title = readLine() ?: "" + if (archive.notes.any { it.title == title }) { + println("Заметка с таким названием уже существует в этом архиве. Пожалуйста, выберите другое название.") + continue + } + if (title.isEmpty()) { + println("Название заметки не может быть пустым") + continue + }else {break} + + } + + println("Введите содержание заметки") + val content = readLine() ?: "" + archive.notes.add(Note(title, content)) + println("Заметка \"$title\" добавлена в архив \"${archive.name}\".") +} + +fun showNotes(archive: Archive){ + archive.notes.forEach{ + i -> println("Название заметки ${i.title}. Содержание: ${i.content}") + } +} +data class Archive(val name: String, val notes: MutableList = mutableListOf()) + +data class Note(val title: String, val content: String) + +object MenuHelper { + fun getUserOption(prompt: String, minOption: Int, maxOption: Int): Int { + while (true) { + println(prompt) + val input = readLine() + val option = input?.toIntOrNull() + if (option != null && option in minOption..maxOption) { + return option + } + println("Некорректный ввод! Пожалуйста, введите число от $minOption до $maxOption.") + } + } } \ No newline at end of file From 0cc72186cc89c4a5eefdd0c07cbf81abac048e77 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 8 Feb 2025 21:32:57 +0300 Subject: [PATCH 2/3] ss --- src/main/kotlin/Main.kt | 15 --------------- src/main/kotlin/arch.kt | 3 +++ src/main/kotlin/menu.kt | 13 +++++++++++++ 3 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/arch.kt create mode 100644 src/main/kotlin/menu.kt diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index f2f3973b..7ecf8ded 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -108,20 +108,5 @@ fun showNotes(archive: Archive){ i -> println("Название заметки ${i.title}. Содержание: ${i.content}") } } -data class Archive(val name: String, val notes: MutableList = mutableListOf()) -data class Note(val title: String, val content: String) -object MenuHelper { - fun getUserOption(prompt: String, minOption: Int, maxOption: Int): Int { - while (true) { - println(prompt) - val input = readLine() - val option = input?.toIntOrNull() - if (option != null && option in minOption..maxOption) { - return option - } - println("Некорректный ввод! Пожалуйста, введите число от $minOption до $maxOption.") - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/arch.kt b/src/main/kotlin/arch.kt new file mode 100644 index 00000000..45e369a6 --- /dev/null +++ b/src/main/kotlin/arch.kt @@ -0,0 +1,3 @@ +data class Archive(val name: String, val notes: MutableList = mutableListOf()) + +data class Note(val title: String, val content: String) \ 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..422ba9f8 --- /dev/null +++ b/src/main/kotlin/menu.kt @@ -0,0 +1,13 @@ +object MenuHelper { + fun getUserOption(prompt: String, minOption: Int, maxOption: Int): Int { + while (true) { + println(prompt) + val input = readLine() + val option = input?.toIntOrNull() + if (option != null && option in minOption..maxOption) { + return option + } + println("Некорректный ввод! Пожалуйста, введите число от $minOption до $maxOption.") + } + } +} \ No newline at end of file From 725fc0e5688c493bb9057ed5f67a44fab8da79a5 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Feb 2025 20:17:43 +0300 Subject: [PATCH 3/3] ss --- src/main/kotlin/Main.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 7ecf8ded..48c4f211 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -96,11 +96,19 @@ fun createNote(archive: Archive) { }else {break} } + while (true){ + println("Введите содержание заметки") + val content = readLine() ?: "" + if (content.isEmpty()){ + println("заметка не может быть пустой") + continue + }else{ + archive.notes.add(Note(title, content)) + println("Заметка \"$title\" добавлена в архив \"${archive.name}\".") + break + } - println("Введите содержание заметки") - val content = readLine() ?: "" - archive.notes.add(Note(title, content)) - println("Заметка \"$title\" добавлена в архив \"${archive.name}\".") + } } fun showNotes(archive: Archive){