-
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?
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,25 @@ | ||
| class ArchiveScreen { | ||
| private val archives = mutableListOf<Archive>() | ||
| private val input = InputUtils() | ||
|
|
||
| fun show() { | ||
| Menu( | ||
| title = "СПИСОК АРХИВОВ", | ||
| items = archives, | ||
| itemToString = { archive -> archive.name }, | ||
| onCreate = { createArchive() }, | ||
| onSelect = { archive -> showNotes(archive) }, | ||
| exitText = "Выход" | ||
| ).show() | ||
| } | ||
|
|
||
| private fun createArchive() { | ||
| val name = input.readString("Введите название архива:") | ||
| archives.add(Archive(name, mutableListOf())) | ||
| println("Архив '$name' создан!") | ||
| } | ||
|
|
||
| private fun showNotes(archive: Archive) { | ||
| NoteScreen(archive).show() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.util.Scanner | ||
|
|
||
| class InputUtils { | ||
| private val scanner = Scanner(System.`in`) | ||
|
|
||
| fun readInt(): Int? { | ||
| val line = scanner.nextLine() | ||
| return line.toIntOrNull() | ||
| } | ||
|
|
||
| fun readString(prompt: String): String { | ||
| while (true) { | ||
|
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. Не рекомендую писать бесконечные циклы через |
||
| println(prompt) | ||
| val input = scanner.nextLine().trim() | ||
| if (input.isNotEmpty()) { | ||
| return input | ||
| } | ||
| println("Ошибка: поле не может быть пустым. Попробуйте снова.") | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| fun main(args: Array<String>) { | ||
| println("Hello World!") | ||
| fun main() { | ||
| println("Добро пожаловать в приложение 'Заметки'!") | ||
| ArchiveScreen().show() | ||
| println("Работа завершена.") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| class Menu<T>( | ||
| private val title: String, | ||
| private val items: MutableList<T>, | ||
|
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. Лучше сюда просто |
||
| private val itemToString: (T) -> String, | ||
| private val onCreate: () -> Unit, | ||
| private val onSelect: (T) -> Unit, | ||
| private val exitText: String = "Выход" | ||
| ) { | ||
| private val input = InputUtils() | ||
|
|
||
| fun show() { | ||
| while (true) { | ||
| println("\n--- $title ---") | ||
| println("0. Создать...") | ||
|
|
||
| items.forEachIndexed { index, item -> | ||
| println("${index + 1}. ${itemToString(item)}") | ||
| } | ||
|
|
||
| val exitIndex = items.size + 1 | ||
| println("$exitIndex. $exitText") | ||
|
|
||
| println("\nВведите номер пункта:") | ||
|
|
||
| val choice = input.readInt() | ||
|
|
||
| if (choice == null) { | ||
| println("Ошибка: введите число.") | ||
| continue | ||
| } | ||
|
|
||
| when (choice) { | ||
| 0 -> onCreate() | ||
| exitIndex -> return | ||
| in 1..items.size -> onSelect(items[choice - 1]) | ||
| else -> println("Такого пункта нет. Введите корректное число.") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| data class Note( | ||
| val title: String, | ||
| val content: String | ||
| ) | ||
|
|
||
| data class Archive( | ||
| val name: String, | ||
| val notes: MutableList<Note> = mutableListOf() | ||
|
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. В data классах не рекомендуется хранить мьютабельные данные (в данном случае MutableList), т.к. в многопоточных средах могут возникнуть проблемы с такими объектами, а именно с доступом к чтению и записи изменяемых полей. У data классов автоматически генерируется функция |
||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import java.util.Scanner | ||
|
|
||
| class NoteScreen(private val archive: Archive) { | ||
| private val input = InputUtils() | ||
|
|
||
| fun show() { | ||
| Menu( | ||
| title = "Архив '${archive.name}': ЗАМЕТКИ", | ||
| items = archive.notes, | ||
| itemToString = { note -> note.title }, | ||
| onCreate = { createNote() }, | ||
| onSelect = { note -> viewNote(note) }, | ||
| exitText = "Назад" | ||
| ).show() | ||
| } | ||
|
|
||
| private fun createNote() { | ||
| val title = input.readString("Введите название заметки:") | ||
| val content = input.readString("Введите текст заметки:") | ||
| archive.notes.add(Note(title, content)) | ||
| println("Заметка '$title' создана!") | ||
| } | ||
|
|
||
| private fun viewNote(note: Note) { | ||
| println("\n--- Просмотр заметки ---") | ||
| println("Заголовок: ${note.title}") | ||
| println("Текст: ${note.content}") | ||
| println("\nНажмите Enter, чтобы вернуться назад...") | ||
| Scanner(System.`in`).nextLine() | ||
| } | ||
| } |
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 - в ней хранятся локальные настройки разработчика, специфичные для проекта. После добавления важно не забыть удалить папку с репозитория