[백준] Transactions (2975)(kotlin)

문제 설명

백준 2975번 문제 링크

입력 및 출력

» 입력

Input consists of a number of lines, each representing a transaction. Each transaction consists of an integer representing the starting balance (between –200 and +10,000), the letter W or the letter D (Withdrawal or Deposit), followed by a second integer representing the amount to be withdrawn or deposited (between 5 and 400). Input will be terminated by a line containing 0 W 0.

» 출력

Output consists of one line for each line of input showing the new balance for each valid transaction If a withdrawal would take the balance below -200, the output must be the words ‘Not allowed’.

예제 입출력(테스트케이스)

입력 출력
10000 W 10
-200 D 300
50 W 300
0 W 0
9990
100
Not allowed

문제 풀이1

import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array<String>) = with(BufferedReader(InputStreamReader(System.`in`))) {
    var trans = readLine().split(" ")

    while (true) {
        if (trans[0] == "0" && trans[1] == "W" && trans[2] == "0") break

        println(
            when (trans[1]) {
                "W" -> withdraw(trans)
                else -> deposit(trans)
            }
        )

        trans = readLine().split(" ")
    }
}

fun withdraw(trans: List<String>): String {
    val balance = trans[0].toInt()
    val target = trans[2].toInt()

    return if (balance - target < -200) "Not allowed" else "${balance - target}"
}

fun deposit(trans: List<String>): String = "${trans[0].toInt() + trans[2].toInt()}"