eranseg

Bank Assignment

Jun 27th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.40 KB | None | 0 0
  1. open class Account constructor(val accountId: Int, val name: String, var balance: Double, val fee: Double, val lineOfCredit: Double, val loanEligibility: Boolean, val accountType: String) {
  2.  
  3.     fun withdrow(amount: Double) {
  4.         if (balance - amount < lineOfCredit * -1) {
  5.                 println("\nCannot withdraw from this account\n")
  6.         } else {
  7.             balance -= amount
  8.             println("\nWithdrawal of $amount from the account. Balance now is $balance\n")
  9.         }
  10.     }
  11.  
  12.     fun deposit(amount: Double) {
  13.         if(amount > 0) {
  14.             balance += amount
  15.             println("\nDeposited $amount to the account. Balance now is $balance\n")
  16.         }
  17.     }
  18.  
  19.     fun getLoan(amount: Double) {
  20.         balance = if (loanEligibility) balance + (amount * (1 - (fee / 100))) else balance
  21.     }
  22.  
  23.     fun printDetails() {
  24.         println("""
  25.            Account owner: $name
  26.            Account Type: $accountType
  27.            Account ID: $accountId
  28.            Account balance: $balance
  29.            Line Of Credit: $lineOfCredit
  30.            Fee: $fee
  31.            Eligible for loan: $loanEligibility
  32.        """.trimIndent())
  33.     }
  34. }
  35.  
  36. class Student (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 0.0, 0.0, true, "Student")
  37. class Business (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 5.8, 100000.0, true, "Business")
  38. class Regular (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 10.2, 10000.0, true, "Regular")
  39. class Soldier (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 0.0, 0.0, false, "Student")
  40.  
  41. class Bank {
  42.  
  43.     companion object {
  44.         private  var accountId = 1
  45.         fun getAccountId(): Int { return accountId++ }
  46.     }
  47.  
  48.     private val accounts = mutableListOf<Account>()
  49.  
  50.     init {
  51.         addAccount("Moshe", 500.0, "Student")
  52.         addAccount("Yossi", 400.0, "Regular")
  53.         addAccount("Haim", 1200.0, "Business")
  54.         addAccount("Yuval", 700.0, "Soldier")
  55.         addAccount("Benni", 550.0, "Student")
  56.         addAccount("Sagit", 460.0, "Regular")
  57.         addAccount("Yael", 1230.0, "Business")
  58.         addAccount("Shira", 780.0, "Soldier")
  59.         addAccount("Bar", 536.0, "Student")
  60.         addAccount("Tali", 454.0, "Regular")
  61.         addAccount("Eli", 1654.0, "Business")
  62.         addAccount("Yafit", 712.0, "Soldier")
  63.         addAccount("Avi", 876.0, "Student")
  64.         addAccount("Noa", 40.0, "Regular")
  65.         addAccount("Ofri", 110.0, "Business")
  66.         addAccount("Hadar", 75.0, "Soldier")
  67.  
  68.     }
  69.  
  70.     private fun addAccount(name: String, balance: Double, type: String) {
  71.         when(type) {
  72.             "Student" -> accounts.add(Student(Bank.getAccountId(),name, balance))
  73.             "Regular" -> accounts.add(Regular(Bank.getAccountId(),name, balance))
  74.             "Business" -> accounts.add(Business(Bank.getAccountId(),name, balance))
  75.             "Soldier" -> accounts.add(Soldier(Bank.getAccountId(),name, balance))
  76.         }
  77.     }
  78.  
  79.     fun getAccountByID(id: Int): Account? {
  80.         for (account in accounts) {
  81.             if (account.accountId == id) { return account}
  82.         }
  83.         return null
  84.     }
  85.  
  86.     fun getAccountByName(name: String): Account? {
  87.         for (account in accounts) {
  88.             if (account.name == name) { return account}
  89.         }
  90.         return null
  91.     }
  92.  
  93.     private fun printDetails(accountsForPrinting: List<Account>) {
  94.         if (accountsForPrinting.isEmpty()) {
  95.             println("\nNo accounts found!\n")
  96.             return
  97.         }
  98.         val accountsIterator = accountsForPrinting.iterator()
  99.         while (accountsIterator.hasNext()) {
  100.             println(accountsIterator.next().printDetails())
  101.             println("\n***************************\n")
  102.         }
  103.     }
  104.  
  105.     fun printAccounts() {
  106.         println("""
  107.            //----------------------------------------------------------
  108.            All accounts
  109.            //----------------------------------------------------------
  110.        """.trimIndent())
  111.         printDetails(accounts)
  112.     }
  113.  
  114.     fun printAccountsInOverdraft() {
  115.         println("""
  116.            //----------------------------------------------------------
  117.            Accounts that have negative balance
  118.            //----------------------------------------------------------
  119.        """.trimIndent())
  120.         printDetails(accounts.filter { it.balance < 0 })
  121.     }
  122.  
  123.     fun printAccountsCloseToLimit() {
  124.         println("""
  125.            //----------------------------------------------------------
  126.            Accounts with balance less then 1000 close to their account limit
  127.            //----------------------------------------------------------
  128.        """.trimIndent())
  129.         printDetails(accounts.filter { it.balance - 1000 <= it.lineOfCredit * -1 })
  130.     }
  131. }
  132.  
  133. fun main(args: Array<String>) {
  134.     var bank = Bank()
  135.     bank.printAccounts()
  136.  
  137.     var ac = bank.getAccountByName("Benni")
  138.     if (ac != null) {
  139.         ac.withdrow(1000.0) // cannot withdraw 1000. student account with 550 balance
  140.         ac.deposit(1200.0)
  141.         ac.withdrow(1000.0)
  142.     }
  143.  
  144.     var bc = bank.getAccountByName("Yael")
  145.     if (bc != null) {
  146.         bc.withdrow(12000.0)
  147.         bc.getLoan(32000.0)
  148.         bc.withdrow(100000.0)
  149.     }
  150.  
  151.     bank.printAccountsInOverdraft()
  152.     bank.printAccountsCloseToLimit()
  153. }
Add Comment
Please, Sign In to add comment