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
41 changes: 41 additions & 0 deletions src/main/kotlin/Archive.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package app

class Archive(val name: String) {
val notes = mutableListOf<Note>()

fun addNote() {
println("Введите название заметки:")
val name = readNonEmptyLine("Название не может быть пустым.")

println("Введите содержание заметки:")
val content = readNonEmptyLine("Содержание не может быть пустым.")

notes.add(Note(name, content))
println("Заметка добавлена.")
}

fun showNotes() {
if (notes.isEmpty()) {
println("Нет заметок в архиве.")
return
}

val noteMenu = Menu("Заметки в архиве '$name'")
notes.forEachIndexed { index, note ->
noteMenu.addItem(note.name) {
println("\n--- ${note.name} ---")
println(note.content)
}
}
noteMenu.addItem("Назад") {}
noteMenu.show()
}

private fun readNonEmptyLine(errorMsg: String): String {
while (true) {
val line = readln()
if (line.isNotBlank()) return line
println(errorMsg)
}
}
}
50 changes: 48 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
fun main(args: Array<String>) {
println("Hello World!")
//fun main(args: Array<String>) {
// println("Hello World!")
//}

package app

fun main() {
val archives = mutableListOf<Archive>()

fun showArchives() {
var running = true
while (running) {
val archiveMenu = Menu("Список архивов:")
archiveMenu.addItem("Создать архив") {
println("Введите название архива:")
val name = readNonEmptyLine("Название архива не может быть пустым.")
archives.add(Archive(name))
println("Архив добавлен.")
}

archives.forEach { archive ->
archiveMenu.addItem(archive.name) {
val subMenu = Menu("Архив: ${archive.name}")
subMenu.addItem("Добавить заметку") { archive.addNote() }
subMenu.addItem("Просмотреть заметки") { archive.showNotes() }
subMenu.addItem("Назад") {}
subMenu.show()
}
}

archiveMenu.addItem("Выход из программы") {
println("До свидания!")
running = false // ✅ Меняем флаг, чтобы выйти из while
}

archiveMenu.show()
}
}

showArchives()
}

fun readNonEmptyLine(errorMsg: String): String {
while (true) {
val line = readln()
if (line.isNotBlank()) return line
println(errorMsg)
}
}
34 changes: 34 additions & 0 deletions src/main/kotlin/Menu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package app

import java.util.Scanner

class Menu(private val title: String) {
private val items = mutableListOf<MenuItem>()
private val scanner = Scanner(System.`in`)

fun addItem(title: String, action: () -> Unit) {
items.add(MenuItem(title, action))
}

fun show() {
while (true) {
println("\n$title")
items.forEachIndexed { index, item -> println("$index. ${item.title}") }
print("Выберите пункт меню: ")
val input = scanner.nextLine()

val choice = input.toIntOrNull()
if (choice == null) {
println("Ошибка: нужно ввести цифру.")
continue
}
if (choice !in items.indices) {
println("Ошибка: пункта с таким номером нет.")
continue
}

items[choice].action()
break
}
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/MenuItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package app

data class MenuItem(val title: String, val action: () -> Unit)
3 changes: 3 additions & 0 deletions src/main/kotlin/Note.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package app

data class Note(val name: String, val content: String)