소스 검색

передача от банка клиенту с расчётом сдачи

kpmy 5 년 전
부모
커밋
bdaffdae4d
2개의 변경된 파일26개의 추가작업 그리고 9개의 파일을 삭제
  1. 5 0
      pom.xml
  2. 21 9
      src/main/kotlin/in/ocsf/bee/freigeld/core/demo/DemoInMem.kt

+ 5 - 0
pom.xml

@@ -97,6 +97,11 @@
             <version>1.14</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.jboss.aerogear</groupId>
+            <artifactId>aerogear-otp-java</artifactId>
+            <version>1.0.0</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 21 - 9
src/main/kotlin/in/ocsf/bee/freigeld/core/demo/DemoInMem.kt

@@ -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"
+        }
     }
 }