diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 00000000..6a3e9f5d --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,1550 @@ + + + + + + \ 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/src/main/kotlin/Archive.kt b/src/main/kotlin/Archive.kt new file mode 100644 index 00000000..e33c452c --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,3 @@ +class Archive(val name: String) { + val notes: MutableList = mutableListOf() +} \ No newline at end of file diff --git a/src/main/kotlin/ArchiveMenu.kt b/src/main/kotlin/ArchiveMenu.kt new file mode 100644 index 00000000..aee99024 --- /dev/null +++ b/src/main/kotlin/ArchiveMenu.kt @@ -0,0 +1,55 @@ +import java.util.Scanner + +class ArchiveMenu(private val archives: MutableList) { + + fun show() { + val menuItems = mutableListOf() + + menuItems.add( + MenuItem( + name = "Создать архив", + action = { + createArchive() + show() + } + )) + + archives.forEach { archive -> + menuItems.add( + MenuItem( + name = archive.name, + action = { + NoteMenu(archive).show() + show() + } + )) + } + + menuItems.add( + MenuItem( + name = "Выход", + action = { }, + isExit = true + ) + ) + + Menu("Список архивов:", menuItems).show() + } + + private fun createArchive() { + println("Введите название архива:") + val name = readNonEmptyString() + archives.add(Archive(name)) + println("Архив '$name' успешно создан!") + } + + private fun readNonEmptyString(): String { + while (true) { + val input = Scanner(System.`in`).nextLine() + if (input.isNotBlank()) { + return input + } + println("Ошибка: поле не может быть пустым. Попробуйте снова:") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..f8434ea9 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 archives = mutableListOf() + ArchiveMenu(archives).show() } \ 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..b3de3fa9 --- /dev/null +++ b/src/main/kotlin/Menu.kt @@ -0,0 +1,49 @@ +import java.util.Scanner + +class Menu(private val title: String, private val items: MutableList) { + + fun show() { + while (true) { + println("\n$title") + items.forEachIndexed { index, item -> + println("$index. ${item.name}") + } + + print("Выберите пункт меню: ") + val input = Scanner(System.`in`).nextLine() + + when { + !isValidNumber(input) -> { + println("Ошибка: введите целое число") + } + else -> { + val index = input.toInt() + if (index in items.indices) { + items[index].action() + if (items[index].isExit) { + break + } + } else { + println("Ошибка: пункта с номером $index не существует") + } + } + } + } + } + + private fun isValidNumber(str: String): Boolean { + // Проверяем, что строка не пустая и состоит только из цифр + if (str.isEmpty()) return false + // Проверяем, что все символы - цифры + for (c in str) { + if (!c.isDigit()) return false + } + // Проверяем, что число не выходит за пределы Int + try { + str.toInt() + return true + } catch (e: NumberFormatException) { + return false + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/MenuItem.kt b/src/main/kotlin/MenuItem.kt new file mode 100644 index 00000000..72cb09d8 --- /dev/null +++ b/src/main/kotlin/MenuItem.kt @@ -0,0 +1,5 @@ +class MenuItem( + val name: String, + val action: () -> Unit, + val isExit: Boolean = false +) \ 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..4c0fe23e --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1 @@ +class Note(val name: String, val content: String) \ No newline at end of file diff --git a/src/main/kotlin/NoteMenu.kt b/src/main/kotlin/NoteMenu.kt new file mode 100644 index 00000000..045b8d68 --- /dev/null +++ b/src/main/kotlin/NoteMenu.kt @@ -0,0 +1,59 @@ +import java.util.Scanner + +class NoteMenu(private val archive: Archive) { + + fun show() { + val menuItems = mutableListOf() + + menuItems.add( + MenuItem( + name = "Создать заметку", + action = { + createNote() + show() + } + )) + + archive.notes.forEach { note -> + menuItems.add( + MenuItem( + name = note.name, + action = { + NoteView(note).show() + show() + } + )) + } + + menuItems.add( + MenuItem( + name = "Назад", + action = { }, + isExit = true + ) + ) + + Menu("Архив: ${archive.name}", menuItems).show() + } + + private fun createNote() { + println("Введите название заметки:") + val name = readNonEmptyString() + + println("Введите текст заметки:") + val content = readNonEmptyString() + + archive.notes.add(Note(name, content)) + println("Заметка '$name' успешно создана!") + } + + private fun readNonEmptyString(): String { + while (true) { + val input = Scanner(System.`in`).nextLine() + if (input.isNotBlank()) { + return input + } + println("Ошибка: поле не может быть пустым. Попробуйте снова:") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/NoteView.kt b/src/main/kotlin/NoteView.kt new file mode 100644 index 00000000..30889f85 --- /dev/null +++ b/src/main/kotlin/NoteView.kt @@ -0,0 +1,13 @@ +import java.util.Scanner + +class NoteView(private val note: Note) { + + fun show() { + println("\nЗаметка: ${note.name}") + println("=".repeat(50)) + println(note.content) + println("=".repeat(50)) + println("Нажмите Enter, чтобы вернуться...") + Scanner(System.`in`).nextLine() + } +} \ No newline at end of file