diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..f515117b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..be7fb309 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,4 @@ -fun main(args: Array) { - println("Hello World!") +fun main() { + val notesApp = NotesApp() + notesApp.start() } \ 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..32962100 --- /dev/null +++ b/src/main/kotlin/NotesApp.kt @@ -0,0 +1,72 @@ +data class Archive(val id: Int, val name: String) +data class Note(val id: Int, val name: String, val content: String) + +class NotesApp { + val archives = mutableListOf() + val notes = mutableMapOf>() + private var archiveCounter = 0 + private var noteCounter = 0 + + fun start() { + val ui = UserInterface(this) + ui.start() + } + + fun createArchive() { + while (true) { + println("Введите название архива:") + val name = readLine()?.trim() + if (name.isNullOrEmpty()) { + println("Название архива не может быть пустым. Пожалуйста, попробуйте снова.") + } else { + archives.add(Archive(++archiveCounter, name)) + notes[archiveCounter] = mutableListOf() + println("Архив '$name' успешно создан.") + break + } + } + } + + fun displayArchives() { + if (archives.isEmpty()) { + println("Нет доступных архивов.") + } else { + archives.forEach { archive -> + println("${archive.id}. ${archive.name}") + } + } + } + + fun createNote(archiveId: Int) { + while (true) { + println("Введите название заметки:") + val name = readLine()?.trim() + if (name.isNullOrEmpty()) { + println("Название заметки не может быть пустым. Пожалуйста, попробуйте снова.") + } else { + println("Введите содержимое заметки:") + val content = readLine()?.trim() + if (content.isNullOrEmpty()) { + println("Содержимое заметки не может быть пустым. Пожалуйста, попробуйте снова.") + } else { + notes[archiveId]?.add(Note(++noteCounter, name, content)) + println("Заметка '$name' успешно добавлена.") + break + } + } + } + } + + fun displayNotes(archiveId: Int) { + notes[archiveId]?.forEach { note -> + println("${note.id}. ${note.name}") + } ?: println("Нет заметок в этом архиве.") + } + + fun viewNote(archiveId: Int, noteIndex: Int) { + val selectedNote = notes[archiveId]?.get(noteIndex - 1) + selectedNote?.let { + println("Содержимое заметки '${it.name}': ${it.content}") + } ?: println("Заметка не найдена.") + } +} diff --git a/src/main/kotlin/UserInterface.kt b/src/main/kotlin/UserInterface.kt new file mode 100644 index 00000000..f9b115ec --- /dev/null +++ b/src/main/kotlin/UserInterface.kt @@ -0,0 +1,80 @@ +class UserInterface(private val notesApp: NotesApp) { + + fun start() { + while (true) { + println("Список архивов:") + notesApp.displayArchives() + println("Введите номер архива для выбора, 0 для создания нового архива или -1 для выхода:") + if (!handleArchiveInput()) break + } + } + + fun selectArchive(archiveIndex: Int) { + val selectedArchive = notesApp.archives[archiveIndex - 1] + println("Выбрали архив: ${selectedArchive.name}.") + + while (true) { + println("Заметки в архиве '${selectedArchive.name}':") + notesApp.displayNotes(selectedArchive.id) + println("Введите номер заметки для выбора, 0 для создания новой заметки или -1 для выхода в меню архивов:") + if (!handleNoteInput(selectedArchive.id)) { + return + } + } + } + + private fun handleArchiveInput(): Boolean { + val input = readLine() ?: return true + val option = input.toIntOrNull() + + return when { + option == null -> { + println("Пожалуйста, вводите число.") + true + } + option == -1 -> { + println("Выход из программы.") + false + } + option == 0 -> { + notesApp.createArchive() + true + } + option in 1..notesApp.archives.size -> { + selectArchive(option) + true + } + else -> { + println("Такого элемента нет. Пожалуйста, попробуйте снова.") + true + } + } + } + + private fun handleNoteInput(archiveId: Int): Boolean { + val input = readLine() ?: return true + val option = input.toIntOrNull() + + return when { + option == null -> { + println("Пожалуйста, вводите число.") + true + } + option == -1 -> { + return false + } + option == 0 -> { + notesApp.createNote(archiveId) + true + } + notesApp.notes[archiveId]?.isNotEmpty() == true && option in 1..notesApp.notes[archiveId]!!.size -> { + notesApp.viewNote(archiveId, option) + true + } + else -> { + println("Такого элемента нет. Пожалуйста, попробуйте снова.") + true + } + } + } +}