diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..48c4f211 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,120 @@ -fun main(args: Array) { - println("Hello World!") -} \ No newline at end of file +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} + + } + while (true){ + println("Введите содержание заметки") + val content = readLine() ?: "" + if (content.isEmpty()){ + println("заметка не может быть пустой") + continue + }else{ + archive.notes.add(Note(title, content)) + println("Заметка \"$title\" добавлена в архив \"${archive.name}\".") + break + } + + } +} + +fun showNotes(archive: Archive){ + archive.notes.forEach{ + i -> println("Название заметки ${i.title}. Содержание: ${i.content}") + } +} + + 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