diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..52b96137 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/Main.kt b/src/main/kotlin/Main.kt index aade54c5..22571423 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,140 @@ -fun main(args: Array) { - println("Hello World!") +import java.util.Scanner + +data class Archive(val name: String, val notes: MutableList = mutableListOf()) +data class Note(val title: String, val content: String) + +open class Menu(private val title: String) { + protected val scanner = Scanner(System.`in`) + protected val items = mutableListOf>() + + data class MenuItem(val name: String, val action: () -> T) + + fun show(): T { + while (true) { + println("\n$title") + items.forEachIndexed { index, item -> + println("$index. ${item.name}") + } + + print("Выберите пункт меню: ") + val input = scanner.nextLine() + + try { + val choice = input.toInt() + if (choice in items.indices) { + return items[choice].action() + } else { + println("Ошибка: пункт $choice не существует. Пожалуйста, выберите из списка.") + } + } catch (e: NumberFormatException) { + println("Ошибка: пожалуйста, введите число.") + } + } + } + + protected fun readNonEmptyString(prompt: String): String { + while (true) { + print(prompt) + val input = scanner.nextLine().trim() + if (input.isNotEmpty()) { + return input + } + println("Ошибка: значение не может быть пустым. Попробуйте еще раз.") + } + } +} + +class NoteMenu(private val archive: Archive) : Menu("Архив: ${archive.name}") { + + init { + updateMenuItems() + } + + private fun updateMenuItems() { + items.clear() + + items.add(MenuItem("Создать заметку") { + createNote() + updateMenuItems() + Unit + }) + + archive.notes.forEach { note -> + items.add(MenuItem(note.title) { + viewNote(note) + updateMenuItems() + Unit + }) + } + + items.add(MenuItem("Назад") { + Unit + }) + } + + private fun createNote() { + val title = readNonEmptyString("Введите заголовок заметки: ") + val content = readNonEmptyString("Введите текст заметки: ") + archive.notes.add(Note(title, content)) + println("Заметка '$title' успешно создана!") + } + + private fun viewNote(note: Note) { + println("\nЗаголовок: ${note.title}") + println("Текст заметки:") + println(note.content) + print("\nНажмите Enter для возврата...") + scanner.nextLine() + } +} + +class ArchiveMenu(private val archives: MutableList) : Menu("Список архивов") { + + init { + updateMenuItems() + } + + private fun updateMenuItems() { + items.clear() + + items.add(MenuItem("Создать архив") { + createArchive() + updateMenuItems() + Unit + }) + + archives.forEach { archive -> + items.add(MenuItem(archive.name) { + NoteMenu(archive).show() + updateMenuItems() + Unit + }) + } + + items.add(MenuItem("Выход") { + println("До свидания!") + kotlin.system.exitProcess(0) + }) + } + + private fun createArchive() { + val name = readNonEmptyString("Введите имя архива: ") + archives.add(Archive(name)) + println("Архив '$name' успешно создан!") + } + + fun start() { + while (true) { + show() + } + } +} + +fun main() { + println("Приложение Заметки") + + val archives = mutableListOf() + val archiveMenu = ArchiveMenu(archives) + + archiveMenu.start() } \ No newline at end of file