Skip to content
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

Практическая работа 3 #204

Open
wants to merge 2 commits 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
1 change: 0 additions & 1 deletion .idea/misc.xml

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

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 notesApp = NotesApp()
notesApp.start()
}
72 changes: 72 additions & 0 deletions src/main/kotlin/NotesApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
data class Archive(val id: Int, val name: String)
data class Note(val id: Int, val name: String, val content: String)

class NotesApp {
val archives = mutableListOf<Archive>()
val notes = mutableMapOf<Int, MutableList<Note>>()
private var archiveCounter = 0
private var noteCounter = 0

fun start() {
val ui = UserInterface(this)
ui.start()
}

fun createArchive() {
while (true) {
println("Введите название архива:")
val name = readLine()?.trim()
if (name.isNullOrEmpty()) {
println("Название архива не может быть пустым. Пожалуйста, попробуйте снова.")
} else {
archives.add(Archive(++archiveCounter, name))
notes[archiveCounter] = mutableListOf()
println("Архив '$name' успешно создан.")
break
}
}
}

fun displayArchives() {
if (archives.isEmpty()) {
println("Нет доступных архивов.")
} else {
archives.forEach { archive ->
println("${archive.id}. ${archive.name}")
}
}
}

fun createNote(archiveId: Int) {
while (true) {
println("Введите название заметки:")
val name = readLine()?.trim()
if (name.isNullOrEmpty()) {
println("Название заметки не может быть пустым. Пожалуйста, попробуйте снова.")
} else {
println("Введите содержимое заметки:")
val content = readLine()?.trim()
if (content.isNullOrEmpty()) {
println("Содержимое заметки не может быть пустым. Пожалуйста, попробуйте снова.")
} else {
notes[archiveId]?.add(Note(++noteCounter, name, content))
println("Заметка '$name' успешно добавлена.")
break
}
}
}
}

fun displayNotes(archiveId: Int) {
notes[archiveId]?.forEach { note ->
println("${note.id}. ${note.name}")
} ?: println("Нет заметок в этом архиве.")
}

fun viewNote(archiveId: Int, noteIndex: Int) {
val selectedNote = notes[archiveId]?.get(noteIndex - 1)
selectedNote?.let {
println("Содержимое заметки '${it.name}': ${it.content}")
} ?: println("Заметка не найдена.")
}
}
80 changes: 80 additions & 0 deletions src/main/kotlin/UserInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class UserInterface(private val notesApp: NotesApp) {

fun start() {
while (true) {
println("Список архивов:")
notesApp.displayArchives()
println("Введите номер архива для выбора, 0 для создания нового архива или -1 для выхода:")
if (!handleArchiveInput()) break
}
}

fun selectArchive(archiveIndex: Int) {
val selectedArchive = notesApp.archives[archiveIndex - 1]
println("Выбрали архив: ${selectedArchive.name}.")

while (true) {
println("Заметки в архиве '${selectedArchive.name}':")
notesApp.displayNotes(selectedArchive.id)
println("Введите номер заметки для выбора, 0 для создания новой заметки или -1 для выхода в меню архивов:")
if (!handleNoteInput(selectedArchive.id)) {
return
}
}
}

private fun handleArchiveInput(): Boolean {
val input = readLine() ?: return true
val option = input.toIntOrNull()

return when {
option == null -> {
println("Пожалуйста, вводите число.")
true
}
option == -1 -> {
println("Выход из программы.")
false
}
option == 0 -> {
notesApp.createArchive()
true
}
option in 1..notesApp.archives.size -> {
selectArchive(option)
true
}
else -> {
println("Такого элемента нет. Пожалуйста, попробуйте снова.")
true
}
}
}

private fun handleNoteInput(archiveId: Int): Boolean {
val input = readLine() ?: return true
val option = input.toIntOrNull()

return when {
option == null -> {
println("Пожалуйста, вводите число.")
true
}
option == -1 -> {
return false
}
option == 0 -> {
notesApp.createNote(archiveId)
true
}
notesApp.notes[archiveId]?.isNotEmpty() == true && option in 1..notesApp.notes[archiveId]!!.size -> {
notesApp.viewNote(archiveId, option)
true
}
else -> {
println("Такого элемента нет. Пожалуйста, попробуйте снова.")
true
}
}
}
}