-
Notifications
You must be signed in to change notification settings - Fork 0
kotlin project Archive #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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,33 @@ | ||
| import menu.chooseNote | ||
| import menu.createArchive | ||
| import menu.createNote | ||
| import modules.Archive | ||
|
|
||
| import utils.showMenu | ||
| fun main(args: Array<String>) { | ||
| println("Hello World!") | ||
| val archives = mutableListOf<Archive>() | ||
| var isRunnig = true | ||
| while (isRunnig) { | ||
| val mainMenuOptions = listOf("Создать архив", "Выбрать архив", "Выход") | ||
| when(showMenu(mainMenuOptions, "Главное меню: ")) { | ||
| 1 -> createArchive(archives) | ||
| 2 -> { | ||
| val selectedArchive = chooseArchive(archives) | ||
| if (selectedArchive != null) { | ||
| var isRunningNoteMenu = true | ||
| while (isRunningNoteMenu) { | ||
| val noteMenuOption = listOf("Создать заметку", "Выбрать заметку", "Выход") | ||
|
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. Здесь аналогично создание списка можно вынести на строчку выше, чтобы было меньше операций с памятью |
||
| when(showMenu(noteMenuOption, "Меню заметок:")) { | ||
| 1 -> createNote(selectedArchive) | ||
| 2 -> chooseNote(selectedArchive) | ||
| 3 -> isRunningNoteMenu = false | ||
| else -> println("Неверное значение") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 3 -> isRunnig = false | ||
| else -> println("Неверное значение") } | ||
| } | ||
| println("Завершение работы!") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import modules.Archive | ||
| import utils.readIntInput | ||
|
|
||
| fun chooseArchive(archives: List<Archive>) : Archive? { | ||
| if (archives.isEmpty()) { | ||
| println("Архивов нет") | ||
| return null | ||
| } | ||
|
|
||
| println("Список архивов") | ||
| archives.forEachIndexed { index, archive -> println("${index + 1}. ${archive.nameArchive}") } | ||
| println("0. Назад") | ||
|
|
||
| val action = readIntInput("Введите номер архива", 0, archives.size) | ||
| return if (action == 0) return null else archives[action - 1] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package menu | ||
|
|
||
| import modules.Archive | ||
| import utils.readIntInput | ||
|
|
||
| fun chooseNote(archive: Archive) { | ||
| if (archive.notes.isEmpty()) { | ||
| println("Заметок нет") | ||
| return | ||
| } | ||
|
|
||
| println("Список заметок") | ||
| archive.notes.forEachIndexed { index, note -> println("${index + 1}. ${note.title}") } | ||
| println("0. Выход") | ||
|
|
||
| val action = readIntInput("Введите номер заметки", 0, archive.notes.size) | ||
| if (action != 0) { | ||
| val selectedNote = archive.notes[action - 1] | ||
| displayNote(selectedNote) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package menu | ||
| import modules.Archive | ||
| import utils.readNonBlankString | ||
|
|
||
| fun createArchive(archives: MutableList<Archive>) { | ||
|
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. Лучше функции делать чистыми(https://ru.wikipedia.org/wiki/%D0%A7%D0%B8%D1%81%D1%82%D0%BE%D1%82%D0%B0_%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%B8), чтобы их было проще переиспользовать и покрывать тестами. Чтобы данную функцию сделать чистой, можно убрать аргумент функции и сделать ей возвращаемое значение - объект класса Archive, чтобы метод добавления созданного архива в список была на вызывающей стороне |
||
| val name = readNonBlankString("Введите название архива:") | ||
| archives.add(Archive(name)) | ||
| println("Архив \"$name\" был создан") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package menu | ||
|
|
||
| import modules.Archive | ||
| import modules.Note | ||
| import utils.readNonBlankString | ||
|
|
||
| fun createNote(archive: Archive) { | ||
| val title = readNonBlankString("Введите заголовок заметки:") | ||
| val content = readNonBlankString("Введите содержимое заметки") | ||
| archive.notes.add(Note(title, content)) | ||
| println("Заметка \"$title\" была создана") | ||
| println() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package menu | ||
|
|
||
| import modules.Note | ||
|
|
||
| fun displayNote(note: Note) { | ||
| println("Заголовок заметки: ${note.title}") | ||
| println("Содержимое заметки: ${note.content}") | ||
| println() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package modules | ||
|
|
||
| data class Archive(val nameArchive: String, val notes: MutableList<Note> = mutableListOf()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package modules | ||
|
|
||
| data 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. В этом файле очень хорошие утилитные функции, молодец. Файл правда суперский, но нейминг ему лучше подбирать более говорящий, например |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package utils | ||
|
|
||
| fun readIntInput (name: String, min: Int, max: Int) : Int { | ||
| while (true) { | ||
| println(name) | ||
| val input = readln().toIntOrNull() | ||
| if (input != null && input in min..max) { | ||
| return input | ||
| } else { | ||
| println("Введите корректное число!") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun readNonBlankString(name: String) : String { | ||
| while (true) { | ||
| println(name) | ||
| val input = readln().trim() | ||
| if (input.isBlank()) { | ||
| println("Поле не может быть пустым") | ||
| } else return input | ||
| } | ||
| } | ||
| fun showMenu(options: List<String>, title: String) : Int { | ||
| println(title) | ||
| options.forEachIndexed { index, option -> println("${index + 1}. $option") } | ||
| return readIntInput("Выберите пункт меню: ", 0, options.size) | ||
| } |
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, т.к. сейчас на каждую итерацию цикла будет создан новый список, а если вынести на строчку выше, то список будет создан один на всю программу, таким образом будет меньше операций с памятью