From d668c61c3de86158e487f40b3af29655cf3eb29b Mon Sep 17 00:00:00 2001 From: dankito Date: Sun, 13 Mar 2022 22:17:47 +0100 Subject: [PATCH] Using now String as data type for amount --- .../net/codinux/banking/epcqrcode/EpcQrCode.kt | 2 +- .../banking/epcqrcode/EpcQrCodeConfig.kt | 17 +++++++++++++++-- .../banking/epcqrcode/EpcQrCodeCreator.kt | 6 +----- .../codinux/banking/epcqrcode/MainActivity.kt | 6 +----- .../epcqrcode/windows/main/AppSettings.kt | 2 +- .../epcqrcode/windows/main/MainWindow.kt | 8 ++------ .../rest/dto/GenerateEpcQrCodeRequestDto.kt | 2 +- .../EpcQrCodeiOSApp/ContentView.swift | 10 +--------- 8 files changed, 23 insertions(+), 30 deletions(-) diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt index 5bdd966..902316c 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCode.kt @@ -58,7 +58,7 @@ open class EpcQrCode( * Optional amount. * Max. 12 bytes. */ - open val amount: Double?, // TODO: use BigDecimal + open val amount: String?, /** * Optional purpose code. diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt index 88cfe5b..a58fab9 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeConfig.kt @@ -9,7 +9,7 @@ open class EpcQrCodeConfig( var bic: String? = null, - var amount: Double? = null, + amount: String? = null, var reference: String? = null, @@ -17,4 +17,17 @@ open class EpcQrCodeConfig( var encoding: EpcQrCodeCharacterSet = EpcQrCodeCharacterSet.UTF_8 -) \ No newline at end of file +) { + + companion object { + + fun ensureAmountFormat(amount: String?): String? { + return amount?.replace(',', '.') + } + + } + + + open val amount: String? = ensureAmountFormat(amount) + +} \ No newline at end of file diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeCreator.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeCreator.kt index dd28f1b..e04af6c 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeCreator.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeCreator.kt @@ -38,7 +38,7 @@ open class EpcQrCodeCreator { epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.bic)) epcQrCodeBuilder.appendLine(epcQrCode.receiverName) epcQrCodeBuilder.appendLine(epcQrCode.iban) - epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.amount?.let { (epcQrCode.currencyCode ?: EpcQrCode.CurrencyCodeDefaultValue) + convert(it) })) + epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.amount?.let { (epcQrCode.currencyCode ?: EpcQrCode.CurrencyCodeDefaultValue) + it })) // TODO: ensure that amount is converted to the correct format epcQrCodeBuilder.appendLine(epcQrCode.purposeCode) epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.remittanceReference)) @@ -54,10 +54,6 @@ open class EpcQrCodeCreator { } - protected open fun convert(amount: Double): String { - return amount.toString() // TODO: ensure that it's converted to the correct format - } - protected open fun mapNullable(value: String?): String { return value ?: "" } diff --git a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt index 2536cbe..ef38147 100644 --- a/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt +++ b/EpcQrCodeAndroidApp/src/main/java/net/codinux/banking/epcqrcode/MainActivity.kt @@ -20,7 +20,7 @@ class MainActivity : AppCompatActivity() { private fun generateQrCode() { hideKeyboard(edtxtReference) - val param = EpcQrCodeConfig(edtxtReceiver.string, edtxtIban.string, edtxtBic.string, map(edtxtAmount.string), edtxtReference.string) + val param = EpcQrCodeConfig(edtxtReceiver.string, edtxtIban.string, edtxtBic.string, edtxtAmount.string, edtxtReference.string) generateQrCodeAsync(param) { createdQrCode -> imgGeneratedQrCode.setImageBitmap(createdQrCode) @@ -43,8 +43,4 @@ class MainActivity : AppCompatActivity() { } } - private fun map(amount: String): Double? { - return amount.replace(',', '.').toDoubleOrNull() - } - } diff --git a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/AppSettings.kt b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/AppSettings.kt index b145765..a20aa8a 100644 --- a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/AppSettings.kt +++ b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/AppSettings.kt @@ -5,7 +5,7 @@ open class AppSettings( open var receiver: String = "", open var iban: String = "", open var bic: String = "", - open var amount: Double? = null, + open var amount: String? = null, open var reference: String = "", open var destinationFile: String = "" ) \ No newline at end of file diff --git a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt index cab8047..e282702 100644 --- a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt +++ b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt @@ -267,7 +267,7 @@ class MainWindow : View(FX.messages["application.title"]) { } private fun generateQrCode() { - val param = EpcQrCodeConfig(receiver.value, iban.value, bic.value, map(amount.value), reference.value) + val param = EpcQrCodeConfig(receiver.value, iban.value, bic.value, amount.value, reference.value) val qrCodeContent = EpcQrCodeCreator().generateAsString(param) generatedQrCodeImage.value = generateQrCode(qrCodeContent) @@ -277,10 +277,6 @@ class MainWindow : View(FX.messages["application.title"]) { saveSettings() // TODO: actually a side effect } - private fun map(amount: String): Double? { - return DecimalFormat.parse(amount, ParsePosition(0))?.toDouble() - } - private fun generateQrCode(informationToEncode: String): Image? { try { generatedQrCodeBytes = qrCodeGenerator.generateQrCode(informationToEncode) @@ -296,7 +292,7 @@ class MainWindow : View(FX.messages["application.title"]) { private fun saveSettings() { try { - val appSettings = AppSettings(receiver.value, iban.value, bic.value, map(amount.value), reference.value, + val appSettings = AppSettings(receiver.value, iban.value, bic.value, amount.value, reference.value, selectedDestinationFile.value) serializer.serializeObject(appSettings, AppSettingsFile) diff --git a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt index fb73bdd..14f264d 100644 --- a/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt +++ b/EpcQrCodeRest/src/main/kotlin/net/codinux/banking/epcqrcode/rest/dto/GenerateEpcQrCodeRequestDto.kt @@ -12,7 +12,7 @@ class GenerateEpcQrCodeRequestDto { var iban: String = "" - var amount: Double? = null + var amount: String? = null var reference: String? = null diff --git a/EpcQrCodeiOSApp/EpcQrCodeiOSApp/ContentView.swift b/EpcQrCodeiOSApp/EpcQrCodeiOSApp/ContentView.swift index c17d3f6..860dc35 100644 --- a/EpcQrCodeiOSApp/EpcQrCodeiOSApp/ContentView.swift +++ b/EpcQrCodeiOSApp/EpcQrCodeiOSApp/ContentView.swift @@ -82,21 +82,13 @@ struct ContentView: View { private func generateQRCode() { let creator = EpcQrCodeCreator() - let param = EpcQrCodeConfig(receiverName: receiver, iban: iban, bic: bic, amount: map(amount), reference: reference, noteToUser: nil, encoding: .utf8) + let param = EpcQrCodeConfig(receiverName: receiver, iban: iban, bic: bic, amount: amount, reference: reference, noteToUser: nil, encoding: .utf8) let qrCodeContent = creator.generateAsString(param: param) self.generatedQrCode = generateQRCode(qrCodeContent) } - private func map(_ amount: String) -> KotlinDouble? { - if let double = Double(amount.replacingOccurrences(of: ",", with: ".")) { - return KotlinDouble(double: double) - } - - return nil - } - private func generateQRCode(_ informationToEncode: String) -> UIImage? { let writer = ZXQRCodeWriter()