generated from admin/template
Добавить works/lab-01/8.kt
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
//class Pokemon (
|
||||
// val name: String,
|
||||
// var hp: Int
|
||||
//) {
|
||||
// init {
|
||||
// require(name.isNotBlank()) {"Name cannot be empty"}
|
||||
// require(hp in 0..999) {"Health must be positive, not $hp"}
|
||||
// }
|
||||
//
|
||||
// fun isAlive() = hp > 0
|
||||
//
|
||||
// fun takeDamage(amount: Int): Int {
|
||||
// require(amount >= 0) {"Damage must not be negative"}
|
||||
// val before = hp
|
||||
// hp = (hp - amount).coerceAtLeast(0)
|
||||
// return before - hp
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//fun main() {
|
||||
// // Задание 1
|
||||
// val pikachu = Pokemon("Pikachu", 100)
|
||||
// println(pikachu)
|
||||
// pikachu.takeDamage(30)
|
||||
// println(pikachu.hp)
|
||||
// println(pikachu.isAlive())
|
||||
// pikachu.takeDamage(80)
|
||||
// println(pikachu.hp)
|
||||
// println(pikachu.isAlive())
|
||||
//
|
||||
// pikachu.hp = 50
|
||||
// // pikachu.name = "Raichu" // 'val' cannot be reassigned
|
||||
//
|
||||
// // Задание 2
|
||||
// pikachu.hp = 50
|
||||
// // pikachu.name = "Raichu" //- 'val' cannot be reassigned
|
||||
// // pikachu = Pokemon("Raichu", 100) //- 'val' cannot be reassigned
|
||||
//
|
||||
// val team = mutableListOf<Pokemon>()
|
||||
// team.add(pikachu)
|
||||
// // MutableList, так что нормально
|
||||
//
|
||||
// // Задание 3
|
||||
// var pikapika = Pokemon("Pikachu", 30)
|
||||
// // pikapika = Pokemon("", 100)
|
||||
// // Exception in thread "main" java.lang.IllegalArgumentException: Name cannot be empty
|
||||
// // Pokemon("Mewtwo", 5000)
|
||||
// // Exception in thread "main" java.lang.IllegalArgumentException: Health must be positive, not 5000
|
||||
//
|
||||
// pikapika.takeDamage(100)
|
||||
// println(pikapika.hp)
|
||||
//}
|
||||
|
||||
//class Pokemon(
|
||||
// val name: String,
|
||||
// var hp: Int,
|
||||
// val maxHp: Int
|
||||
//) {
|
||||
// init {
|
||||
// require (name.isNotBlank()) {"Name must not be blank"}
|
||||
// require (maxHp in 1..999) {"Max HP must be between 1 and 999"}
|
||||
// require (hp in 0..maxHp) { "HP must be between 0 and max HP, not $hp" }
|
||||
// }
|
||||
//
|
||||
// fun takeDamage(damage: Int): Int {
|
||||
// require (damage >= 0) { "Damage must be positive during takeDamage" }
|
||||
// val before = hp
|
||||
// hp = (hp - damage).coerceAtLeast(0)
|
||||
// return before - hp
|
||||
// }
|
||||
//
|
||||
// fun heal(amount: Int): Int {
|
||||
// require (amount >= 0) { "Amount must be positive during heal" }
|
||||
// val before = hp
|
||||
// hp = (hp + amount).coerceAtMost(maxHp)
|
||||
// return before + hp
|
||||
// }
|
||||
//
|
||||
// fun isAlive(): Boolean = hp > 0
|
||||
//}
|
||||
//fun main() {
|
||||
//// var bolb = Pokemon("Bulbasaur", hp = 30, maxHp = 100)
|
||||
//// bolb.takeDamage(20)
|
||||
//// println(bolb.hp)
|
||||
//// bolb.takeDamage(10)
|
||||
//// println(bolb.hp)
|
||||
//// bolb.heal(200)
|
||||
//// println(bolb.hp)
|
||||
//// bolb.heal(50)
|
||||
//// println(bolb.hp)
|
||||
//
|
||||
// var poke1 = Pokemon("Poke", hp = 100, maxHp = 120)
|
||||
// var poke2 = Pokemon("Mon", hp = 115, maxHp = 115)
|
||||
// var poke3 = Pokemon("Chi", hp = 0, maxHp = 60)
|
||||
// var poke4 = Pokemon("Ken", hp = 60, maxHp = 75)
|
||||
// var poke5 = Pokemon("Rock", hp = 150, maxHp = 150)
|
||||
//
|
||||
// var team: MutableList<Pokemon> = mutableListOf(poke1, poke2, poke3, poke4, poke5)
|
||||
// println(team.size)
|
||||
// println(team.filter{ it.isAlive() }.size)
|
||||
// println(team.maxByOrNull { it.hp }?.name)
|
||||
// println(team.filter{ it.isAlive() }.map { it.name })
|
||||
// if (team.filter{ !it.isAlive() }.isNotEmpty()) {
|
||||
// println(team.filter{ !it.isAlive() }.map { it.name }[0])
|
||||
// } else {
|
||||
// println("нет мёртвых")
|
||||
// }
|
||||
// println(team.filter{ it.isAlive() }.map { it.hp }.average())
|
||||
//}
|
||||
|
||||
//class Kvas1(
|
||||
// val name: String,
|
||||
// val type: String,
|
||||
// val level: Int
|
||||
//) {
|
||||
//
|
||||
//}
|
||||
//data class Kvas2(
|
||||
// val name: String,
|
||||
// val type: String,
|
||||
// val level: Int
|
||||
//) {
|
||||
//
|
||||
//}
|
||||
//
|
||||
//val a = Kvas1("Mat", "lab", 30)
|
||||
//val b = Kvas1("Mat", "lab", 30)
|
||||
//val c = Kvas1("Pat", "cab", 15)
|
||||
//
|
||||
//val d = Kvas2("Rat", "sab", 25)
|
||||
//val e = Kvas2("Rat", "sab", 25)
|
||||
//val f = Kvas2("Bat", "nab", 20)
|
||||
//
|
||||
//fun main(){
|
||||
// println(a)
|
||||
// println(d)
|
||||
// println(a == b)
|
||||
// println(d == e)
|
||||
// println("${a.hashCode()} ${b.hashCode()} vs ${d.hashCode()} ${e.hashCode()}")
|
||||
// println(f.copy(level = 35))
|
||||
//}
|
||||
|
||||
class Pokemon (
|
||||
val name: String,
|
||||
val hp: Int
|
||||
) {
|
||||
init {
|
||||
require (name.isNotBlank()) { "Name must not be blank" }
|
||||
require (hp in 1..999) { "HP must be between 1 and 999" }
|
||||
}
|
||||
}
|
||||
|
||||
fun createPokemonOrNull(name: String?, hpRaw: String?): Pokemon? {
|
||||
if (name.isNullOrBlank()) return null
|
||||
val hp = hpRaw?.toIntOrNull() ?: return null
|
||||
return Pokemon(name, hp)
|
||||
}
|
||||
|
||||
var team: MutableList<Pokemon?> = mutableListOf()
|
||||
|
||||
fun main() {
|
||||
do {
|
||||
println("Имя покемона (Enter - закончить ввод):")
|
||||
val name: String = readln()
|
||||
if (name.isBlank()) {
|
||||
break
|
||||
}
|
||||
println("HP покемона:")
|
||||
val hpRaw: String = readln()
|
||||
if (createPokemonOrNull(name, hpRaw) != null) {
|
||||
team.add(createPokemonOrNull(name, hpRaw))
|
||||
println("✅ Добавлен $name (HP: ${hpRaw.toInt()})")
|
||||
} else {
|
||||
println("⚠ Покемон не добавлен: некорректные данные")
|
||||
}
|
||||
} while (name.isNotBlank())
|
||||
|
||||
|
||||
println("========================================\n" +
|
||||
"ОТЧЁТ ПО ОТРЯДУ\n" +
|
||||
"========================================")
|
||||
if (team.isNotEmpty()) {
|
||||
println("Всего покемонов: ${team.size}\n" +
|
||||
"Суммарный HP: ${team.sumOf { it?.hp ?: 0 }}\n" +
|
||||
"Средний HP: ${team.mapNotNull { it?.hp }.average()}\n" +
|
||||
"Самый сильный: ${team.maxByOrNull { it?.hp ?: 0 }?.name}\n" +
|
||||
"Имена: ${team.map { it?.name }.joinToString(", ") }\n" +
|
||||
"========================================")
|
||||
} else {
|
||||
println("Всего покемонов: 0\n" +
|
||||
"Суммарный HP: 0\n" +
|
||||
"Средний HP: 0\n" +
|
||||
"Самый сильный: 0\n" +
|
||||
"Имена: \n" +
|
||||
"========================================")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user