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/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..e621ecc3 --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,5 @@ + +class Archive(val name: String) { + val notes = mutableListOf() +} + diff --git a/src/main/kotlin/ArchiveMenu.kt b/src/main/kotlin/ArchiveMenu.kt new file mode 100644 index 00000000..e1e22586 --- /dev/null +++ b/src/main/kotlin/ArchiveMenu.kt @@ -0,0 +1,56 @@ + +import menu.NoteMenu +import java.util.Scanner + +class ArchiveMenu { + + private val archives = mutableListOf() + private val scanner = Scanner(System.`in`) + + fun start() { + val menu = Menu("Список архивов") + + menu.addItem("Создать архив") { createArchive() } + menu.addItem("Открыть архив") { openArchive() } + menu.addItem("Выход") { + println("Выход из программы") + System.exit(0) + } + + menu.show() + } + + private fun createArchive() { + println("Введите имя архива:") + val name = scanner.nextLine() + + if (name.isBlank()) { + println("Имя не может быть пустым") + return + } + + archives.add(Archive(name)) + println("Архив создан") + } + + private fun openArchive() { + if (archives.isEmpty()) { + println("Архивов нет") + return + } + + archives.forEachIndexed { index, archive -> + println("$index. ${archive.name}") + } + + println("Введите номер архива:") + val index = scanner.nextLine().toIntOrNull() + + if (index == null || index !in archives.indices) { + println("Неверный номер") + return + } + + NoteMenu(archives[index]).start() + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..57d57f22 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,7 @@ -fun main(args: Array) { - println("Hello World!") -} \ No newline at end of file + + +fun main() { + val archiveMenu = ArchiveMenu() + archiveMenu.start() +} +// Д \ 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..1500f5d3 --- /dev/null +++ b/src/main/kotlin/Menu.kt @@ -0,0 +1,48 @@ + + + +import java.util.Scanner + + +class Menu(private val title: String) { + + private val items = mutableListOf() + private val scanner = Scanner(System.`in`) + + fun addItem(title: String, exit: Boolean = false, action: () -> Unit) { + items.add(MenuItem(title, action, exit)) + } + + fun show() { + + loop@ while (true) { + + println("\n$title") + + items.forEachIndexed { index, item -> + println("$index. ${item.title}") + } + + val input = scanner.nextLine() + val number = input.toIntOrNull() + + if (number == null) { + println("Введите цифру") + continue + } + + if (number !in items.indices) { + println("Такой цифры нет") + continue + } + + val item = items[number] + + item.action() + + if (item.exit) { + break + } + } + } +} \ 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..cae8a3bd --- /dev/null +++ b/src/main/kotlin/MenuItem.kt @@ -0,0 +1,5 @@ +class MenuItem( + val title: String, + val action: () -> Unit, + val exit: 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..f544935c --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1,3 @@ + + +class Note(val title: String, val text: 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..49d29724 --- /dev/null +++ b/src/main/kotlin/NoteMenu.kt @@ -0,0 +1,90 @@ +package menu + +import Archive +import Menu +import Note +import NoteScreen +import java.util.Scanner + +class NoteMenu(private val archive: Archive) { + + private val scanner = Scanner(System.`in`) + + fun start() { + val menu = Menu("Архив: ${archive.name}") + + menu.addItem("Создать заметку") { createNote() } + menu.addItem("Открыть заметку") { openNote() } + menu.addItem("Назад", exit = true) {} + + menu.show() + } + + private fun createNote() { + println("Введите название заметки:") + val title = scanner.nextLine() + + if (title.isBlank()) { + println("Название не может быть пустым") + return + } + + println("Введите текст заметки:") + val text = scanner.nextLine() + + if (text.isBlank()) { + println("Текст не может быть пустым") + return + } + + archive.notes.add(Note(title, text)) + println("Заметка создана") + } + + private fun openNote() { + if (archive.notes.isEmpty()) { + println("Заметок нет") + return + } + + archive.notes.forEachIndexed { index, note -> + println("$index. ${note.title}") + } + + println("Введите номер заметки:") + val index = scanner.nextLine().toIntOrNull() + + if (index == null || index !in archive.notes.indices) { + println("Неверный номер") + return + } + + NoteScreen(archive.notes[index]).show() + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/kotlin/NoteScreen.kt b/src/main/kotlin/NoteScreen.kt new file mode 100644 index 00000000..81146efa --- /dev/null +++ b/src/main/kotlin/NoteScreen.kt @@ -0,0 +1,11 @@ + +import java.util.Scanner + +class NoteScreen(private val note: Note) { + fun show() { + println("\nЗаметка: ${note.title}") + println(note.text) + println("\nНажмите Enter чтобы вернуться") + Scanner(System.`in`).nextLine() + } +}