SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun main() {
  2.     parseBanks()
  3. }
  4.  
  5. data class OldBank(
  6.     val id: Int,
  7.     val title: String
  8. )
  9.  
  10. data class NewBank(
  11.     val title: String,
  12.     val min: Int,
  13.     val max: Int
  14. )
  15.  
  16. data class Bank(
  17.     val id: Int,
  18.     val title: String,
  19.     val min: Int,
  20.     val max: Int
  21. )
  22.  
  23. fun parseBanks() {
  24.     val banksWithIds = parseBanksWithIds()
  25.     val banksWithLengths = parseBanksWithLengths()
  26.  
  27.     val banks = concatBankLists(banksWithIds, banksWithLengths)
  28.     val bankList = createStringFromBanks(banks)
  29.  
  30.     print(bankList)
  31. }
  32.  
  33. fun parseBanksWithIds(): List<OldBank> {
  34.     val banksWithId = Banks.banksWithId.replace("),", ");")
  35.  
  36.     val stringOldBankList = banksWithId.split(';')
  37.     val oldBanks = ArrayList<OldBank>()
  38.  
  39.     stringOldBankList.forEach { bank ->
  40.         val trimmedBank = bank.trim()
  41.         val indexOfComma = trimmedBank.indexOf(',')
  42.         val indexOfOpenBrace = trimmedBank.indexOf('(')
  43.         val indexOfFirstQuote = trimmedBank.indexOf('"')
  44.         val indexOfLastQuote = trimmedBank.lastIndexOf('"')
  45.  
  46.         val id = try {
  47.             trimmedBank.substring(indexOfOpenBrace + 1, indexOfComma).toInt()
  48.         } catch (e: StringIndexOutOfBoundsException) {
  49.             0
  50.         }
  51.         val title = try {
  52.             trimmedBank.substring(indexOfFirstQuote + 1, indexOfLastQuote)
  53.         } catch (e: StringIndexOutOfBoundsException) {
  54.             ""
  55.         }
  56.  
  57.         val oldBank = OldBank(id, title)
  58.         oldBanks.add(oldBank)
  59.     }
  60.  
  61.     return oldBanks
  62. }
  63.  
  64. fun parseBanksWithLengths(): List<NewBank> {
  65.     val banksWithMinAndMaxLength = Banks.banksWithMinAndMaxLength.replace("],", "];")
  66.  
  67.     val stringNewBankList = banksWithMinAndMaxLength.split(';')
  68.     val newBanks = ArrayList<NewBank>()
  69.  
  70.     stringNewBankList.forEach { bank ->
  71.         val trimmedBank = bank.trim()
  72.         val indexOfFirstQuote = trimmedBank.indexOf('"')
  73.         val indexOfLastQuote = trimmedBank.lastIndexOf('"')
  74.         val indexOfFirstComma = trimmedBank.indexOf(',')
  75.         val indexOfLastComma = trimmedBank.lastIndexOf(',')
  76.         val indexOfClosedBrace = trimmedBank.lastIndexOf(']')
  77.  
  78.         val title = try {
  79.             trimmedBank.substring(indexOfFirstQuote + 1, indexOfLastQuote)
  80.         } catch (e: StringIndexOutOfBoundsException) {
  81.             ""
  82.         }
  83.  
  84.         val min = try {
  85.             trimmedBank.substring(indexOfFirstComma + 2, indexOfLastComma).toInt()
  86.         } catch (e: StringIndexOutOfBoundsException) {
  87.             0
  88.         }
  89.  
  90.         val max = try {
  91.             trimmedBank.substring(indexOfLastComma + 2, indexOfClosedBrace).toInt()
  92.         } catch (e: StringIndexOutOfBoundsException) {
  93.             0
  94.         }
  95.  
  96.         val newBank = NewBank(title, min, max)
  97.         newBanks.add(newBank)
  98.     }
  99.  
  100.     return newBanks
  101. }
  102.  
  103. fun concatBankLists(oldBanks: List<OldBank>, newBanks: List<NewBank>): List<Bank> {
  104.     val banks = ArrayList<Bank>()
  105.     oldBanks.forEach { oldBank ->
  106.         val newBank = getBankByTitle(oldBank.title, newBanks)
  107.         val bank = Bank(oldBank.id, oldBank.title, newBank?.min!!, newBank.max)
  108.         banks.add(bank)
  109.     }
  110.     return banks
  111. }
  112.  
  113. fun getBankByTitle(title: String, banks: List<NewBank>): NewBank? {
  114.     banks.forEach { bank ->
  115.         if (bank.title == title) {
  116.             return bank
  117.         }
  118.     }
  119.     return null
  120. }
  121.  
  122. fun createStringFromBanks(banks: List<Bank>): String {
  123.     val stringBuilder = StringBuilder()
  124.     banks.forEach { bank ->
  125.         stringBuilder
  126.             .append("Bank(")
  127.             .append("${bank.id}")
  128.             .append(", ")
  129.             .append('"')
  130.             .append(bank.title)
  131.             .append('"')
  132.             .append(", ")
  133.             .append("${bank.min}")
  134.             .append(", ")
  135.             .append("${bank.max}")
  136.             .append("),\n")
  137.     }
  138.     return stringBuilder.toString().trim()
  139. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top