From a3923b79a55b22b9e16274800643cc3c54c125de Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 14 Sep 2026 13:00:28 +0300 Subject: [PATCH] decomposition completed --- stars/Main.kt | 383 +++++++++++++++++++++++++------------------------- 1 file changed, 188 insertions(+), 195 deletions(-) diff --git a/stars/Main.kt b/stars/Main.kt index 1400347..4fbad63 100644 --- a/stars/Main.kt +++ b/stars/Main.kt @@ -6,12 +6,21 @@ data class Star (val identifier: Int, val name: String, val description: String, val coordinates: List, - val size: Double) + val size: Double){ + override fun toString(): String { + return "$name $constellation ${description.take(30)}" + } + +} // Класс для хранения созвездий data class Constellation (val identifier: Int, val name: String, - var description: String) + var description: String){ + override fun toString(): String { + return "$name ${stars.count{it.constellation == identifier}} ${description.take(30)}" + } +} // Пробная звезда val starFirst = Star(1,1, "Бетельгейзе", @@ -20,7 +29,6 @@ val starFirst = Star(1,1, 877.0 ) - // Пробное созвездие val constellationFirst = Constellation(1, "Орион", "одно из самых ярких и узнаваемых созвездий на ночном небе, расположенное в области небесного экватора") @@ -47,21 +55,12 @@ val stars = mutableListOf(starFirst, )) val constellations = mutableListOf(constellationFirst) -// Функция вывода информации о звезде -fun printStarInfo(star: Star){ - println("${star.name} ${star.constellation} ${star.description.take(30)}") -} -fun printConstellationInfo(constellation: Constellation){ - println("${constellation.name} ${stars.count{it.constellation == constellation.identifier}} ${constellation.description.take(30)}") -} +// Функция выбора созвездия fun getConstellation(): String{ println("Введите созвездие:") var constellationName: String = readln().lowercase().replaceFirstChar { it.uppercase() } while (true) { - - if (constellations.firstOrNull{it.name == constellationName} != null){ - break - } + if (constellations.firstOrNull{it.name == constellationName} != null) break print("Список созвездий: ") constellations.forEach { print(it.name) } println() @@ -71,6 +70,175 @@ fun getConstellation(): String{ return constellationName } +// Функция вывода звездной карты +fun printMap(){ + val map = Array(30) { CharArray(30) { ' ' }} + for (star in stars){ + val symbol = when { + star.size > 500.0 -> '★' + star.size > 50.0 -> '✦' + else -> '•' + } + map[star.coordinates[0]][star.coordinates[1]] = symbol + } + for (row in map) { + println(row.joinToString(" ")) + } +} + +// Функция добавления новой звезды +fun addNewStar(){ + val constellationName = getConstellation() + println("Введите название звезды:") + var starName: String = readln() + while (true) { + if (starName.isNotBlank()) break + println("Введите название звезды") + starName = readln() + } + println("Введите описание звезды:") + var starDescription: String = readln() + while (true) { + if (starDescription.isNotBlank()) break + println("Введите описание звезды") + starDescription = readln() + } + println("Введите координаты звезды:") + var starCoordinates = readln().split(" ").mapNotNull { it.toIntOrNull() } + + while (true) { + if (starCoordinates.size == 2 && starCoordinates[0] < 30 && starCoordinates[1] < 30) break + println("Введите координаты через пробел от 0 до 30") + starCoordinates = readln().split(" ").mapNotNull { it.toIntOrNull() } + } + println("Введите размер звезды:") + var starSize: Double? = readln().toDoubleOrNull() + while (true) { + if (starSize != null) break + println("Введите размер звезды") + starSize = readln().toDoubleOrNull() + } + // Cоздаем id для звезды + val starId:Int = (stars.maxOfOrNull {it.identifier } ?:0) + 1 + // Находим id созвездия + val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier + // Создаем звезду + if (constellationId != null) { + val newStar = Star( + starId, + constellationId, + starName, + starDescription, + starCoordinates, + starSize + ) + stars.add(newStar) + } + // Выводим созданную звезду + print("Создана звезда: ") + val star = stars.firstOrNull {it.identifier == starId} + if (star != null) { + println(star) + } + +} + +// Функция изменения описания созвездия +fun editConstellationDescription(){ + val constellationName = getConstellation() + + println("Введите новое описание созвездия:") + var constellationDescription: String = readln() + while (true) { + if (constellationDescription.isNotBlank()) break + println("Введите описание созвездия") + constellationDescription = readln() + } + val constellation = constellations.firstOrNull {it.name == constellationName} + if (constellation != null) { + constellation.description = constellationDescription + print("Измененное созвездие: ") + println(constellation) + } +} + +// Функция удаления звезды +fun deleteStar(){ + val constellationName = getConstellation() + + println("Введите звезду:") + // Находим id созвездия + val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier + var starName: String = readln().lowercase().replaceFirstChar { it.uppercase() } + while (true) { + if (stars.firstOrNull{it.name == starName && it.constellation == constellationId} != null) break + println("Список звезд: ") + stars.forEach { print(it.name + " ") } + println() + + starName = readln().lowercase().replaceFirstChar { it.uppercase() } + + } + + println("Вы точно хотите удалить звезду? Y/N") + val input = readln() + if (input in "Yy"){ + stars.removeIf{it.name == starName} + println("Звезда успешно удалена") + } + else { + println("Отмена") + } +} + +// Функция изменения созвездия +fun editConstellation(){ + while (true) { + println("Меню") + println("a. Добавить новую звезду в созвездие") + println("b. Изменить описание") + println("c. Удалить звезду по идентификатору") + println("d. Назад") + val input = readln() + when (input) { + "a" -> { + addNewStar() + break + } + "b" -> { + editConstellationDescription() + break + } + "c" -> { + deleteStar() + break + } + "d" -> break + else -> { + println("Такого нет в меню") + } + } + } +} + +// Функция удаления созвездия +fun deleteConstellation(){ + val constellationName = getConstellation() + + println("Вы точно хотите удалить созведие и звезды? Y/N") + val input = readln().lowercase() + if (input == "y"){ + // Находим id созвездия + val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier + stars.removeIf{it.constellation == constellationId} + constellations.removeIf{it.name == constellationName} + println("Созвездия и звезды успешно удалены") + } + else { + println("Отмена") + } +} + // Главная функция fun main(){ // Главное меню @@ -88,195 +256,20 @@ fun main(){ when (input){ "1" -> { // Перебираем все звезды и выводим информацию о них - stars.forEach { printStarInfo(it)} - + println(stars.joinToString(separator="\n" )) } "2" -> { // Перебираем все созвездия и выводим информацию о них - constellations.forEach { printConstellationInfo(it)} + println(constellations.joinToString(separator="\n" )) } "3" -> { - val map = Array(30) { CharArray(30) { ' ' }} - for (star in stars){ - val symbol = when { - star.size > 500.0 -> '★' - star.size > 50.0 -> '✦' - else -> '•' - - } - map[star.coordinates[0]][star.coordinates[1]] = symbol - - } - for (row in map) { - println(row.joinToString(" ")) - } + printMap() } "4" -> { - while (true) { - println("Меню") - println("a. Добавить новую звезду в созвездие") - println("b. Изменить описание") - println("c. Удалить звезду по идентификатору") - println("d. Назад") - val input = readln() - when (input) { - "a" -> { - val constellationName = getConstellation() - println("Введите название звезды:") - var starName: String = readln() - while (true) { - if (starName.isNotBlank()) { - break - } - println("Введите название звезды") - starName = readln() - } - println("Введите описание звезды:") - var starDescription: String = readln() - while (true) { - - if (starDescription.isNotBlank()) { - break - } - println("Введите описание звезды") - starDescription = readln() - - } - println("Введите координаты звезды:") - var starCoordinates = readln().split(" ").mapNotNull { it.toIntOrNull() } - - while (true) { - if (starCoordinates.size == 2 && starCoordinates[0] < 30 && starCoordinates[1] < 30) { - break - - } - println("Введите координаты через пробел от 0 до 30") - - starCoordinates = readln().split(" ").mapNotNull { it.toIntOrNull() } - - } - println("Введите размер звезды:") - var starSize: Double? = readln().toDoubleOrNull() - while (true) { - - if (starSize != null) { - break - - } - println("Введите размер звезды") - - - starSize = readln().toDoubleOrNull() - - - } - // Cоздаем id для звезды - val starId:Int = (stars.maxOfOrNull {it.identifier } ?:0) + 1 - // Находим id созвездия - val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier - // Создаем звезду - if (constellationId != null) { - stars.add( - Star( - starId, - constellationId, - starName, - starDescription, - starCoordinates, - starSize - ) - ) - } - // Выводим созданную звезду - print("Создана звезда: ") - val star = stars.firstOrNull {it.identifier == starId} - if (star != null) { - printStarInfo(star) - } - break - } - "b" -> { - - val constellationName = getConstellation() - - println("Введите новое описание созвездия:") - var constellationDescription: String = readln() - while (true) { - - if (constellationDescription.isNotBlank()) { - break - } - println("Введите описание созвездия") - - constellationDescription = readln() - - } - - val constellation = constellations.firstOrNull {it.name == constellationName} - if (constellation != null) { - constellation.description = constellationDescription - print("Измененное созвездие: ") - printConstellationInfo(constellation) - } - break - - - } - "c" -> { - val constellationName = getConstellation() - - println("Введите звезду:") - // Находим id созвездия - val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier - var starName: String = readln().lowercase().replaceFirstChar { it.uppercase() } - while (true) { - if (stars.firstOrNull{it.name == starName && it.constellation == constellationId} != null){ - break - } - println("Список звезд: ") - stars.forEach { print(it.name + " ") } - println() - - starName = readln().lowercase().replaceFirstChar { it.uppercase() } - - } - - println("Вы точно хотите удалить звезду? Y/N") - val input = readln() - if (input in "Yy"){ - stars.removeIf{it.name == starName} - println("Звезда успешно удалена") - } - else { - println("Отмена") - } - break - } - "d" -> { - break - } - else -> { - println("Такого нет в меню") - } - } - } - + editConstellation() } "5" -> { - val constellationName = getConstellation() - - println("Вы точно хотите удалить созведие и звезды? Y/N") - val input = readln().lowercase() - if (input == "y"){ - // Находим id созвездия - val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier - stars.removeIf{it.constellation == constellationId} - constellations.removeIf{it.name == constellationName} - println("Созвездия и звезды успешно удалены") - } - else { - println("Отмена") - } + deleteConstellation() } "6" -> { println("До свидания!") @@ -287,4 +280,4 @@ fun main(){ } } } -} +} \ No newline at end of file