From 839573a73de4a87740aecb49f088c75cbe9ee726 Mon Sep 17 00:00:00 2001 From: YaMiN Date: Tue, 25 Jul 2023 00:35:06 +0330 Subject: [PATCH] fixed a bug that thrown exception with certain numbers --- Persian_Numbers/build.gradle | 4 ++-- .../com/github/yamin8000/ppn/PersianDigits.kt | 7 +++--- .../com/github/yamin8000/ppn/MehdiTests.kt | 23 +++++++++++++++++++ build.gradle | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 Persian_Numbers/src/test/java/com/github/yamin8000/ppn/MehdiTests.kt diff --git a/Persian_Numbers/build.gradle b/Persian_Numbers/build.gradle index 7a260ca..02f68ca 100644 --- a/Persian_Numbers/build.gradle +++ b/Persian_Numbers/build.gradle @@ -12,8 +12,8 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 33 - versionCode 4 - versionName "2.0.0" + versionCode 5 + versionName "2.0.1" consumerProguardFiles "consumer-rules.pro" } diff --git a/Persian_Numbers/src/main/java/com/github/yamin8000/ppn/PersianDigits.kt b/Persian_Numbers/src/main/java/com/github/yamin8000/ppn/PersianDigits.kt index 1801ceb..6604904 100644 --- a/Persian_Numbers/src/main/java/com/github/yamin8000/ppn/PersianDigits.kt +++ b/Persian_Numbers/src/main/java/com/github/yamin8000/ppn/PersianDigits.kt @@ -100,16 +100,17 @@ object PersianDigits { } private fun processTwoDigitsNumber(number: String): String { + if (number.length != 2) spellUnknownNumber(number) val oneNotation = "${number[1]}".toLong() val tenNotation = ("${number[0]}".toLong()) * 10 return "${twoDigits[tenNotation]} $AND ${singleDigits[oneNotation]}" } private fun processThreeDigitsNumber(number: String): String { - val oneNotation = "${number[2]}".toLong() - val tenNotation = (("${number[1]}".toLong()) * 10) + oneNotation + if (number.length != 3) spellUnknownNumber(number) + val smallNotations = number.drop(1) val hundredNotation = ("${number[0]}".toLong()) * 100 - return "${threeDigits[hundredNotation]} $AND ${spellTwoDigitsNumber("$tenNotation")}" + return "${threeDigits[hundredNotation]} $AND ${spellUnknownNumber(smallNotations)}" } private fun spellFourDigitsAndMoreNumber(number: String): String { diff --git a/Persian_Numbers/src/test/java/com/github/yamin8000/ppn/MehdiTests.kt b/Persian_Numbers/src/test/java/com/github/yamin8000/ppn/MehdiTests.kt new file mode 100644 index 0000000..0b67b04 --- /dev/null +++ b/Persian_Numbers/src/test/java/com/github/yamin8000/ppn/MehdiTests.kt @@ -0,0 +1,23 @@ +package com.github.yamin8000.ppn + +import com.github.yamin8000.ppn.PersianHelpers.spellToPersian +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class MehdiTests { + + @Test + fun firstMehdiTests() { + assertEquals("یکصد و یک", 101.spellToPersian()) + } + + @Test + fun otherMehdiTests() { + assertEquals("دویست و دو", 202.spellToPersian()) + assertEquals("یک هزار و یکصد و یک", 1101.spellToPersian()) + assertEquals("نود و نه هزار و نهصد و نه", 99_909.spellToPersian()) + assertEquals("پانصد و پنجاه و پنج هزار و پانصد و پنج", 555_505.spellToPersian()) + assertEquals("یک هزار و پنج", 1005.spellToPersian()) + assertEquals("یک هزار و پانصد و پنج", 1505.spellToPersian()) + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index b575ff0..f361433 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:8.0.1' + classpath 'com.android.tools.build:gradle:8.0.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }