Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/main/kotlin/ArchiveMenu.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
class ArchiveMenu : Menu<Archive>("Список архивов", mutableListOf()) {
class ArchiveMenu : Menu<Archive>(Constants.CREATE_ARCHIVE, mutableListOf()) {
private val archives = mutableListOf<Archive>()

init {
items.add(MenuItem("Создать архив") { createArchive() })
items.add(MenuItem(Constants.CREATE_ARCHIVE) { createArchive() })
}

override fun show() {
updateArchiveItems()
updateMenuItems()
super.show()
}

private fun updateArchiveItems() {
items.removeAll { it.title != "Создать архив" }
private fun updateMenuItems() {
items.removeAll { it.title != Constants.CREATE_ARCHIVE }
archives.forEach { archive ->
items.add(MenuItem(archive.name) { archive })
}
}

override fun onItemSelected(item: Archive) {
NoteMenu(item).show()
updateArchiveItems()
val noteMenu = NoteMenu(item)
noteMenu.show()

val updatedArchive = noteMenu.getCurrentArchive()
val index = archives.indexOfFirst { it.name == item.name }
if (index != -1) {
archives[index] = updatedArchive
}

updateMenuItems()
}

private fun createArchive(): Archive {
println("\nВведите название архива:")
val name = readNonEmptyString("Название не может быть пустым!")
val name = readNonEmptyString(Constants.EMPTY_NAME_ERROR)
return Archive(name).also {
archives.add(it)
println("Архив '$name' создан!")
updateMenuItems()
}
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Constants {
const val CREATE_ARCHIVE = "Создать архив"
const val CREATE_NOTE = "Создать заметку"
const val EXIT = "Выход"


const val EMPTY_NAME_ERROR = "Название не может быть пустым!"
const val EMPTY_CONTENT_ERROR = "Текст не может быть пустым!"
const val INVALID_NUMBER_ERROR = "Ошибка: введите число от %d до %d"
const val NOT_A_NUMBER_ERROR = "Ошибка: введите корректное число"
}
2 changes: 1 addition & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fun main() {
ArchiveMenu().show()
}
}
20 changes: 11 additions & 9 deletions src/main/kotlin/NoteMenu.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
class NoteMenu(archive: Archive) : Menu<Note>("Архив: ${archive.name}", mutableListOf()) {
private var currentArchive = archive
class NoteMenu(initialArchive: Archive) : Menu<Note>("Архив: ${initialArchive.name}", mutableListOf()) {
private var currentArchive = initialArchive

fun getCurrentArchive(): Archive = currentArchive

init {
items.add(MenuItem("Создать заметку") { createNote() })
items.add(MenuItem(Constants.CREATE_NOTE) { createNote() })
}

override fun show() {
updateNoteItems()
updateMenuItems()
super.show()
}

private fun updateNoteItems() {
items.removeAll { it.title != "Создать заметку" }
private fun updateMenuItems() {
items.removeAll { it.title != Constants.CREATE_NOTE }
currentArchive.notes.forEach { note ->
items.add(MenuItem(note.title) { note })
}
Expand All @@ -23,15 +25,15 @@ class NoteMenu(archive: Archive) : Menu<Note>("Архив: ${archive.name}", mut

private fun createNote(): Note {
println("\nВведите название заметки:")
val title = readNonEmptyString("Название не может быть пустым!")
val title = readNonEmptyString(Constants.EMPTY_NAME_ERROR)

println("Введите текст заметки:")
val content = readNonEmptyString("Текст не может быть пустым!")
val content = readNonEmptyString(Constants.EMPTY_CONTENT_ERROR)

val newNote = Note(title, content)
currentArchive = currentArchive.copy(notes = currentArchive.notes + newNote)
println("Заметка '$title' создана!")
updateNoteItems()
updateMenuItems()
return newNote
}

Expand Down
19 changes: 11 additions & 8 deletions src/main/kotlin/menu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ import java.util.Scanner

abstract class Menu<T>(private val title: String, protected val items: MutableList<MenuItem<T>>) {
protected val scanner = Scanner(System.`in`)
private val exitItem = MenuItem("Выход") { null }
private val exitItem = MenuItem(Constants.EXIT) { null }

open fun show() {
while (true) {
println("\n$title:")
items.forEachIndexed { index, item -> println("$index. ${item.title}") }
println("${items.size}. ${exitItem.title}")

printMenu()
when (val selected = readValidOption()) {
items.size -> return
else -> items[selected].action()?.let { result -> onItemSelected(result) }
else -> items[selected].action()?.let { onItemSelected(it) }
}
}
}

protected abstract fun onItemSelected(item: T)

private fun printMenu() {
println("\n$title:")
items.forEachIndexed { index, item -> println("$index. ${item.title}") }
println("${items.size}. ${exitItem.title}")
}

private fun readValidOption(): Int {
while (true) {
print("Выберите пункт меню: ")
try {
val input = scanner.nextLine().toInt()
if (input in 0..items.size) return input
println("Ошибка: введите число от 0 до ${items.size}")
println(Constants.INVALID_NUMBER_ERROR.format(0, items.size))
} catch (e: NumberFormatException) {
println("Ошибка: введите корректное число")
println(Constants.NOT_A_NUMBER_ERROR)
}
}
}
Expand Down