diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 00000000..be40440d --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,546 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..827bc035 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..9e94ac0e --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,51 @@ + +class Archive(val name: String) { + private val _notes = mutableListOf() + val notes: List + get() = _notes.toList() + + fun addNote(note: Note) { + _notes.add(note) + } +} + +fun handleArchive(archive: Archive, menu: Menu) { + while (true) { + val noteItems = mutableListOf("Создать заметку", "Назад") + archive.notes.forEachIndexed { index, note -> + noteItems.add(index + 1, note.title) + } + + val choice = menu.showMenu(noteItems, "Список заметок в архиве '${archive.name}':") + + when (choice) { + 0 -> { + print("Введите название заметки: ") + val title = readLine() + if (title.isNullOrEmpty()) { + println("Название заметки не может быть пустым.") + continue + } + print("Введите текст заметки: ") + val content = readLine() + if (content.isNullOrEmpty()) { + println("Текст заметки не может быть пустым.") + continue + } + archive.addNote(Note(title, content)) + println("Заметка '$title' создана.") + } + noteItems.size - 1 -> { + return + } + else -> { + val note = archive.notes[choice - 1] + println("Заметка: ${note.title}") + println("Текст: ${note.content}") + println("Нажмите Enter чтобы вернуться назад.") + readLine() + } + } + } +} + diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..1df03237 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,33 @@ -fun main(args: Array) { - println("Hello World!") -} \ No newline at end of file + +fun main() { + val menu = Menu() + val archives = mutableListOf() + + while (true) { + val archiveItems = mutableListOf("Создать архив", "Выход") + archives.forEachIndexed { index, archive -> + archiveItems.add(index + 1, archive.name) + } + + when (val choice = menu.showMenu(archiveItems, "Список архивов:")) { + 0 -> { + print("Введите имя архива: ") + val name = readlnOrNull() + if (name.isNullOrEmpty()) { + println("Имя архива не может быть пустым.") + } else { + archives.add(Archive(name)) + println("Архив '$name' создан.") + } + } + archiveItems.size - 1 -> { + println("Выход из программы.") + return + } + else -> { + val archive = archives[choice - 1] + handleArchive(archive, menu) + } + } + } +} diff --git a/src/main/kotlin/Menu.kt b/src/main/kotlin/Menu.kt new file mode 100644 index 00000000..d8f26e09 --- /dev/null +++ b/src/main/kotlin/Menu.kt @@ -0,0 +1,24 @@ + + +class Menu { + fun showMenu(items: List, prompt: String): Int { + while (true) { + println(prompt) + items.forEachIndexed { index, item -> + println("$index. $item") + } + print("Выберите пункт: ") + val input = readLine() + if (input.isNullOrEmpty() || !input.matches(Regex("\\d+"))) { + println("Пожалуйста, введите цифру.") + continue + } + val choice = input.toInt() + if (choice < 0 || choice >= items.size) { + println("Такого пункта нет. Пожалуйста, выберите снова.") + continue + } + return choice + } + } +} diff --git a/src/main/kotlin/Note.kt b/src/main/kotlin/Note.kt new file mode 100644 index 00000000..ee2b5843 --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1,6 @@ + + +data class Note( + val title: String, + val content: String +)