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
560 changes: 560 additions & 0 deletions .idea/caches/deviceStreaming.xml

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.

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.

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

override fun toString() = name

fun createNote() {
val title = InputHandler.readValidInput("Введите заголовок заметки:") { it.isNotEmpty() }
val content = InputHandler.readValidInput("Введите текст заметки:") { it.isNotEmpty() }
notes.add(Note(title, content))
}
}

class Note(val title: String, val content: String) {

Choose a reason for hiding this comment

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

Класс, предназначенный только для хранения данных, лучше делать data class, т.к. у него автоматически переопределяются некоторые методы

override fun toString() = title
}
11 changes: 11 additions & 0 deletions src/main/kotlin/ArchiveMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ArchiveMenu(private val archive: Archive) : Menu<Note>("Список заметок в архиве '${archive.name}'") {
override fun getItems() = archive.notes
override fun createItem() = archive.createNote()

override fun handleItemSelected(item: Note) {
println("\nЗаметка: ${item.title}")
println("Содержание:\n${item.content}")
println("\nНажмите Enter чтобы вернуться...")
readlnOrNull()
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/InputHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object InputHandler {
fun readValidInput(prompt: String, validator: (String) -> Boolean): String {

Choose a reason for hiding this comment

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

Функцию можно написать на уровне файла, не обязательно создавать объект

while (true) {
println(prompt)
val input = readlnOrNull()?.trim() ?: ""
if (validator(input)) return input
println("Некорректный ввод, попробуйте снова")
}
}
}
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() {
NoteApp().start()
}
32 changes: 32 additions & 0 deletions src/main/kotlin/Menu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
abstract class Menu<T>(private val title: String) {
abstract fun getItems(): List<T>
abstract fun createItem()
abstract fun handleItemSelected(item: T)

fun show() {
while (true) {
println("\n$title:")
println("0. Создать")
getItems().forEachIndexed { index, item -> println("${index + 1}. ${item}") }
println("${getItems().size + 1}. Выход")

when (val choice = readChoice(getItems().size + 1)) {
0 -> createItem()
getItems().size + 1 -> return
else -> handleItemSelected(getItems()[choice - 1])
}
}
}

private fun readChoice(max: Int): Int {
while (true) {
print("Выберите пункт: ")
val input = readlnOrNull()?.toIntOrNull()
when {
input == null -> println("Введите число!")
input in 0..max -> return input
else -> println("Некорректный выбор, попробуйте снова")
}
}
}
}
34 changes: 34 additions & 0 deletions src/main/kotlin/NoteApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class NoteApp {
private val archives = mutableListOf<Archive>()

fun start() {
val mainMenu = object : Menu<Archive>("Список архивов") {
override fun getItems() = archives
override fun createItem() {
createArchive()
}

override fun handleItemSelected(item: Archive) {
val notesMenu = object : Menu<Note>("Список заметок в архиве '${item.name}'") {
override fun getItems() = item.notes
override fun createItem() {
item.createNote()
}
override fun handleItemSelected(note: Note) {
println("\nЗаметка: ${note.title}")
println("Содержание:\n${note.content}")
println("\nНажмите Enter чтобы вернуться...")
readlnOrNull()
}
}
notesMenu.show()
}
}
mainMenu.show()
}

private fun createArchive() {
val name = InputHandler.readValidInput("Введите название архива:") { it.isNotEmpty() }
archives.add(Archive(name))
}
}