diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 00000000..d966f1b5 --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,1450 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deviceManager.xml b/.idea/deviceManager.xml new file mode 100644 index 00000000..91f95584 --- /dev/null +++ b/.idea/deviceManager.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/.idea/markdown.xml b/.idea/markdown.xml new file mode 100644 index 00000000..c61ea334 --- /dev/null +++ b/.idea/markdown.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..cf905821 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,5 @@ - - + \ No newline at end of file diff --git a/Kotlin-Module-Project.iml b/Kotlin-Module-Project.iml index 4eba30bc..2b259895 100644 --- a/Kotlin-Module-Project.iml +++ b/Kotlin-Module-Project.iml @@ -7,6 +7,7 @@ + diff --git a/src/main/kotlin/Archive.kt b/src/main/kotlin/Archive.kt new file mode 100644 index 00000000..26b6a0b9 --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,4 @@ +data class Archive( + val name: String, + val notes: MutableList = mutableListOf() +) diff --git a/src/main/kotlin/ArchiveMenu.kt b/src/main/kotlin/ArchiveMenu.kt new file mode 100644 index 00000000..ac572f8a --- /dev/null +++ b/src/main/kotlin/ArchiveMenu.kt @@ -0,0 +1,50 @@ +class ArchiveMenu( + private val archives: MutableList + +) { + fun start() { + + while (true) { + //var needRefresh = false + val menuItems = mutableListOf() + + // 0 — создание архива + menuItems.add(MenuScreen.MenuItem("Создать архив") { + createArchive() + // needRefresh = true + }) + + // Существующие архивы + archives.forEachIndexed { index, archive -> + menuItems.add(MenuScreen.MenuItem(archive.name) { + NoteListScreen(archive).start() + }) + } + + // Последним всегда выход + menuItems.add(MenuScreen.exitItem { + println("\nДо свидания!") + kotlin.system.exitProcess(0) + }) + + val screen = MenuScreen( + title = "Список архивов:", + items = menuItems + ) + + screen.show() + + } + } + + private fun createArchive() { + println("Начало создания архива") + println("\nВведите название архива:") + val name = readNotEmptyString("Название архива не может быть пустым") + println("Имя получено: '$name'") + archives.add(Archive(name)) + println("Архив $name успешно создан.") + println("Архив добавлен, теперь размер списка = ${archives.size}") + + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..0d7897fb 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,22 @@ -fun main(args: Array) { - println("Hello World!") -} \ No newline at end of file +//fun main(args: Array) { +fun main() { + val archives = mutableListOf() + + ArchiveMenu(archives).start() + //println("Hello World!") + + +} + +fun readNotEmptyString(errorMessage: String): String { + val scanner = java.util.Scanner(System.`in`) + while (true) { + val input = scanner.nextLine().trim() + if (input.isNotBlank()) { + return input + } + println() + println(errorMessage) + print("Попробуйте снова: ") + } +} diff --git a/src/main/kotlin/MenuScreen.kt b/src/main/kotlin/MenuScreen.kt new file mode 100644 index 00000000..4ce3285f --- /dev/null +++ b/src/main/kotlin/MenuScreen.kt @@ -0,0 +1,46 @@ +class MenuScreen( + private val title: String, // Заголовок - "Список архивов:" + private val items: List, // список к выбору + //private val onExit: () -> Unit = {} // действие при выходе +) { + data class MenuItem( + val text: String, // элементы меню + val action: () -> Unit // действие при выборе элемента меню + ) + + private val scanner = java.util.Scanner(System.`in`) + fun show() { + while (true) { + println("$title") + items.forEachIndexed { index, item -> + println("$index. ${item.text}") + } + print("Выберите пункт меню: ") + + val input = scanner.nextLine().trim() + + val selectedIndex = input.toIntOrNull() + + when { + selectedIndex == null -> { + println("Ошибка: нужно ввести число.") + } + + selectedIndex < 0 || selectedIndex >= items.size -> { + println("Ошибка: такого пункта нет. Выберите число от 0 до ${items.size - 1}") + } + + else -> { + val selectedItem = items[selectedIndex] + selectedItem.action() // выполняем действие пункта меню + return + } + } + } + + } + + companion object { + fun exitItem(onExit: () -> Unit) = MenuItem("Выход") { onExit() } + } +} \ No newline at end of file diff --git a/src/main/kotlin/Note.kt b/src/main/kotlin/Note.kt new file mode 100644 index 00000000..03e67add --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1,5 @@ +data class Note( + val title: String, + val text: String + +) diff --git a/src/main/kotlin/NoteListScreen.kt b/src/main/kotlin/NoteListScreen.kt new file mode 100644 index 00000000..2a2f9c5d --- /dev/null +++ b/src/main/kotlin/NoteListScreen.kt @@ -0,0 +1,47 @@ +class NoteListScreen( + private val archive: Archive +) { + fun start() { + while (true) { + var exitRequested = false + val menuItems = mutableListOf() + + menuItems.add(MenuScreen.MenuItem("Создать заметку") { + createNote() + }) + + archive.notes.forEachIndexed { index, note -> + menuItems.add(MenuScreen.MenuItem(note.title) { + NoteViewScreen(note).show() + }) + + } + + + menuItems.add(MenuScreen.MenuItem("Назад") { + exitRequested = true + }) + + val screen = MenuScreen( + title = "Архив: ${archive.name}\nЗаметки:", + items = menuItems + ) + + screen.show() + if (exitRequested) { + return // выход из функции start() и возврат в меню архивов + } + } + } + + private fun createNote() { + println("\nВведите заголовок заметки:") + val title = readNotEmptyString("Заголовок не может быть пустым") + + println("Введите текст заметки:") + val text = readNotEmptyString("Текст заметки не может быть пустым") + + archive.notes.add(Note(title, text)) + println("Заметка «$title» сохранена.") + } +} \ No newline at end of file diff --git a/src/main/kotlin/NoteViewScreen.kt b/src/main/kotlin/NoteViewScreen.kt new file mode 100644 index 00000000..acdb1ffd --- /dev/null +++ b/src/main/kotlin/NoteViewScreen.kt @@ -0,0 +1,11 @@ +class NoteViewScreen( + private val note: Note +) { + fun show() { + println("Заметка: ${note.title}") + println(note.text) + println("Нажмите Enter для возврата...") + readln() + // После нажатия Enter возврат в предыдущее меню + } +} \ No newline at end of file