Skip to content

Проектная работа на котлин #229

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 3 commits into
base: dev
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
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(override val name: String) : Selectable {
var notes = arrayListOf<Note>()
}
45 changes: 45 additions & 0 deletions src/main/kotlin/Input.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Scanner

class Input {
companion object {
var scanner = Scanner(System.`in`)

fun intInput(menuSize: Int): Int {
var input = -1

do {
try {
println("Введите номер пункта меню:")
input = scanner.nextLine().toInt()
if (input > menuSize) {
println("Такой пункт отсутствует")
input = -1
}
} catch (e: Exception) {
println("Необоходимо ввести число.")
}
} while (input < 0)
return input
}

fun textInput(prompt: String, maxLength: Int): String {

var input = ""
val max = maxLength

do {
println(prompt)
input = scanner.nextLine().trim()
when (input.length) {
in 1..max -> return input
in 0..0 -> println("Значение не может быть пустым")
else -> {
println("Введено слишком длинное значение")
input = ""
}
}
} while (input == "")
return input
}
}
}
7 changes: 6 additions & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
fun main(args: Array<String>) {
println("Hello World!")

val screen = MainScreen()
do {
screen.showMenu()
if (screen.isExit) break
} while (true)
}
103 changes: 103 additions & 0 deletions src/main/kotlin/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
class MainScreen() {
private var screenType = ARCHIVE
private val data = mutableMapOf<Archive, ArrayList<Note>>()
var isExit = false

private lateinit var active: Archive //ссылка на архив для экрана с записями

fun showMenu(screenType: List<String> = this.screenType) {
val caption = "_______ Меню ${screenType[2].lowercase()} _______"
val firstItem = "Создать ${screenType[1].lowercase()}"
val lastItem = "Выйти"
var input = 0
var num = 0
val dataItems = if (screenType == ARCHIVE) data.keys.toList() else active.notes.toList()

println(caption)
println("$num. $firstItem")
for (item in dataItems) {
println("${++num}. \"${item.name}\"")
}
println("${++num}. ${lastItem}")
println("---------------")

input = Input.intInput(num)
select(dataItems, input, num)
}

private fun select(dataItems: List<Selectable>, input: Int, menuSize: Int) {
if (menuSize > 1) {
when (input) {
0 -> create()
in 1 until menuSize -> {
if (screenType == ARCHIVE) {
active = dataItems[input - 1] as Archive
screenType = NOTE
} else {
showNote(dataItems[input - 1] as Note)
}
}

menuSize -> {
if (screenType == NOTE) {
screenType = ARCHIVE
} else {
isExit = true
}
}
}
} else {
when (input) {
0 -> create()
1 -> {
if (screenType == NOTE) {
screenType = ARCHIVE
} else {
isExit = true
}
}
}
}
}

private fun create() {
println("Процедура создания ${screenType[2].lowercase()}")

val name = Input.textInput(
"Введите название ${screenType[2].lowercase()}}:\n(не более $NAME_MAX_LENGTH символов)",
NAME_MAX_LENGTH
)

when (screenType) {
ARCHIVE -> {
val archive = Archive(name)
data.put(archive, archive.notes)
}

NOTE -> {
val text = Input.textInput(
"Введите текст ${NOTE[2].lowercase()}:\n(не более $TEXT_MAX_LENGTH символа)",
TEXT_MAX_LENGTH
)
val note = Note(name, text)
active.notes.add(note)
}
}
}

private fun showNote(note: Note) {
println("====== Просмотр записи ${note.name} ======")
println("Содержимое записи:")
println("${note.text}")
println("====== Конец ======")
println("\nВведите любой символ, чтобы вернуться в меню...")
Input.scanner.nextLine()
}

companion object {
const val NAME_MAX_LENGTH = 32
const val TEXT_MAX_LENGTH = 1024
val ARCHIVE = listOf("Архив", "Архива", "Архивов")
val NOTE = listOf("Запись", "Записи", "Записей")
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/Note.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Note(override val name: String, val text: String) : Selectable {
}
3 changes: 3 additions & 0 deletions src/main/kotlin/ScreenNote.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ScreenNote {

}
3 changes: 3 additions & 0 deletions src/main/kotlin/Selectable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Selectable {
val name: String
}