-
Notifications
You must be signed in to change notification settings - Fork 0
Проектная #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?
Проектная #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Archive(val name: String) { | ||
| val notes = mutableListOf<Note>() | ||
|
|
||
| override fun toString() = name | ||
|
|
||
| fun createNote() { | ||
| val title = InputHandler.readValidInput("Введите заголовок заметки:") { it.isNotEmpty() } | ||
| val content = InputHandler.readValidInput("Введите текст заметки:") { it.isNotEmpty() } | ||
| notes.add(Note(title, content)) | ||
| } | ||
| } | ||
|
|
||
| class Note(val title: String, val content: String) { | ||
|
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. Класс, предназначенный только для хранения данных, лучше делать |
||
| override fun toString() = title | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class ArchiveMenu(private val archive: Archive) : Menu<Note>("Список заметок в архиве '${archive.name}'") { | ||
| override fun getItems() = archive.notes | ||
| override fun createItem() = archive.createNote() | ||
|
|
||
| override fun handleItemSelected(item: Note) { | ||
| println("\nЗаметка: ${item.title}") | ||
| println("Содержание:\n${item.content}") | ||
| println("\nНажмите Enter чтобы вернуться...") | ||
| readlnOrNull() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| object InputHandler { | ||
| fun readValidInput(prompt: String, validator: (String) -> Boolean): String { | ||
|
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. Функцию можно написать на уровне файла, не обязательно создавать объект |
||
| while (true) { | ||
| println(prompt) | ||
| val input = readlnOrNull()?.trim() ?: "" | ||
| if (validator(input)) return input | ||
| println("Некорректный ввод, попробуйте снова") | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| fun main(args: Array<String>) { | ||
| println("Hello World!") | ||
| fun main() { | ||
| NoteApp().start() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| abstract class Menu<T>(private val title: String) { | ||
| abstract fun getItems(): List<T> | ||
| abstract fun createItem() | ||
| abstract fun handleItemSelected(item: T) | ||
|
|
||
| fun show() { | ||
| while (true) { | ||
| println("\n$title:") | ||
| println("0. Создать") | ||
| getItems().forEachIndexed { index, item -> println("${index + 1}. ${item}") } | ||
| println("${getItems().size + 1}. Выход") | ||
|
|
||
| when (val choice = readChoice(getItems().size + 1)) { | ||
| 0 -> createItem() | ||
| getItems().size + 1 -> return | ||
| else -> handleItemSelected(getItems()[choice - 1]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun readChoice(max: Int): Int { | ||
| while (true) { | ||
| print("Выберите пункт: ") | ||
| val input = readlnOrNull()?.toIntOrNull() | ||
| when { | ||
| input == null -> println("Введите число!") | ||
| input in 0..max -> return input | ||
| else -> println("Некорректный выбор, попробуйте снова") | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| class NoteApp { | ||
| private val archives = mutableListOf<Archive>() | ||
|
|
||
| fun start() { | ||
| val mainMenu = object : Menu<Archive>("Список архивов") { | ||
| override fun getItems() = archives | ||
| override fun createItem() { | ||
| createArchive() | ||
| } | ||
|
|
||
| override fun handleItemSelected(item: Archive) { | ||
| val notesMenu = object : Menu<Note>("Список заметок в архиве '${item.name}'") { | ||
| override fun getItems() = item.notes | ||
| override fun createItem() { | ||
| item.createNote() | ||
| } | ||
| override fun handleItemSelected(note: Note) { | ||
| println("\nЗаметка: ${note.title}") | ||
| println("Содержание:\n${note.content}") | ||
| println("\nНажмите Enter чтобы вернуться...") | ||
| readlnOrNull() | ||
| } | ||
| } | ||
| notesMenu.show() | ||
| } | ||
| } | ||
| mainMenu.show() | ||
| } | ||
|
|
||
| private fun createArchive() { | ||
| val name = InputHandler.readValidInput("Введите название архива:") { it.isNotEmpty() } | ||
| archives.add(Archive(name)) | ||
| } | ||
| } |
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.
Всю папку .idea лучше сразу добавлять в gitignore - в ней хранятся локальные настройки разработчика, специфичные для проекта