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

first try #203

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
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 @@
class Archive (val name:String) {
val notes: MutableList<Note> = ArrayList()
}
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 archive: Archive, private val ioHandler: IOHandler){ //меню архивов

fun show() {
while (true) {
val noteNames = archive.notes.map { it.name }

ioHandler.showList(
title = "Архив: ${archive.name}",
items = noteNames,
footer = "${archive.notes.size + 1}. Назад",
createOp = "0. Создать заметку"
)

when (val choice = ioHandler.getUserChoice()) {
0 -> createNote(archive)
in 1..archive.notes.size -> NoteMenu(archive.notes[choice!! - 1],ioHandler).show()
archive.notes.size + 1 -> break
else -> ioHandler.showMessage("Неверный выбор, попробуйте снова.\n")
}
}
}
private fun createNote(archive: Archive) {
ioHandler.showMessage("Введите название заметки:")
val name = readlnOrNull().orEmpty()
ioHandler.showMessage("Введите текст заметки:")
val body = readlnOrNull().orEmpty()
if (name.isNotEmpty() && body.isNotEmpty()) {
archive.notes.add(Note(name, body))
ioHandler.showMessage("Заметка '$name' создана.\n")
} else{
ioHandler.showMessage("Название и текст заметки не может быть пустым!\n")
}
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/IOHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class IOHandler {

fun showList(title: String, items: List<String>, footer: String, createOp: String) {
println(title)
println(createOp)
if (items.isNotEmpty()) {
for (i in items.indices) {
println("${i + 1}. ${items[i]}")
}
} else {
println("Элементов нет.")
}
println(footer)
}

fun getUserChoice(): Int? {
return readlnOrNull()?.toIntOrNull()
}

fun showMessage(message: String) {
println(message)
}
}
8 changes: 6 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
fun main(args: Array<String>) {
println("Hello World!")
fun main() {
val archives = mutableListOf<Archive>()
val ioHandler = IOHandler()
val mainMenu = MainMenu(archives, ioHandler)
ioHandler.showMessage("Добро пожаловать в программу!\n")
mainMenu.show()
}
36 changes: 36 additions & 0 deletions src/main/kotlin/MainMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class MainMenu(private val archives: MutableList<Archive>, private val ioHandler: IOHandler) {

fun show() {
while (true) {
val archiveNames = archives.map { it.name }

ioHandler.showList(
title = "Список архивов:",
items = archiveNames,
footer = "${archives.size + 1}. Выход",
createOp = "0. Создать архив"
)

when (val choice = ioHandler.getUserChoice()){
0 -> createArchive()
in 1..archives.size -> ArchiveMenu(archives[choice!! - 1], ioHandler).show()
archives.size + 1 -> {
ioHandler.showMessage("Выход из программы")
break
}
else -> ioHandler.showMessage("Неверный выбор, попробуйте снова.\n")
}
}
}

private fun createArchive() {
ioHandler.showMessage("Введите имя архива:")
val name = readlnOrNull().orEmpty()
if(name.isNotEmpty()) {
archives.add(Archive(name))
ioHandler.showMessage("Архив '$name' создан.\n")
}else{
ioHandler.showMessage("Имя архива не может быть пустым.\n")
}
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/Note.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Note(val name: String, val body: String)
16 changes: 16 additions & 0 deletions src/main/kotlin/NoteMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class NoteMenu(private val note: Note, private val ioHandler: IOHandler) {//меню заметок

fun show() {
while (true) {
ioHandler.showMessage("Заметка: ${note.name}")
ioHandler.showMessage("Текст: ${note.body}\n")
ioHandler.showMessage("0. Назад")
val choice = ioHandler.getUserChoice()
if(choice==0){
break
}else{
ioHandler.showMessage("Неверный ввод. Попробуйте снова.\n")
}
}
}
}