Skip to content
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
1,258 changes: 1,258 additions & 0 deletions .idea/caches/deviceStreaming.xml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions .idea/deviceManager.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/markdown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.

18 changes: 16 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
fun main(args: Array<String>) {
println("Hello World!")
import menu.MenuNavigator
import models.Archive
import screens.ArchiveSelectionScreen

fun main() {
println("Добро пожаловать в приложение Заметки!")
println("Версия 1.0")


val archives = mutableListOf<Archive>()


val archiveSelectionScreen = ArchiveSelectionScreen(archives)


MenuNavigator.startWith(archiveSelectionScreen)
}
50 changes: 50 additions & 0 deletions src/main/kotlin/menu/Menu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package menu

import java.util.Scanner

abstract class Menu {
protected val scanner = Scanner(System.`in`)
protected val menuItems = mutableListOf<MenuItem>()

protected fun addMenuItem(item: MenuItem) {
menuItems.add(item)
}

protected fun displayMenu(header: String) {
println("\n${"=".repeat(50)}")
println(header)
println("${"=".repeat(50)}")

menuItems.sortedBy { it.index }.forEach { item ->
println("${item.index}. ${item.title}")
}

println("${"=".repeat(50)}")
print("Выберите пункт меню: ")
}

open fun show() {
while (true) {
displayMenu(getMenuHeader())

try {
val choice = scanner.nextLine().toInt()
val menuItem = menuItems.find { it.index == choice }

if (menuItem != null) {
menuItem.action()
if (shouldExitAfterAction(choice)) {
break
}
} else {
println("Ошибка: выбран неверный пункт меню. Попробуйте снова.")
}
} catch (e: NumberFormatException) {
println("Ошибка: введите номер пункта меню.")
}
}
}

protected abstract fun getMenuHeader(): String
protected abstract fun shouldExitAfterAction(choice: Int): Boolean
}
7 changes: 7 additions & 0 deletions src/main/kotlin/menu/MenuItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package menu

data class MenuItem(
val index: Int,
val title: String,
val action: () -> Unit
)
37 changes: 37 additions & 0 deletions src/main/kotlin/menu/MenuNavigator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package menu

object MenuNavigator {
private val screenStack = mutableListOf<Menu>()

fun pushScreen(screen: Menu) {
screenStack.add(screen)
}

fun popScreen() {
if (screenStack.isNotEmpty()) {
screenStack.removeLast()
}
}

fun startWith(screen: Menu) {
screenStack.clear()
pushScreen(screen)
showCurrentScreen()
}

fun navigateTo(screen: Menu) {
pushScreen(screen)
showCurrentScreen()
}

fun back() {
popScreen()
if (screenStack.isNotEmpty()) {
showCurrentScreen()
}
}

private fun showCurrentScreen() {
screenStack.lastOrNull()?.show()
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/models/Archive.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models

class Archive(val name: String) {
private val noteStorage = mutableListOf<Note>()

fun addNote(note: Note) {
noteStorage.add(note)
}


fun getNotes(): List<Note> = noteStorage.toList()


fun getNotesCount(): Int = noteStorage.size
}
7 changes: 7 additions & 0 deletions src/main/kotlin/models/Note.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

data class Note(
val title: String,
val content: String,
val createdAt: Long = System.currentTimeMillis()
)
52 changes: 52 additions & 0 deletions src/main/kotlin/screens/ArchiveCreationScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package screens

import models.Archive
import menu.Menu
import menu.MenuNavigator
import java.util.Scanner

class ArchiveCreationScreen(private val archives: MutableList<Archive>) : Menu() {

init {

menuItems.add(menu.MenuItem(1, "Вернуться назад") {})
}

override fun show() {
println("\n${"=".repeat(50)}")
println("СОЗДАНИЕ АРХИВА")
println("${"=".repeat(50)}")

var archiveName: String
val scanner = Scanner(System.`in`)

while (true) {
print("Введите название архива: ")
archiveName = scanner.nextLine().trim()

if (archiveName.isEmpty()) {
println("Ошибка: название архива не может быть пустым.")
continue
}

if (archives.any { it.name == archiveName }) {
println("Ошибка: архив с таким названием уже существует.")
continue
}

break
}

val archive = Archive(archiveName)
archives.add(archive)
println("Архив '$archiveName' успешно создан!")


Thread.sleep(1000)
MenuNavigator.back()
}

override fun getMenuHeader(): String = "Создание архива"

override fun shouldExitAfterAction(choice: Int): Boolean = true
}
48 changes: 48 additions & 0 deletions src/main/kotlin/screens/ArchiveSelectionScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package screens

import models.Archive
import menu.Menu
import menu.MenuItem
import menu.MenuNavigator

class ArchiveSelectionScreen(private val archives: MutableList<Archive>) : Menu() {

override fun show() {
menuItems.clear()
initializeMenu()
super.show()
}

private fun initializeMenu() {
// Пункт для создания нового архива
addMenuItem(MenuItem(0, "Создать архив") {
MenuNavigator.navigateTo(ArchiveCreationScreen(archives))
})

// Пункты для существующих архивов
archives.forEachIndexed { index, archive ->
addMenuItem(MenuItem(index + 1, archive.name) {
MenuNavigator.navigateTo(NoteSelectionScreen(archive))
})
}

// Пункт для выхода
val exitIndex = archives.size + 1
addMenuItem(MenuItem(exitIndex, "Выход") {
println("До свидания!")
System.exit(0)
})
}

override fun getMenuHeader(): String {
return if (archives.isEmpty()) {
"Список архивов (пока пуст)"
} else {
"Список архивов:"
}
}

override fun shouldExitAfterAction(choice: Int): Boolean {
return false
}
}
71 changes: 71 additions & 0 deletions src/main/kotlin/screens/NoteCreationScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package screens

import models.Note
import models.Archive
import menu.Menu
import menu.MenuItem
import menu.MenuNavigator
import java.util.Scanner

class NoteCreationScreen(private val archive: Archive) : Menu() {

init {
menuItems.add(MenuItem(1, "Вернуться назад") {})
}

override fun show() {
println("\n${"=".repeat(50)}")
println("СОЗДАНИЕ ЗАМЕТКИ")
println("Архив: ${archive.name}")
println("${"=".repeat(50)}")

val scanner = Scanner(System.`in`)

// Ввод названия заметки
var title: String
while (true) {
print("Введите название заметки: ")
title = scanner.nextLine().trim()

if (title.isEmpty()) {
println("Ошибка: название заметки не может быть пустым.")
continue
}

break
}


println("Введите текст заметки (введите пустую строку для завершения):")
val lines = mutableListOf<String>()

while (true) {
print("> ")
val line = scanner.nextLine()
if (line.trim().isEmpty() && lines.isNotEmpty()) {
break
}
if (line.trim().isNotEmpty()) {
lines.add(line)
}
}

val content = lines.joinToString("\n")

if (content.trim().isEmpty()) {
println("Ошибка: текст заметки не может быть пустым.")
} else {
val note = Note(title, content)
archive.addNote(note)
println("Заметка '$title' успешно создана!")
}


Thread.sleep(1000)
MenuNavigator.back()
}

override fun getMenuHeader(): String = "Создание заметки"

override fun shouldExitAfterAction(choice: Int): Boolean = true
}
Loading