Added currency formatting.

This commit is contained in:
Erik C. Thauvin 2021-08-01 23:23:44 -07:00
parent d11f32189e
commit c7961c2817
2 changed files with 20 additions and 14 deletions

View file

@ -31,7 +31,6 @@
*/ */
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.buildCmdSyntax
@ -45,10 +44,12 @@ import org.jdom2.input.SAXBuilder
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
import java.text.NumberFormat import java.text.NumberFormat
import java.util.Currency
import java.util.Locale
import javax.xml.XMLConstants import javax.xml.XMLConstants
/** /**
* The CurrentConverter module. * The CurrencyConverter module.
*/ */
class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) { class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
override fun commandResponse( override fun commandResponse(
@ -87,9 +88,9 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
helpResponse(sender, isPrivate) helpResponse(sender, isPrivate)
} }
} else if (args.contains(CURRENCY_RATES_KEYWORD)) { } else if (args.contains(CURRENCY_RATES_KEYWORD)) {
send(sender, "The currency rates for ${bold(pubDate)} are:", isPrivate) send(sender, "The reference rates for ${bold(pubDate)} are:", isPrivate)
@Suppress("MagicNumber") @Suppress("MagicNumber")
sendList(sender, currencyRates(), 3, isPrivate, isIndent = true) sendList(sender, currencyRates(), 3, " ", isPrivate, isIndent = true)
} else { } else {
helpResponse(sender, isPrivate) helpResponse(sender, isPrivate)
} }
@ -116,7 +117,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
), ),
isPrivate isPrivate
) )
send(sender, "For a listing of current rates:", isPrivate) send(sender, "For a listing of current reference rates:", isPrivate)
send( send(
sender, sender,
helpFormat( helpFormat(
@ -126,7 +127,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
) )
send(sender, "The supported currencies are: ", isPrivate) send(sender, "The supported currencies are: ", isPrivate)
@Suppress("MagicNumber") @Suppress("MagicNumber")
sendList(sender, ArrayList(EXCHANGE_RATES.keys), 11, isPrivate, isIndent = true) sendList(sender, ArrayList(EXCHANGE_RATES.keys), 11, isPrivate = isPrivate, isIndent = true)
} }
} }
return true return true
@ -151,8 +152,11 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
// Last exchange rates table publication date // Last exchange rates table publication date
private var pubDate = "" private var pubDate = ""
private fun Double.formatCurrency(): String = private fun Double.formatCurrency(currency: String): String =
NumberFormat.getCurrencyInstance(Constants.LOCALE).format(this).substring(1) NumberFormat.getCurrencyInstance(Locale.getDefault(Locale.Category.FORMAT)).let {
it.currency = Currency.getInstance(currency)
it.format(this)
}
/** /**
* Converts from a currency to another. * Converts from a currency to another.
@ -173,8 +177,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
val doubleFrom = EXCHANGE_RATES[to]!!.toDouble() val doubleFrom = EXCHANGE_RATES[to]!!.toDouble()
val doubleTo = EXCHANGE_RATES[from]!!.toDouble() val doubleTo = EXCHANGE_RATES[from]!!.toDouble()
PublicMessage( PublicMessage(
amt.formatCurrency() + " ${cmds[1].uppercase()} = " amt.formatCurrency(to) + " = " + (amt * doubleTo / doubleFrom).formatCurrency(from)
+ (amt * doubleTo / doubleFrom).formatCurrency() + " ${cmds[3].uppercase()}"
) )
} catch (e: NumberFormatException) { } catch (e: NumberFormatException) {
ErrorMessage("Let's try with some real numbers next time, okay?") ErrorMessage("Let's try with some real numbers next time, okay?")
@ -192,7 +195,7 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
val rates = mutableListOf<String>() val rates = mutableListOf<String>()
for ((key, value) in EXCHANGE_RATES.toSortedMap()) { for ((key, value) in EXCHANGE_RATES.toSortedMap()) {
@Suppress("MagicNumber") @Suppress("MagicNumber")
rates.add(" $key: ${value.padStart(8)}") rates.add("$key: ${value.padStart(8)}")
} }
return rates return rates
} }

View file

@ -51,11 +51,14 @@ class CurrencyConverterTest {
@Test @Test
fun testConvertCurrency() { fun testConvertCurrency() {
assertThat(convertCurrency("100 USD to EUR").msg) assertThat(convertCurrency("100 USD to EUR").msg)
.describedAs("100 USD to EUR").matches("100\\.00 USD = \\d{2,3}\\.\\d{2} EUR") .describedAs("100 USD to EUR").matches("\\$100\\.00 = \\d{2,3}\\.\\d{2}")
assertThat(convertCurrency("100 USD to USD").msg).describedAs("100 USD to USD") assertThat(convertCurrency("100 USD to USD").msg).describedAs("100 USD to USD")
.contains("You're kidding, right?") .contains("You're kidding, right?")
assertThat(convertCurrency("100 USD").msg).describedAs("100 USD").contains("Invalid query.") assertThat(convertCurrency("100 USD").msg).describedAs("100 USD").contains("Invalid query.")
assertThat(currencyRates().size).describedAs("currencyRates().size() == 33").isEqualTo(33) val rates = currencyRates()
assertThat(currencyRates()).describedAs("currencyRates().get(EUR)").contains(" EUR: 1") assertThat(rates.size).describedAs("currencyRates.size == 33").isEqualTo(33)
assertThat(rates).describedAs("currencyRates(EUR)").contains("EUR: 1")
assertThat(rates.stream().anyMatch { it.matches("USD: .*".toRegex()) })
.describedAs("currencyRates(USD)").isTrue
} }
} }