|
@@ -154,10 +154,14 @@ class GlobalBank : Bank {
|
|
|
globalQueue.offerLast(ExchangeSuccessEvent(UUID.randomUUID(), e))
|
|
|
} else {
|
|
|
e.from.accept(coinsExact)
|
|
|
- coinsCashback.map { it as DemoCashbackCoin }.map { it.invert() }.forEach {
|
|
|
- val coinId = emitter.emitOne(it)
|
|
|
- val coin = emitter.extractOne(coinId)
|
|
|
- e.from.accept(listOf(coin))
|
|
|
+ if (coinsCashback.isNotEmpty()) {
|
|
|
+ val invCoinsCashback = coinsCashback.map { it as DemoCashbackCoin }.map { it.invert() }
|
|
|
+ log.info("${e.from} get cashback from emitter ${invCoinsCashback.map { it.current }.joinToString(", ")}")
|
|
|
+ invCoinsCashback.forEach {
|
|
|
+ val coinId = emitter.emitOne(it)
|
|
|
+ val coin = emitter.extractOne(coinId)
|
|
|
+ e.from.accept(listOf(coin))
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
ok
|
|
@@ -469,14 +473,16 @@ open class DemoAccount(val accountId: UUID, personId: UUID) : BankAccount {
|
|
|
|
|
|
override fun accept(coins: MutableCollection<Coin>) {
|
|
|
coins.forEach {
|
|
|
- if (!internalCoins.containsKey(it.id)) {
|
|
|
+ if (it.id != null && !internalCoins.containsKey(it.id)) {
|
|
|
internalCoins.put(it.id, it)
|
|
|
incOverall(it.current)
|
|
|
- } else {
|
|
|
+ } else if (it.id == null) {
|
|
|
+ throw IllegalArgumentException("fake coin is not acceptable")
|
|
|
+ } else if (internalCoins.containsKey(it.id)) {
|
|
|
throw IllegalArgumentException("same coin is not acceptable")
|
|
|
}
|
|
|
}
|
|
|
- log.info("${this} debit coins ${CoinUtils.sum(coins)}")
|
|
|
+ log.info("${this} debit coins ${CoinUtils.sumString(coins)}")
|
|
|
}
|
|
|
|
|
|
override fun extractMoreOrExact(amount: Long): MutableCollection<Coin> {
|
|
@@ -486,12 +492,12 @@ open class DemoAccount(val accountId: UUID, personId: UUID) : BankAccount {
|
|
|
internalCoins.remove(it.id)
|
|
|
decOverall(it.current)
|
|
|
}
|
|
|
- log.info("${this} credit coins ${CoinUtils.sum(coins)}")
|
|
|
+ log.info("${this} credit coins ${CoinUtils.sumString(coins)}")
|
|
|
return coins
|
|
|
}
|
|
|
|
|
|
override fun toString(): String {
|
|
|
- return ZBase32Utils.encode(accountId.toString())
|
|
|
+ return ZBase32Utils.encode(accountId.hashCode().toString(16))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -555,6 +561,12 @@ class CoinUtils {
|
|
|
}
|
|
|
|
|
|
fun sum(coins: Iterable<Coin>): Long = coins.map { it.current }.sum()
|
|
|
+
|
|
|
+ fun sumString(coins: Iterable<Coin>): String {
|
|
|
+ val pos = sum(coins.filter { it.id != null })
|
|
|
+ val neg = sum(coins.filter { it.id == null })
|
|
|
+ return if (neg != 0L) "${pos + neg}(${pos}${neg})" else "$pos"
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|