From 7818e60d7e58dc6c44dd46ed40255453b2e0be0a Mon Sep 17 00:00:00 2001 From: artem Date: Sat, 11 Jan 2025 18:20:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=B7-3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/deviceStreaming.xml | 340 +++++++++++++++++++++++++++++++ .idea/misc.xml | 2 +- src/main/kotlin/Archive.kt | 39 ++++ src/main/kotlin/Main.kt | 2 +- src/main/kotlin/Menu.kt | 46 +++++ src/main/kotlin/Note.kt | 46 +++++ 6 files changed, 473 insertions(+), 2 deletions(-) create mode 100644 .idea/caches/deviceStreaming.xml create mode 100644 src/main/kotlin/Archive.kt create mode 100644 src/main/kotlin/Menu.kt create mode 100644 src/main/kotlin/Note.kt diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 00000000..406736c9 --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,340 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 9c8e7400..66f29f60 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..6d334346 --- /dev/null +++ b/src/main/kotlin/Archive.kt @@ -0,0 +1,39 @@ +import java.util.Scanner + +class Archive(var name: String) { + val notes = mutableListOf() +} + +object ArchiveManager { + private val archives = mutableListOf() + private val scanner = Scanner(System.`in`) + + fun showArchives() { + val archiveMenu = Menu("Список архивов") + archiveMenu.addCommand("Создать архив", action = ({createArchive() })) + archives.forEach() { archive -> + archiveMenu.addCommand(archive.name, action = ({showNotes(archive) })) + } + archiveMenu.showMenuScreen() + } + + private fun createArchive() { + while (true) { + print("Введите имя архива: ") + val name = scanner.nextLine() + if (name.isNotEmpty()) { + archives.add(Archive(name)) + println("Архив создан!") + break + } else { + println("Введите непустое имя архива") + } + } + showArchives() + } + + private fun showNotes(archive: Archive) { + NoteManager.showNotes(archive) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index aade54c5..b8fe20c3 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,3 +1,3 @@ fun main(args: Array) { - println("Hello World!") + ArchiveManager.showArchives() } \ 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..b31b2675 --- /dev/null +++ b/src/main/kotlin/Menu.kt @@ -0,0 +1,46 @@ +import java.util.Scanner + +class Menu(private val screenName: String) { + private val scanner = Scanner(System.`in`) + private val commands = mutableListOf() + + fun addCommand(name: String, action: () -> Unit) { + commands.add(MenuItem(name, action)) + } + + fun showMenuScreen() { + + while (true) { + screenOut() + val choice = choice() + if (choice == 0) { + break + } else if (choice in 1..commands.size) { + commands[choice - 1].action() + } else { + println("Неверный ввод") + + } + } + } + + private fun choice(): Int { + while (true) { + print("Введите число: ") + try { + return scanner.nextLine().toInt() + } catch (e: NumberFormatException) { + println("Пожалуйста, введите число.") + } + } + } + private fun screenOut(){ + println("\n$screenName") + commands.forEachIndexed { index, item -> + println("${index + 1}. ${item.title}") + } + println("0. Выход") + } +} + +class MenuItem(val title: String, val action: () -> Unit) \ 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..077aef75 --- /dev/null +++ b/src/main/kotlin/Note.kt @@ -0,0 +1,46 @@ +import java.util.Scanner + +class Note(var title: String, var text: String){ + @Override + override fun toString(): String { + return title + } +} + +object NoteManager { + private val scanner = Scanner(System.`in`) + + fun showNotes(archive: Archive) { + val noteMenu = Menu("Список заметок в '${archive.name}'") + noteMenu.addCommand("Создать заметку", action = {-> createNote(archive) }) + archive.notes.forEach() {note -> + noteMenu.addCommand(note.title, action = {-> readNote(note)}) + } + noteMenu.showMenuScreen() + } + + private fun createNote(archive: Archive) { + while (true) { + print("Введите название: ") + val title = scanner.nextLine() + if (title.isEmpty()) { + println("Введите непустое название") + continue + } + print("Введите текст: ") + val text = scanner.nextLine() + if (text.isEmpty()) { + println("Введите непустое содержание") + continue + } + archive.notes.add(Note(title, text)) + println("Заметка создана!") + break + } + showNotes(archive) + } + + private fun readNote(note: Note) { + println("\n${note.text}") + } +} \ No newline at end of file