From fe64caef82b996a4ab01f0de899ccd3af368e554 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Sep 2026 15:55:42 +0300 Subject: [PATCH] changes made due to refactoring --- stars/Main.kt | 159 +++++++++++++++++++++----------------------------- 1 file changed, 68 insertions(+), 91 deletions(-) diff --git a/stars/Main.kt b/stars/Main.kt index b14d719..8e20d6c 100644 --- a/stars/Main.kt +++ b/stars/Main.kt @@ -36,6 +36,12 @@ val stars = mutableListOf(starFirst, "Яркий красный сверхгигант на правом плече Охотника. Полурегулярная переменная звезда, находящаяся на поздней стадии эволюции.", listOf(15,29), 5.0 + ), + Star(4,2, + "Бетельгейзе4", + "Яркий красный сверхгигант на правом плече Охотника. Полурегулярная переменная звезда, находящаяся на поздней стадии эволюции.", + listOf(1,29), + 960.0 )) val constellations = mutableListOf(constellationFirst) @@ -46,6 +52,20 @@ fun printStarInfo(star: Star){ fun printConstellationInfo(constellation: Constellation){ println("${constellation.name} ${stars.count{it.constellation == constellation.identifier}} ${constellation.description.take(30)}") } +fun getConstellation(): String{ + println("Введите созвездие:") + val constellationsList = constellations.map { it.name } + var constellationName: String = readln().lowercase().replaceFirstChar { it.uppercase() } + while (true) { + if (constellationName in constellationsList){ + break + } + println("Список созвездий: ${constellationsList.joinToString(", ")}") + constellationName = readln().lowercase().replaceFirstChar { it.uppercase() } + + } + return constellationName +} // Главная функция fun main(){ @@ -64,15 +84,12 @@ fun main(){ when (input){ "1" -> { // Перебираем все звезды и выводим информацию о них - for (item in stars){ - printStarInfo(item) - } + stars.forEach { printStarInfo(it)} + } "2" -> { // Перебираем все созвездия и выводим информацию о них - for (item in constellations){ - printConstellationInfo(item) - } + constellations.forEach { printConstellationInfo(it)} } "3" -> { val map = Array(30) { CharArray(30) { ' ' }} @@ -100,41 +117,24 @@ fun main(){ val input = readln() when (input) { "a" -> { - println("Введите созвездие:") - val constellationsList = constellations.map { it.name } - var constellationName: String = readln().lowercase().replaceFirstChar { it.uppercase() } - while (true) { - if (constellationName in constellationsList){ - break - } - else { - println("Список созвездий: ${constellationsList.joinToString(", ")}") - } - constellationName = readln().lowercase().replaceFirstChar { it.uppercase() } - - } + val constellationName = getConstellation() println("Введите название звезды:") var starName: String = readln() while (true) { - - if (starName == "" || starName == " ") { - println("Введите название звезды") - } - else { + if (starName.isNotBlank()) { break } + println("Введите название звезды") starName = readln() } println("Введите описание звезды:") var starDescription: String = readln() while (true) { - if (starDescription == "" || starDescription == " ") { - println("Введите описание звезды") - } - else { + if (starDescription.isNotBlank()) { break } + println("Введите описание звезды") starDescription = readln() } @@ -146,10 +146,8 @@ fun main(){ break } - else { - println("Введите координаты через пробел от 0 до 30") + println("Введите координаты через пробел от 0 до 30") - } starCoordinates = readln().split(" ").mapNotNull { it.toIntOrNull() } } @@ -161,10 +159,9 @@ fun main(){ break } - else { - println("Введите размер звезды") + println("Введите размер звезды") + - } starSize = readln().toDoubleOrNull() @@ -172,77 +169,69 @@ fun main(){ // Cоздаем id для звезды val starId:Int = (stars.maxOfOrNull {it.identifier } ?:0) + 1 // Находим id созвездия - val constellationId: Int? = (constellations.find{it.name == constellationName})?.identifier + val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier // Создаем звезду - stars.add(Star(starId,constellationId!!,starName,starDescription,starCoordinates,starSize)) + if (constellationId != null) { + stars.add( + Star( + starId, + constellationId, + starName, + starDescription, + starCoordinates, + starSize + ) + ) + } // Выводим созданную звезду print("Создана звезда: ") - printStarInfo(stars.find {it.identifier == starId}!!) + val star = stars.firstOrNull {it.identifier == starId} + if (star != null) { + printStarInfo(star) + } break } "b" -> { - println("Введите созвездие:") - val constellationsList = constellations.map { it.name } - var constellationName: String = readln().lowercase().replaceFirstChar { it.uppercase() } - while (true) { - if (constellationName in constellationsList){ - break - } - else { - println("Список созвездий: ${constellationsList.joinToString(", ")}") - } - constellationName = readln().lowercase().replaceFirstChar { it.uppercase() } - - } + val constellationName = getConstellation() println("Введите новое описание созвездия:") var constellationDescription: String = readln() while (true) { - if (constellationDescription == "" || constellationDescription == " ") { - println("Введите описание созвездия") - } - else { + if (constellationDescription.isNotBlank()) { break } + println("Введите описание созвездия") + constellationDescription = readln() } - val constellation = constellations.find{it.name == constellationName} - constellation!!.description = constellationDescription - print("Измененное созвездие: ") - printConstellationInfo(constellation) + val constellation = constellations.firstOrNull {it.name == constellationName} + if (constellation != null) { + constellation.description = constellationDescription + print("Измененное созвездие: ") + printConstellationInfo(constellation) + } break } "c" -> { - println("Введите созвездие:") - val constellationsList = constellations.map { it.name } - var constellationName: String = readln().lowercase().replaceFirstChar { it.uppercase() } - while (true) { - if (constellationName in constellationsList){ - break - } - else { - println("Список созвездий: ${constellationsList.joinToString(", ")}") - } - constellationName = readln().lowercase().replaceFirstChar { it.uppercase() } - - } + val constellationName = getConstellation() println("Введите звезду:") - val starsList = stars.map { it.name } + // Находим id созвездия + val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier + val starsList = stars.filter{it.constellation == constellationId }.map{it.name} var starName: String = readln().lowercase().replaceFirstChar { it.uppercase() } while (true) { if (starName in starsList){ break } - else { - println("Список звезд: ${starsList.joinToString(", ")}") - } + println("Список звезд: ${starsList.joinToString(", ")}") + starName = readln().lowercase().replaceFirstChar { it.uppercase() } } @@ -269,25 +258,13 @@ fun main(){ } "5" -> { - println("Введите созвездие:") - val constellationsList = constellations.map { it.name } - var constellationName: String = readln().lowercase().replaceFirstChar { it.uppercase() } - while (true) { - if (constellationName in constellationsList){ - break - } - else { - println("Список созвездий: ${constellationsList.joinToString(", ")}") - } - constellationName = readln().lowercase().replaceFirstChar { it.uppercase() } - - } + val constellationName = getConstellation() println("Вы точно хотите удалить созведие и звезды? Y/N") - val input = readln() - if (input in "Yy"){ + val input = readln().lowercase() + if (input == "y"){ // Находим id созвездия - val constellationId: Int? = (constellations.find{it.name == constellationName})?.identifier + val constellationId: Int? = (constellations.firstOrNull {it.name == constellationName})?.identifier stars.removeIf{it.constellation == constellationId} constellations.removeIf{it.name == constellationName} println("Созвездия и звезды успешно удалены")