-
Notifications
You must be signed in to change notification settings - Fork 0
"Готовое консольное приложение 'Заметки'"-1 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| fun main(args: Array<String>) { | ||
| println("Hello World!") | ||
| import models.Archive | ||
| import ui.ArchiveMenu | ||
| fun main() { | ||
| println("=== Приложение \"Заметки\" ===") | ||
|
|
||
| val archives = mutableListOf<Archive>() | ||
| val archiveMenu = ArchiveMenu(archives) | ||
|
|
||
| archiveMenu.show() | ||
|
|
||
| println("До свидания!") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package models | ||
|
|
||
| data class Archive( | ||
| val name: String, | ||
| val notes: MutableList<Note> = mutableListOf() | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package models | ||
| import models.Note | ||
|
|
||
| data class Note( | ||
| val title: String, | ||
| val content: String | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package ui | ||
| import models.Archive | ||
|
|
||
| class ArchiveMenu(private val archives: MutableList<Archive>) : Menu<Archive>( | ||
| title = "Меню архивов", | ||
| createItemText = "Создать архив" | ||
| ) { | ||
| override fun getItemsList(): List<Archive> = archives | ||
|
|
||
| override fun getItemName(item: Archive): String = item.name | ||
|
|
||
| override fun onItemSelected(item: Archive) { | ||
| val noteMenu = NoteMenu(item.notes) | ||
| noteMenu.show() | ||
| } | ||
|
|
||
| override fun onCreateItem() { | ||
| while (true) { | ||
| val name = ConsoleReader.readLine("Введите название архива: ") | ||
|
|
||
| if (name.isBlank()) { | ||
| println("Название архива не может быть пустым") | ||
| continue | ||
| } | ||
|
Comment on lines
+19
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
|
|
||
| if (archives.any { it.name.equals(name, ignoreCase = true) }) { | ||
| println("Архив с таким названием уже существует") | ||
| continue | ||
| } | ||
|
|
||
| archives.add(Archive(name)) | ||
| println("Архив \"$name\" создан") | ||
| break | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package ui | ||
|
|
||
| import java.util.Scanner | ||
|
|
||
| object ConsoleReader { | ||
| private val scanner = Scanner(System.`in`) | ||
|
|
||
| fun readLine(prompt: String): String { | ||
| print(prompt) | ||
| return scanner.nextLine().trim() | ||
| } | ||
|
|
||
| fun readInt(prompt: String): Int? { | ||
| print(prompt) | ||
| return try { | ||
| scanner.nextLine().toInt() | ||
| } catch (e: NumberFormatException) { | ||
| null | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package ui | ||
|
|
||
| abstract class Menu<T>( | ||
| private val title: String, | ||
| private val createItemText: String = "Создать", | ||
| private val exitText: String = "Выход" | ||
| ) { | ||
| private val items = mutableListOf<Pair<String, () -> Unit>>() | ||
|
|
||
| abstract fun getItemsList(): List<T> | ||
| abstract fun getItemName(item: T): String | ||
| abstract fun onItemSelected(item: T) | ||
| abstract fun onCreateItem() | ||
|
|
||
| fun show() { | ||
| while (true) { | ||
| updateMenuItems() | ||
| printMenu() | ||
|
|
||
| val input = ConsoleReader.readLine("Выберите пункт меню: ") | ||
|
|
||
| if (!validateInput(input)) { | ||
| continue | ||
| } | ||
|
|
||
| val choice = input.toInt() | ||
|
|
||
| if (!processChoice(choice)) { | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun updateMenuItems() { | ||
| items.clear() | ||
| items.add(Pair(createItemText) { onCreateItem() }) | ||
|
|
||
| getItemsList().forEachIndexed { index, item -> | ||
| items.add(Pair(getItemName(item)) { onItemSelected(item) }) | ||
| } | ||
|
|
||
| items.add(Pair(exitText) { /* Выход обрабатывается в processChoice */ }) | ||
| } | ||
|
|
||
| private fun printMenu() { | ||
| println("\n=== $title ===") | ||
| items.forEachIndexed { index, pair -> | ||
| println("$index. ${pair.first}") | ||
| } | ||
| } | ||
|
|
||
| private fun validateInput(input: String): Boolean { | ||
| if (input.isEmpty()) { | ||
| println("Ошибка: Введите число") | ||
| return false | ||
| } | ||
|
|
||
| val choice = try { | ||
| input.toInt() | ||
| } catch (e: NumberFormatException) { | ||
| println("Ошибка: Введите корректное число") | ||
| return false | ||
| } | ||
|
|
||
| if (choice < 0 || choice >= items.size) { | ||
| println("Ошибка: Пункт $choice не существует. Выберите от 0 до ${items.size - 1}") | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| private fun processChoice(choice: Int): Boolean { | ||
| if (choice == items.size - 1) { | ||
| return false // Выход | ||
| } | ||
|
Comment on lines
+74
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не стоит в этой функции проверять, является ли опция выходом - это не надёжно. Лучше из |
||
|
|
||
| items[choice].second.invoke() | ||
| return true | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package ui | ||
| import models.Archive | ||
| import models.Note | ||
|
|
||
| class NoteMenu(private val notes: MutableList<Note>) : Menu<Note>( | ||
| title = "Меню заметок", | ||
| createItemText = "Создать заметку" | ||
| ) { | ||
| override fun getItemsList(): List<Note> = notes | ||
|
|
||
| override fun getItemName(item: Note): String = item.title | ||
|
|
||
| override fun onItemSelected(item: Note) { | ||
| showNoteContent(item) | ||
| } | ||
|
|
||
| override fun onCreateItem() { | ||
| while (true) { | ||
| val title = ConsoleReader.readLine("Введите название заметки: ") | ||
|
|
||
| if (title.isBlank()) { | ||
| println("Название заметки не может быть пустым") | ||
| continue | ||
| } | ||
|
|
||
| val content = ConsoleReader.readLine("Введите текст заметки: ") | ||
|
|
||
| if (content.isBlank()) { | ||
| println("Текст заметки не может быть пустым") | ||
| continue | ||
| } | ||
|
|
||
| notes.add(Note(title, content)) | ||
| println("Заметка \"$title\" создана") | ||
| break | ||
| } | ||
| } | ||
|
|
||
| private fun showNoteContent(note: Note) { | ||
| println("\n=== ${note.title} ===") | ||
| println(note.content) | ||
| println("\nНажмите Enter для возврата...") | ||
| ConsoleReader.readLine("") | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не рекомендую писать бесконечные циклы через
while (true)- лучше всегда явно прописывать условие выхода из цикла, чтобы уменьшить вероятность ошибиться и повысить читабельность кода