Dealing with currency

Report a typo

You have a class BankAccount, which has three methods: addGold(), addSilver(), and addCopper(). All these methods change your balance:

class BankAccount {
    var balance = 0L

    fun addMoney(action: String) {
        when (action) {
            "1" -> addGold()
            "2" -> addSilver()
            else -> addCopper()
        }
    }

    fun addGold() {
        balance += 10000
    }
    fun addSilver() {
        balance += 100
    }
    fun addCopper() {
        balance += 1
    }
}

The program reads the number of threads and creates the threads that will change your balance.

Use block synchronization to ensure the correct work of this program.

Write a program in Kotlin
@SuppressWarnings("MagicNumber")
class BankAccount {
var balance = 0L

fun addMoney(action: String) {
// synchronize adding money with synchronized()
when (action) {
"1" -> ... {
addGold()
}
"2" -> ... {
addSilver()
}
else -> ... {
addCopper()
}
}
}
fun addGold() {
balance += 10000
}
fun addSilver() {
balance += 100
}
fun addCopper() {
balance += 1
}
}
___

Create a free account to access the full topic