Skip to content

Исправленная версия #230

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 3 commits into
base: main
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
395 changes: 395 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.

1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Пустой репозиторий для работы с Kotlin кодом в Android Studio
Приложение заметки на КОтлин
26 changes: 26 additions & 0 deletions src/main/kotlin/ArchiveMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class ArchiveMenu : BaseMenu<String>(
mutableListOf(), MenuUtils(), "архивов", "Создать архив"
) {

private val noteMenu = NoteMenu()

override fun menuItemSelected(item: String) {
println("Выбран архив: $item")
noteMenu.show()
}

override fun createItem() {
println("Напишите имя архива:")
val name = readln().trim()
if (name.isNotEmpty()) {
items.add(name)
println("Поздравляю, архив \"$name\" успешно создан.")
} else {
println("Имя архива не может быть пустым, введите название.")
}
}

fun show() {
showMenu()
}
}
41 changes: 41 additions & 0 deletions src/main/kotlin/BaseMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
abstract class BaseMenu<T>(
protected val items: MutableList<T>,
protected val menuUtils: MenuUtils,
private val itemType: String,
private val createOption: String
) {

protected abstract fun menuItemSelected(item: T)

protected abstract fun createItem()

fun showMenu() {
while (true) {
println("\nСписок $itemType:")
println("0. $createOption")
showItems()
println("${items.size + 1}. Выход")

val choice = menuUtils.readInput()
if (choice == items.size + 1) return

when (choice) {
0 -> createItem()
in 1..items.size -> menuItemSelected(items[choice - 1])
else -> println("Некорректный выбор. Пожалуйста, введите число от 0 до ${items.size + 1}")
}
}
}

private fun showItems() {
if (items.isNotEmpty()) {
items.forEachIndexed { index, item ->
when (item) {
is Pair<*, *> -> println("${index + 1}. ${item.first}")
is String -> println("${index + 1}. $item")
else -> println("${index + 1}. Неизвестный элемент")
}
}
}
}
}
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 archiveMenu = ArchiveMenu()
archiveMenu.show()
}
16 changes: 16 additions & 0 deletions src/main/kotlin/MenuUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Scanner

class MenuUtils {
private val scanner = Scanner(System.`in`)

fun readInput(): Int {
while (true) {
val input = scanner.nextLine()
try {
return input.toInt()
} catch (e: NumberFormatException) {
println("Пожалуйста, введите число.")
}
}
}
}
69 changes: 69 additions & 0 deletions src/main/kotlin/NoteMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class NoteMenu : BaseMenu<Pair<String, String>>(
mutableListOf(), MenuUtils(), "заметок", "Создать заметку"
) {

override fun menuItemSelected(item: Pair<String, String>) {
println("Заметка: ${item.first}")
println("Текст заметки:\n${item.second}")

println("Выберите действие:")
println("0. Добавить текст")
println("1. Выход")

val choice = menuUtils.readInput()

when (choice) {
0 -> {
appendContent(item)
}

1 -> {
return
}

else -> {
println("Некорректный выбор. Пожалуйста выберите 0 или 1.")
}
}
}

override fun createItem() {
println("Введите название заметки:")
val title = readln().trim()
if (title.isEmpty()) {
println("Название заметки не может быть пустым.")
return
}

println("Введите текст заметки:")
val content = readln().trim()
if (content.isEmpty()) {
println("Текст заметки не может быть пустым.")
return
}

items.add(Pair(title, content))
println("Заметка \"$title\" успешно создана.")
}

private fun appendContent(item: Pair<String, String>) {
println("Введите текст, который Вы хотите добавить:")
val additionalText = readln().trim()
if (additionalText.isNotEmpty()) {
val updateContent = item.second + "\n" + additionalText
val index = items.indexOf(item)
if (index != -1) {
items[index] = Pair(item.first, updateContent)
println("Текст успешно добавлен к заметке \"${item.first}\".")
} else {
println("Ошибка: Не удалось найти заметку для обновления.")
}
} else {
println("Добавляемый текст не может быть пустым.")
}
}

fun show() {
showMenu()
}
}