-
[Kotlin] Ex(Product, Repairable)Kotlin 2022. 9. 14. 16:26
Product
// Product.kt fun main(){ val tv = Tv() val computer = Computer() val audio = Audio() val buyer = Buyer() buyer.buy(tv) buyer.buy(computer) buyer.buy(audio) buyer.summary() buyer.refund(audio) buyer.summary() } // Product 클래스는 부모클래스 // 멤버변수 prince, bonusPoint를 대표생성자로 생성 // 보조생성자는 price 값만 전달하는 함수로 제작 open class Product constructor(val price:Int, val bousPoint:Int){ constructor( p:Int ):this( p, (p/10).toInt() ) } // 나머지 세 개의 클래스는 모두 Product 클래스로 상속받음 // 각 생성자는 부모클래스의 생성자를 각자의 가격을 전달해서 호출함 // 각자의 상품이름을 리턴하는 toString 메서드를 오버라이딩 class Computer : Product(150){ override fun toString(): String { return "Computer" } } class Tv : Product( 100 ){ override fun toString(): String { return "Tv" } } class Audio : Product( 60 ){ override fun toString(): String { return "Audio" } } // 각각 제품이 Product 를 상속받게 제작 class Buyer{ var money:Int = 1000 var bonusPoint:Int = 0 // 구매목록 리스트 생성 var item = mutableListOf<Product>() // 구입할 제품이 전달되어 물건을 구입하는 동작을 넣은 buy 함수 제작 fun buy( p:Product ){ if( this.money < p.price ){ println("잔액이 부족합니다"); return; } this.money -= p.price this.bonusPoint += p.bousPoint item.add(p) println("$p 을(를) 구입하셨습니다.") } // 구입한 물품과 지출총액이 출력되는 summary 함수 제작 fun summary(){ if( item.isEmpty() ){ println("구입하신 제품이 없습니다") return } var totalPrice:Int = 0 var itemList:String = "" for( p in item ){ // item 에 담긴 객체를 한씩 꺼내어 p에 저장하고 한번씩 반복 실행 p as Product totalPrice += p.price itemList = itemList + " " +p /* if( p is Product ) { totalPrice += p.price itemList = itemList + " " +p } */ } println("지출총액: $totalPrice, 구매목록: $itemList, 잔액: ${this.money}" ) } // 환불 refund 함수 제작 fun refund( p:Product ){ if( item.remove(p) ){ this.money += p.price this.bonusPoint -= p.bousPoint println("$p 을/를 환불했습니다") } else { println("구입하신 물품중에 해당 제품이 없습니다") } } }
Repairablefun main(){ var tank:Tank = Tank() var dropship = DropShip() var marine = Marine() var scv:SCV = SCV() print("$tank : "); tank.prnHp(); print("$dropship : "); dropship.prnHp(); print("$marine : "); marine.prnHp(); print("$scv : "); scv.prnHp(); println() marine.move(23,45); dropship.move(23,45); println() scv.repair(tank) println() scv.repair(dropship) } // Unit 클래스의 hp, maxHp를 대표생성자로 생성 abstract class Unit (val maxHp:Int, var hp:Int ){ constructor( mh:Int ):this(mh, (mh*0.8).toInt() ) fun prnHp(){ println("Max: ${maxHp} - Cur: ${hp}") } abstract fun move(x:Int, y:Int) } open class GroundUnit ( mh:Int ) : Unit( mh, (mh*0.8).toInt() ){ override fun move(x: Int, y: Int) { println("좌표: x:$x, y:$y 로 뛰어 갑니다"); } } open class AirUnit (mh:Int): Unit( mh ){ override fun move(x: Int, y: Int) { println("좌표: x:$x, y:$y 로 날아갑니다"); } } class Tank : GroundUnit(150), Repairable{ override fun toString(): String { return "Tank" } } class DropShip : AirUnit(120), Repairable{ override fun toString(): String { return "Dropship" } } class Marine : GroundUnit(50) { override fun toString(): String { return "Marine" } } class SCV : GroundUnit(40), Repairable { override fun toString(): String { return "SCV" } fun repair( r:Repairable ){ if( r is Unit ){ r.prnHp() if( r.hp < r.maxHp ){ println("$r 의 수리를 시작합니다") while(r.hp < r.maxHp) { r.hp += 2; println("현재 체력 : ${r.hp}" ) } System.out.println("$r 의 수리가 끝났습니다\n"); }else{ println("Can't no Repairable Unit") } } } } interface Repairable
'Kotlin' 카테고리의 다른 글
[Kotlin] NullException (0) 2022.09.14 [Kotlin] DataClass (0) 2022.09.14 [Kotlin] Card Game (0) 2022.09.14 [Kotlin] Set (0) 2022.09.14 [Kotlin] Map (0) 2022.09.14