Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. package bank.time
  2.  
  3. import bank._
  4. import java.io
  5. import java.io.{File, FileOutputStream, PrintWriter}
  6.  
  7. class Bank() {
  8.  
  9. var tomBank: Vector[BankAccount] = Vector.empty
  10. var tomHistory: Vector[HistoryEntry] = Vector.empty
  11.  
  12. def allAccounts(): Vector[BankAccount] = {
  13. tomBank.sortBy(_.holder.name)
  14. }
  15.  
  16. def saveString(text: String, fileName: String): Unit = {
  17. if (new java.io.File(fileName).exists) {
  18. val currentFile = new PrintWriter(new FileOutputStream(new File(fileName), true))
  19. currentFile.append(text)
  20. currentFile.close()
  21. }
  22. else {
  23. val file = new io.File(fileName)
  24. val printW = new io.PrintWriter(file, "UTF-8")
  25. try printW.write(text) finally printW.close()
  26. }
  27. }
  28.  
  29. def findByNumber(accountNbr: Int): Option[BankAccount] = {
  30.  
  31. var count = 0
  32. var result: Vector[BankAccount] = Vector[BankAccount]()
  33. for (bankAccount <- tomBank.indices) {
  34. if (tomBank(bankAccount).accountNumber == accountNbr) {
  35. result = result :+ tomBank(bankAccount)
  36. }
  37. else count += 1
  38. }
  39. Option(result(0))
  40.  
  41. }
  42.  
  43. def findAccountsForHolder(id: Long): Vector[BankAccount] = {
  44.  
  45. var count = 0
  46. var result: Vector[BankAccount] = Vector[BankAccount]()
  47. for (bankAccount <- tomBank) {
  48. if (bankAccount.holder.id == id) {
  49. result = result :+ bankAccount
  50. }
  51. else count += 1
  52. }
  53. result
  54. }
  55.  
  56. def findByName(namePattern: String): Vector[Customer] = {
  57. var namn: Vector[Customer] = Vector.empty
  58.  
  59. for (bankAccounts <- tomBank) {
  60. val customers = bankAccounts.holder.name.toLowerCase
  61. if (customers.contains(namePattern.toLowerCase)) {namn = namn :+ bankAccounts.holder}
  62. }
  63. namn
  64. }
  65.  
  66. def doEvent(event: BankEvent): String = {
  67.  
  68. saveString(HistoryEntry(Date.now(), event).toLogFormat + "\n", "minbank.txt")
  69.  
  70. event match {
  71. case Deposit(account, amount) =>
  72. val rensad = tomBank.filter(bankAccount => bankAccount.accountNumber == account)
  73. if (rensad.nonEmpty) {
  74. for (bankAccount <- tomBank) {
  75. if (rensad(0).accountNumber == bankAccount.accountNumber) {
  76. bankAccount.accountBalace += amount
  77. tomHistory = tomHistory :+ HistoryEntry(Date.now(), Deposit(account, amount))
  78. }
  79. }
  80. println(s"Transaktionen lyckades \nNuvarande saldo: ${rensad(0).accountBalace}kr")
  81. Date.now().toNaturalFormat + event.toNaturalFormat
  82. }
  83. else println("Error: Kontonummer finns ej")
  84. "error"
  85.  
  86. case Withdraw(account, amount) =>
  87. val rensad = tomBank.filter(bankAccount => bankAccount.accountNumber == account)
  88.  
  89. if (rensad.nonEmpty) {
  90. for (bankAccount <- tomBank) {
  91. if (rensad(0).accountNumber == bankAccount.accountNumber) {
  92. if (bankAccount.withdraw(amount)) {
  93. bankAccount.accountBalace += -amount
  94. tomHistory = tomHistory :+ HistoryEntry(Date.now(), Withdraw(account, amount))
  95. }
  96. else println("Transaktionen misslyckades. Otillräckligt saldo. ")
  97. }
  98. println(s"Transaktionen lyckades \nNuvarande saldo: ${rensad(0).accountBalace}kr")
  99. Date.now().toNaturalFormat + Withdraw(account, amount).toNaturalFormat
  100. }
  101. }
  102. else println("Error; finns ej ett konto med detta nummer")
  103. "error"
  104.  
  105. case Transfer(accFrom, accTo, amount) =>
  106. val rensadFrom = tomBank.filter(bankAccount => bankAccount.accountNumber == accFrom)
  107. val rensadTo = tomBank.filter(bankAccount => bankAccount.accountNumber == accTo)
  108.  
  109. if (rensadFrom.nonEmpty && rensadTo.nonEmpty) {
  110. for (account <- tomBank) {
  111. if (rensadFrom(0).accountNumber == account.accountNumber) {
  112. if (account.withdraw(amount)) {
  113. account.accountBalace = account.accountBalace - amount
  114. tomHistory = tomHistory :+ HistoryEntry(Date.now(), bank.Transfer(accFrom, accTo, amount))
  115. println(s"\nTransaktionen lyckades \nNuvarande saldo: ${account.accountBalace}kr \n${rensadFrom(0)} ")
  116. Date.now().toNaturalFormat + event.toNaturalFormat
  117. }
  118. else println("Transaktionen misslyckades. Otillräckligt saldo. ")
  119. "error"
  120. }
  121. if (rensadTo(0).accountNumber == account.accountNumber) {
  122. account.accountBalace += amount
  123. println(s"\nNuvarande saldo: ${account.accountBalace}kr \n${rensadTo(0)} \n")
  124. }
  125. }
  126. }
  127. else println("Error; saknas konto med detta nummer")
  128. "error"
  129.  
  130. case NewAccount(id, name) =>
  131. val newBankAccount = new BankAccount(Customer(name, id))
  132.  
  133. tomBank = tomBank :+ newBankAccount
  134. tomHistory = tomHistory :+ HistoryEntry(Date.now(), bank.NewAccount(id, name))
  135. println(s"Nytt konto skapat med kontonummer: \n${newBankAccount.accountNumber}")
  136. event.toNaturalFormat
  137.  
  138. case DeleteAccount(account) =>
  139. tomBank = tomBank.filterNot(bankAccount => bankAccount.accountNumber == account)
  140. Date.now().toNaturalFormat + event.toNaturalFormat
  141. }
  142. }
  143.  
  144. def history(): Vector[HistoryEntry] = scala.io.Source.fromFile("minbank.txt").getLines().toVector.map(HistoryEntry.fromLogFormat)
  145.  
  146. def returnToState(returnDate: Date): String = {
  147.  
  148. val old = history().filter(_.date.compare(returnDate) < 0)
  149. val currentFile = new PrintWriter(new FileOutputStream(new File("minbank.txt"), false))
  150. currentFile.write("")
  151. saveString(old.toString(), "minbank.txt")
  152.  
  153. for (o <- old) {
  154. doEvent(o.event)
  155. }
  156.  
  157. "hej"
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement