Skip to content
Open

Sec #232

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
123 changes: 120 additions & 3 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,120 @@
fun main(args: Array<String>) {
println("Hello World!")
}
import java.util.Scanner
fun main() {
val archives = mutableListOf<Archive>()
while (true){
val option = MenuHelper.getUserOption(
"""
Введите 0, чтобы создать архив
Введите 1, чтобы посмотреть созданные архивы
Введите 2, чтобы выйти
""".trimIndent(), 0, 2
)


when (option){
0 -> createArchive(archives)
1 -> showArchives(archives)
2 -> {
println("Завершаем программу")
break
}
else -> println("Некоректный ввод, введите цифру от 0 до 2")

}
}
}
fun createArchive(archives: MutableList<Archive>){

while (true){
println("Введите имя архива")
val name = readLine() ?: ""
if (archives.any { it.name == name }) {
println("Архив с таким именем уже существует. Выберите другое имя.")
continue
}
if (name.isEmpty()){
println("Имя архива не может быть пустым")
continue
}else{
archives.add(Archive(name))
println("Архив $name создан")
break
}

}

}
fun showArchives(archives: MutableList<Archive>){
if (archives.isEmpty()) {
println("У вас пока нет созданных архивов.")
return
}
println("Ваши архивы:")
archives.forEach{
i -> println(i.name)
}
val put = MenuHelper.getUserOption(
"""
Введите номер архива, чтобы управлять им, или введите -1 для возврата:
""".trimIndent(), -1, archives.size - 1
)
if (put == -1){
return
}
val archive = archives[put]
manage(archive)

}
fun manage(archive: Archive) {
while (true) {
val option = MenuHelper.getUserOption(
"""
Введите 0, чтобы добавить заметку
Введите 1, чтобы просмотреть заметки
Введите 2, чтобы вернуться назад
""".trimIndent(), 0, 2
)
when (option) {
0 -> createNote(archive)
1 -> showNotes(archive)
2 -> return
}
}
}
fun createNote(archive: Archive) {
var title: String
while (true) {
println("Введите название заметки")
title = readLine() ?: ""
if (archive.notes.any { it.title == title }) {
println("Заметка с таким названием уже существует в этом архиве. Пожалуйста, выберите другое название.")
continue
}
if (title.isEmpty()) {
println("Название заметки не может быть пустым")
continue
}else {break}

}
while (true){
println("Введите содержание заметки")
val content = readLine() ?: ""
if (content.isEmpty()){
println("заметка не может быть пустой")
continue
}else{
archive.notes.add(Note(title, content))
println("Заметка \"$title\" добавлена в архив \"${archive.name}\".")
break
}

}
}

fun showNotes(archive: Archive){
archive.notes.forEach{
i -> println("Название заметки ${i.title}. Содержание: ${i.content}")
}
}


3 changes: 3 additions & 0 deletions src/main/kotlin/arch.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 title: String, val content: String)
13 changes: 13 additions & 0 deletions src/main/kotlin/menu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
object MenuHelper {
fun getUserOption(prompt: String, minOption: Int, maxOption: Int): Int {
while (true) {
println(prompt)
val input = readLine()
val option = input?.toIntOrNull()
if (option != null && option in minOption..maxOption) {
return option
}
println("Некорректный ввод! Пожалуйста, введите число от $minOption до $maxOption.")
}
}
}