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

Консольное приложение "Заметки" #206

Open
wants to merge 4 commits 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
329 changes: 329 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.

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

fun createNote() {
println("Название заметки:")
val nameNote = IsEmptyInputCheck.checkEmptyInput("Название заметки не может быть пустым")
println("Содержимое заметки")
val content = IsEmptyInputCheck.checkEmptyInput("Содержимое заметки не может быть пустым")
noteList.add(Note(nameNote, content))
println("Заметка $nameNote успешно создана")
}

fun showNoteList() {
if (noteList.isEmpty()) {
println("Архив $name пуст")
} else {
val menuItems = noteList.map { it.nameNote }
showMenu(Operations.NOTE_MENU, menuItems, "Вернуться к архивам") { index ->
noteList[index].readNote()
}
}
}
}
33 changes: 33 additions & 0 deletions src/main/kotlin/Archives.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Archives : NavigationMenu() {
private val archiveList: MutableList<Archive> = mutableListOf()

fun createArchive() {
val nameArchive = IsEmptyInputCheck.checkEmptyInput("Название архива не может быть пустым")
archiveList.add(Archive(nameArchive))
println("Архив $nameArchive успешно добавлен")
}

fun showArchiveList() {
if (archiveList.isEmpty()) {
println("Список архивов пуст")
} else {
val menuItems = archiveList.map { it.name }
showMenu(Operations.ARCHIVES_MENU, menuItems, "Вернуться к главному меню") { index ->
openArchiveMenu(archiveList[index])
}
}
}

fun openArchiveMenu(archive: Archive) {
val menuItems = listOf("Добавить заметку", "Просмотреть заметки")
showMenu(Operations.NOTES_MENU, menuItems, "Вернуться к списку архивов") { input ->
when (input) {
0 -> archive.createNote()
1 -> {
println("Переходим в заметки архива ${archive.name}...")
archive.showNoteList()
}
}
}
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/IsEmptyInputCheck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.Scanner

object IsEmptyInputCheck {

fun checkEmptyInput(errorMessage: String): String {
while (true) {
val input = Scanner(System.`in`).nextLine()
if (input.isNotBlank()) return input
println(errorMessage)
}
}
}
3 changes: 2 additions & 1 deletion 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!")
val start = Start()
start.startProgramm()
}
25 changes: 25 additions & 0 deletions src/main/kotlin/NavigationMenu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*

open class NavigationMenu {
private val scanner = Scanner(System.`in`)

fun showMenu(menuName: Operations, menuItems: List<String>, exitMenu: String, numberMenu: (Int) -> Unit
) {
while (true) {
menuName.getTypeMenu()
menuItems.forEachIndexed { index, value ->
println("${index + 1} - $value")
}
println("0 - $exitMenu")

val input = scanner.nextLine()
if (input == "0") {
return
} else if (input.toIntOrNull() != null && input.toInt() <= menuItems.size && input.toInt() > 0) {
numberMenu(input.toInt() - 1)
} else {
println("Некорректный ввод. Выберите пункт из списка.")
}
}
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/Note.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data class Note(
val nameNote: String,
val content: String
) {

fun readNote() {
println("Заметка: $nameNote\nСодержание: $content")
}
}
14 changes: 14 additions & 0 deletions src/main/kotlin/Operations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
enum class Operations {
MAIN_MENU,
ARCHIVES_MENU,
NOTES_MENU,
NOTE_MENU;

fun getTypeMenu(): Unit =
when (this) {
MAIN_MENU -> println("\nГлавне меню:")
ARCHIVES_MENU -> println("\nМеню - спсиок архивов:")
NOTES_MENU -> println("\nМеню - заметки:")
NOTE_MENU -> println("\nМеню - список заметок:")
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/Start.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Start : NavigationMenu() {
private val archives = Archives()

fun startProgramm() {
val menuItems = listOf("Создать архив", "Это мой уже созданный архив")
showMenu(Operations.MAIN_MENU,menuItems, "Выход") { input ->
when (input) {
0 -> { println("Задайте имя для архива")
archives.createArchive() }
1 -> { println("Переходим в ваши архивы...")
archives.showArchiveList() }
}
}
}
}