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
1,610 changes: 1,610 additions & 0 deletions .idea/caches/deviceStreaming.xml
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Всю папку .idea лучше сразу добавлять в gitignore - в ней хранятся локальные настройки разработчика, специфичные для проекта. После добавления важно не забыть удалить папку с репозитория

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions .idea/markdown.xml

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

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.

82 changes: 82 additions & 0 deletions src/main/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class App {
private val archives = mutableListOf<Archive>()
private var isRunning = true

fun start() {
while (isRunning) {
showArchiveMenu()

}
println("Программа завершена")
}

private fun showArchiveMenu() {
val menu = MenuScreen(
title = "Архивы",
items = archives.toList(),
onItemSelected = { archive -> showNotesMenu(archive) },
onCreateNew = { createArchive() },
onExit = { exit() }

)
menu.show()
}

private fun createArchive() {
val result = CreateScreen("архив").show()
when (result) {
is CreateResult.ArchiveCreated -> {
archives.add(Archive(result.name))
println("Архив \"${result.name}\" создан!")
}

is CreateResult.Cancelled -> {
println("Создание архива отменено")
}

else -> {}
}
}

private fun showNotesMenu(archive: Archive) {
var isInNotesMenu = true
while (isInNotesMenu) {
val menu = MenuScreen(
title = "Заметки в архиве \"${archive.name}\"",
items = archive.notes.toList(),
onItemSelected = { note -> showNote(note) },
onCreateNew = { createNote(archive) },
onExit = { isInNotesMenu = false },

)
menu.show()
}
}


private fun createNote(archive: Archive) {
val result = CreateScreen("заметки").show()

when (result) {
is CreateResult.NoteCreated -> {
archive.notes.add(Note(result.name, result.text))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше в классе Archive написать отдельный метод для создания заметки, чтобы изменяемый список notes не был доступен всем

println("Заметка \"${result.name}\"создана!")
}

is CreateResult.Cancelled -> {
println("Создание заметки отменено")
}

else -> {}
}
}

private fun showNote(note: Note) {
ContentView(note).show()
}

private fun exit() {
isRunning = false

}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/Archive.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data class Archive(
val name: String,
val notes: MutableList<Note> = mutableListOf()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В data классах не рекомендуется хранить мьютабельные данные (в данном случае MutableList), т.к. в многопоточных средах могут возникнуть проблемы с такими объектами, а именно с доступом к чтению и записи изменяемых полей. У data классов автоматически генерируется функция copy, которую можно использовать для изменения полей объекта с помощью его копирования


) {
override fun toString(): String = name
}
14 changes: 14 additions & 0 deletions src/main/kotlin/ContentView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.Scanner

class ContentView(
private val note: Note
) {
fun show() {
println("\n===${note.nameNote}===")
println("-".repeat(40))
println(note.textNote)
println("_".repeat(40))
println("Нажмите Enter, что бы вернуться...")
Scanner(System.`in`).nextLine()
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/CreateScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Scanner


class CreateScreen(
private val itemType: String
) {
companion object {
private val scanner = Scanner(System.`in`)
}

fun show(): CreateResult {
println("\nСоздание $itemType")
print("Введите название $itemType: ")
val name = scanner.nextLine().trim()
if (name.isEmpty()) {
println("Ошибка: название не может быть пустым")
return CreateResult.Cancelled
}
if (itemType == "заметки") {
print("Введите текст заметки: ")
val text = scanner.nextLine().trim()
if (text.isEmpty()) {
println("Ошибка: текст заметки не может быть пустым")
return CreateResult.Cancelled
}
return CreateResult.NoteCreated(name, text)
}
return CreateResult.ArchiveCreated(name)
}

}

sealed class CreateResult {
object Cancelled : CreateResult()
data class ArchiveCreated(val name: String) : CreateResult()
data class NoteCreated(val name: String, val text: String) : CreateResult()

}
4 changes: 2 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fun main(args: Array<String>) {
println("Hello World!")
fun main() {
App().start()
}
63 changes: 63 additions & 0 deletions src/main/kotlin/MenuScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.Scanner


import kotlin.collections.toList

class MenuScreen<T>(
private val title: String,
private val items: List<T>,
private val onItemSelected: (T) -> Unit,
private val onCreateNew: () -> Unit,
private val onExit: () -> Unit
) {
companion object {
private val scanner = Scanner(System.`in`)
}

fun show() {
val itemsSnapshot = items.toList()

while (true) {
println("\n=== $title ===")
if (itemsSnapshot.isEmpty()) {
println("Список пуст. Чтобы создать элемент, выберите пункт 'Создать'")
} else {
itemsSnapshot.forEachIndexed { index, item ->
println("${index + 1}. $item")
}
}
println("${itemsSnapshot.size + 1}. Создать")
println("0. Выход")
print("Выберите пункт (0-${itemsSnapshot.size + 1}): ")
val input = scanner.nextLine().trim()
val choice = input.toIntOrNull()
if (choice == null) {
println("Ошибка: введите цифру.")
continue
}
when (choice) {
0 -> {
onExit()
return

}

in 1..itemsSnapshot.size -> {
onItemSelected(itemsSnapshot[choice - 1])
return
}

itemsSnapshot.size + 1 -> {
onCreateNew()
return
}

else -> {
println("Ошибка: нет пункта с номером $choice.")
println("Доступные номера: 0-${itemsSnapshot.size + 1}")
}
}

}
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/Note.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data class Note(
val nameNote: String,
val textNote: String
) {
override fun toString(): String = nameNote

}