Skip to content

secTry #238

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 1 commit 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
494 changes: 494 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.

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

data class Note(val name: String, val content: String)
34 changes: 34 additions & 0 deletions src/main/kotlin/ArchiveMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class ArchiveMenu(private val archives: MutableList<Archive>, private val navigator: MenuNavigator) {
fun show() {
while (true) {
val options = listOf( Pair("Создать архив", { createArchive() }), Pair("Выход", { null })) + archives.map { Pair(it.name, { showNotes(it) })
}

val choice = navigator.showMenu(options)
if (choice == null) {
println("Программа завершена")
break
}
}
}


private fun createArchive(): Archive {
println("Введите название архива:")
val name = readLine() ?: ""
if (name.isEmpty()) {
println("Архив не может быть без имени.")
return createArchive()
}

val archive = Archive(name)
archives.add(archive)
println("Архив создан. Возвращаемся к меню.")
return archive
}

private fun showNotes(archive: Archive) {
NoteMenu(archive, navigator).show()
println("Возвращаемся к архивам.")
}
}
8 changes: 7 additions & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import java.util.Scanner

fun main(args: Array<String>) {
println("Hello World!")
val scanner = Scanner(System.`in`)
val navigator = MenuNavigator(scanner)
val archives = mutableListOf<Archive>()
ArchiveMenu(archives, navigator).show()

}
25 changes: 25 additions & 0 deletions src/main/kotlin/MenuNavigator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.InputMismatchException
import java.util.Scanner

class MenuNavigator(private val scanner: Scanner) {
fun <T> showMenu(options: List<Pair<String, () -> T>>): T? {
while (true) {
options.forEachIndexed { index, option ->
println("${index}. ${option.first}")
}
try {
val choice = scanner.nextInt()
scanner.nextLine();
if (choice in options.indices) {
return options[choice].second()
} else {
println("Такого пункта нет. Пожалуйста, выберите корректный.")
}
} catch (e: InputMismatchException) {
println("Пожалуйста, введите цифру.")
scanner.nextLine()

}
}
}
}
43 changes: 43 additions & 0 deletions src/main/kotlin/NoteMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class NoteMenu(private val archive: Archive, private val navigator: MenuNavigator) {
fun show() {
while (true) {
val options = listOf(
Pair("Создать заметку", { createNote() }),
Pair("Назад", { null })
) + archive.notes.map { Pair(it.name, { showNote(it) }) }

val choice = navigator.showMenu(options)
if (choice == null) {
break
}
}
}

private fun createNote(): Note {
println("Введите имя заметки:")
val name = readLine() ?: ""
if (name.isEmpty()) {
println("Заметка не может быть без имени.")
return createNote()
}

println("Введите содержание заметки:")
val content = readLine() ?: ""
if (content.isEmpty()) {
println("Заметка не может быть пустой.")
return createNote()
}

val note = Note(name, content)
archive.notes.add(note)
println("Заметка создана. Возвращаемся к меню заметок.")
return note
}

private fun showNote(note: Note) {
println("Имя заметки: ${note.name}")
println("Содержание заметки: ${note.content}")
println("Нажмите Enter для возврата...")
readLine()
}
}