SHARE
TWEET

Kotlin

a guest Feb 22nd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // return currency dengan 2 angka di belakang koma
  3.  
  4. fun convertCurrency(currencyFrom: Double, currencyTo: Double) = String.format("%.2f", currencyFrom * currencyTo).toDouble()
  5.  
  6. // return true jika input dari user berupa angka
  7. // contoh: -13.5 -> true
  8.  
  9. fun isMatches(value: String): Boolean = value.matches("-?\\d+(\\.\\d+)?".toRegex())
  10.  
  11. // return mutable map yang sudah diconvert currency nya
  12.  
  13. fun getConvertedCurrency(currencyFrom: Double, mapOfCurrency: Map<String, Double>): MutableMap<String, Double> {
  14.     val newMap = mutableMapOf<String, Double>()
  15.     mapOfCurrency.forEach { newMap[it.key] = convertCurrency(currencyFrom, it.value) }
  16.     return newMap
  17. }
  18.  
  19. fun main() {
  20.  
  21.     // currency IDR terhadap mata uang lain
  22.     // data diambil dari https://www.xe.com/currency/idr-indonesian-rupiah
  23.  
  24.     val mapOfCurrency = mapOf(
  25.         "USD" to 0.000071,
  26.         "EUR" to 0.000063,
  27.         "Yen" to 0.0079,
  28.         "Yuan" to 0.00048,
  29.         "AUD" to 0.00010
  30.     )
  31.  
  32.     print("Masukkan uang dalam Rupiah\t: ")
  33.     val rupiah = readLine()
  34.  
  35.     // jika input null atau input bukan angka maka tampilkan 'input salah'
  36.     // selain itu dapatkan hasil konversi
  37.  
  38.     if (rupiah == null) {
  39.         println("\nInput Salah!")
  40.     } else {
  41.         if (isMatches(rupiah)) {
  42.             val mapOfConvertedCurrency = getConvertedCurrency(rupiah.toDouble(), mapOfCurrency)
  43.  
  44.             // tampilkan
  45.  
  46.             println("\nHasil Konversi\n")
  47.             mapOfConvertedCurrency.forEach { println("$rupiah IDR\t= ${it.key} ${it.value}") }
  48.  
  49.         } else {
  50.             println("\nInput Salah!")
  51.         }
  52.     }
  53.  
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top