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

commit3 #211

Open
wants to merge 4 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
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.

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

init {
require(name.isNotBlank()) { "Название архива не может быть пустым." }
}

fun addNote(note: Note) {
notes.add(note)
}
}
class MainMenu(private val noteManager: NoteManager) {
fun show() {
while (true) {
println("Главное меню:")
println("1. Создать архив")
println("2. Просмотреть архивы")
println("0. Выход")

when (readLine()) {
"1" -> createArchive()
"2" -> viewArchives()
"0" -> return
else -> println("Некорректный выбор. Пожалуйста, попробуйте снова.")
}
}
}

private fun createArchive() {
while (true) {
print("Введите название архива: ")
val name = readLine() ?: ""
try {
noteManager.addArchive(Archive(name))
println("Архив создан! Нажмите любую клавишу для продолжения...")
readLine()
return
} catch (e: IllegalArgumentException) {
println(e.message)
}
}
}

private fun viewArchives() {
if (noteManager.archives.isEmpty()) {
println("Нет доступных архивов.")
return
}

println("Доступные архивы:")
noteManager.archives.forEachIndexed { index, archive ->
println("${index + 1}. ${archive.name}")
}

print("Введите номер архива для просмотра заметок (или 0 для выхода): ")
val input = readLine()?.toIntOrNull()
if (input == 0) return
if (input != null && input in 1..noteManager.archives.size) {
viewNotesInArchive(input - 1)
} else {
println("Некорректный номер архива.")
}
}

private fun viewNotesInArchive(archiveIndex: Int) {
while (true) {
noteManager.viewNotesInArchive(archiveIndex)
println("Хотите добавить новую заметку в архив? (да/нет)")
when (readLine()?.toLowerCase()) {
"да" -> addNoteToArchive(archiveIndex)
"нет" -> return
else -> println("Некорректный ввод. Пожалуйста, введите 'да' или 'нет'.")
}
}
}

private fun addNoteToArchive(archiveIndex: Int) {
while (true) {
print("Введите заголовок заметки: ")
val title = readLine() ?: ""
print("Введите содержимое заметки: ")
val content = readLine() ?: ""

try {
val note = Note(title, content)
noteManager.archives[archiveIndex].addNote(note)
println("Заметка добавлена! Нажмите любую клавишу для продолжения...")
readLine()
return
} catch (e: IllegalArgumentException) {
println(e.message)
}
}
}
}
6 changes: 4 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fun main(args: Array<String>) {
println("Hello World!")
fun main() {
val noteManager = NoteManager()
val mainMenu = MainMenu(noteManager)
mainMenu.show()
}
39 changes: 39 additions & 0 deletions src/main/kotlin/NoteClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Note(val title: String, val content: String) {
init {
require(title.isNotBlank()) { "Название заметки не может быть пустым." }
}
init {
require(content.isNotBlank()) { "Содержимое заметки не может быть пустым." }
}
}
class NoteManager {
val archives = mutableListOf<Archive>()

fun addArchive(archive: Archive) {
archives.add(archive)
}

fun viewNotesInArchive(archiveIndex: Int) {
val archive = archives[archiveIndex]
if (archive.notes.isEmpty()) {
println("В архиве '${archive.name}' нет заметок.")
} else {
println("Заметки в архиве '${archive.name}':")
archive.notes.forEachIndexed { index, note ->
println("${index + 1}. ${note.title}")
}
print("Введите номер заметки для просмотра (или 0 для выхода): ")
val input = readLine()
val noteIndex = input?.toIntOrNull()
if (noteIndex != null && noteIndex in 1..archive.notes.size) {
val selectedNote = archive.notes[noteIndex - 1]
println("Заголовок: ${selectedNote.title}")
println("Содержимое: ${selectedNote.content}")
} else if (noteIndex == 0) {
return
} else {
println("Некорректный номер заметки.")
}
}
}
}