diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..ff129a58 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/Archive.kt b/src/main/kotlin/Archive.kt new file mode 100644 index 00000000..a8da0699 --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,95 @@ +class Archive(val name: String) { + val notes = mutableListOf() + + init { + require(name.isNotBlank()) { "Название архива не может быть пустым." } + } + + fun addNote(note: Note) { + notes.add(note) + } +} +class MainMenu(private val noteManager: NoteManager) { + fun show() { + while (true) { + println("Главное меню:") + println("1. Создать архив") + println("2. Просмотреть архивы") + println("0. Выход") + + when (readLine()) { + "1" -> createArchive() + "2" -> viewArchives() + "0" -> return + else -> println("Некорректный выбор. Пожалуйста, попробуйте снова.") + } + } + } + + private fun createArchive() { + while (true) { + print("Введите название архива: ") + val name = readLine() ?: "" + try { + noteManager.addArchive(Archive(name)) + println("Архив создан! Нажмите любую клавишу для продолжения...") + readLine() + return + } catch (e: IllegalArgumentException) { + println(e.message) + } + } + } + + private fun viewArchives() { + if (noteManager.archives.isEmpty()) { + println("Нет доступных архивов.") + return + } + + println("Доступные архивы:") + noteManager.archives.forEachIndexed { index, archive -> + println("${index + 1}. ${archive.name}") + } + + print("Введите номер архива для просмотра заметок (или 0 для выхода): ") + val input = readLine()?.toIntOrNull() + if (input == 0) return + if (input != null && input in 1..noteManager.archives.size) { + viewNotesInArchive(input - 1) + } else { + println("Некорректный номер архива.") + } + } + + private fun viewNotesInArchive(archiveIndex: Int) { + while (true) { + noteManager.viewNotesInArchive(archiveIndex) + println("Хотите добавить новую заметку в архив? (да/нет)") + when (readLine()?.toLowerCase()) { + "да" -> addNoteToArchive(archiveIndex) + "нет" -> return + else -> println("Некорректный ввод. Пожалуйста, введите 'да' или 'нет'.") + } + } + } + + private fun addNoteToArchive(archiveIndex: Int) { + while (true) { + print("Введите заголовок заметки: ") + val title = readLine() ?: "" + print("Введите содержимое заметки: ") + val content = readLine() ?: "" + + try { + val note = Note(title, content) + noteManager.archives[archiveIndex].addNote(note) + println("Заметка добавлена! Нажмите любую клавишу для продолжения...") + readLine() + return + } catch (e: IllegalArgumentException) { + println(e.message) + } + } + } +} diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..9341f0e9 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,5 @@ -fun main(args: Array) { - println("Hello World!") +fun main() { + val noteManager = NoteManager() + val mainMenu = MainMenu(noteManager) + mainMenu.show() } \ No newline at end of file diff --git a/src/main/kotlin/NoteClass.kt b/src/main/kotlin/NoteClass.kt new file mode 100644 index 00000000..6b6c21ac --- /dev/null +++ b/src/main/kotlin/NoteClass.kt @@ -0,0 +1,39 @@ +class Note(val title: String, val content: String) { + init { + require(title.isNotBlank()) { "Название заметки не может быть пустым." } + } + init { + require(content.isNotBlank()) { "Содержимое заметки не может быть пустым." } + } +} +class NoteManager { + val archives = mutableListOf() + + fun addArchive(archive: Archive) { + archives.add(archive) + } + + fun viewNotesInArchive(archiveIndex: Int) { + val archive = archives[archiveIndex] + if (archive.notes.isEmpty()) { + println("В архиве '${archive.name}' нет заметок.") + } else { + println("Заметки в архиве '${archive.name}':") + archive.notes.forEachIndexed { index, note -> + println("${index + 1}. ${note.title}") + } + print("Введите номер заметки для просмотра (или 0 для выхода): ") + val input = readLine() + val noteIndex = input?.toIntOrNull() + if (noteIndex != null && noteIndex in 1..archive.notes.size) { + val selectedNote = archive.notes[noteIndex - 1] + println("Заголовок: ${selectedNote.title}") + println("Содержимое: ${selectedNote.content}") + } else if (noteIndex == 0) { + return + } else { + println("Некорректный номер заметки.") + } + } + } +} \ No newline at end of file