Skip to content

First commit #252

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
787 changes: 787 additions & 0 deletions .idea/caches/deviceStreaming.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/main/kotlin/ArchiveMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ArchiveMenu(private val archives: MutableList<Archive>) : Menu<Archive>() {
override val items: List<Archive> = archives
override val createItemText: String = "Создать архив"
override val menuTitle: String = "Список архивов"
override val exitText: String = "Выход"

override fun displayItem(item: Archive): String = item.name

override fun onCreateItem() {
println("\nСоздание архива")
val name = readNonEmptyInput("Введите название архива: ")
archives.add(Archive(name))
println("Архив '$name' создан!")
}

override fun onItemSelected(item: Archive) {
NoteMenu(item.notes, item.name).show()
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fun main(args: Array<String>) {
println("Hello World!")
fun main() {
val app = NotesApp()
app.start()
}
65 changes: 65 additions & 0 deletions src/main/kotlin/Menu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
abstract class Menu<T> {
protected abstract val items: List<T>
protected abstract val createItemText: String
protected abstract val menuTitle: String
protected abstract val exitText: String

protected abstract fun displayItem(item: T): String
protected abstract fun onCreateItem()
protected abstract fun onItemSelected(item: T)

fun show() {
while (true) {
val menuItems = mutableListOf<String>().apply {
add(createItemText)
addAll(items.map { displayItem(it) })
}

println("\n$menuTitle:")
menuItems.forEachIndexed { index, item ->
println("$index. $item")
}
println("${menuItems.size}. $exitText")

when (val choice = getUserChoice(menuItems.size)) {
menuItems.size -> return
0 -> onCreateItem()
else -> onItemSelected(items[choice - 1])
}
}
}

private fun getUserChoice(maxItem: Int): Int {
while (true) {
print("\nВыберите пункт: ")
val input = readlnOrNull()

if (input.isNullOrBlank()) {
println("Пожалуйста, введите число")
continue
}

try {
val choice = input.toInt()
if (choice in 0..maxItem) {
return choice
} else {
println("Число должно быть от 0 до $maxItem")
}
} catch (e: NumberFormatException) {
println("Пожалуйста, введите корректное число")
}
}
}

protected fun readNonEmptyInput(prompt: String): String {
while (true) {
print(prompt)
val input = readlnOrNull()?.trim()
if (!input.isNullOrEmpty()) {
return input
}
println("Поле не может быть пустым. Пожалуйста, введите значение.")
}
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/Models.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data class Note(val title: String, val content: String)

data class Archive(val name: String, val notes: MutableList<Note> = mutableListOf())
26 changes: 26 additions & 0 deletions src/main/kotlin/NoteMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class NoteMenu(
private val notes: MutableList<Note>,
private val archiveName: String
) : Menu<Note>() {
override val items: List<Note> = notes
override val createItemText: String = "Создать заметку"
override val menuTitle: String = "Список заметок в архиве '$archiveName'"
override val exitText: String = "Назад"

override fun displayItem(item: Note): String = item.title

override fun onCreateItem() {
println("\nСоздание заметки")
val title = readNonEmptyInput("Введите название заметки: ")
val content = readNonEmptyInput("Введите текст заметки: ")
notes.add(Note(title, content))
println("Заметка '$title' создана!")
}

override fun onItemSelected(item: Note) {
println("\nЗаметка: ${item.title}")
println("Текст: ${item.content}")
println("\nНажмите Enter чтобы вернуться...")
readlnOrNull()
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/NotesApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class NotesApp {
private val archives = mutableListOf<Archive>()

fun start() {
println("Добро пожаловать в приложение Заметки!")
ArchiveMenu(archives).show()
println("\nДо свидания!")
}
}