Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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) {
- fun withdrow(amount: Double) {
- if (balance - amount < lineOfCredit * -1) {
- println("\nCannot withdraw from this account\n")
- } else {
- balance -= amount
- println("\nWithdrawal of $amount from the account. Balance now is $balance\n")
- }
- }
- fun deposit(amount: Double) {
- if(amount > 0) {
- balance += amount
- println("\nDeposited $amount to the account. Balance now is $balance\n")
- }
- }
- fun getLoan(amount: Double) {
- balance = if (loanEligibility) balance + (amount * (1 - (fee / 100))) else balance
- }
- fun printDetails() {
- println("""
- Account owner: $name
- Account Type: $accountType
- Account ID: $accountId
- Account balance: $balance
- Line Of Credit: $lineOfCredit
- Fee: $fee
- Eligible for loan: $loanEligibility
- """.trimIndent())
- }
- }
- class Student (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 0.0, 0.0, true, "Student")
- class Business (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 5.8, 100000.0, true, "Business")
- class Regular (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 10.2, 10000.0, true, "Regular")
- class Soldier (accountId: Int, name: String, balance: Double): Account(accountId, name, balance, 0.0, 0.0, false, "Student")
- class Bank {
- companion object {
- private var accountId = 1
- fun getAccountId(): Int { return accountId++ }
- }
- private val accounts = mutableListOf<Account>()
- init {
- addAccount("Moshe", 500.0, "Student")
- addAccount("Yossi", 400.0, "Regular")
- addAccount("Haim", 1200.0, "Business")
- addAccount("Yuval", 700.0, "Soldier")
- addAccount("Benni", 550.0, "Student")
- addAccount("Sagit", 460.0, "Regular")
- addAccount("Yael", 1230.0, "Business")
- addAccount("Shira", 780.0, "Soldier")
- addAccount("Bar", 536.0, "Student")
- addAccount("Tali", 454.0, "Regular")
- addAccount("Eli", 1654.0, "Business")
- addAccount("Yafit", 712.0, "Soldier")
- addAccount("Avi", 876.0, "Student")
- addAccount("Noa", 40.0, "Regular")
- addAccount("Ofri", 110.0, "Business")
- addAccount("Hadar", 75.0, "Soldier")
- }
- private fun addAccount(name: String, balance: Double, type: String) {
- when(type) {
- "Student" -> accounts.add(Student(Bank.getAccountId(),name, balance))
- "Regular" -> accounts.add(Regular(Bank.getAccountId(),name, balance))
- "Business" -> accounts.add(Business(Bank.getAccountId(),name, balance))
- "Soldier" -> accounts.add(Soldier(Bank.getAccountId(),name, balance))
- }
- }
- fun getAccountByID(id: Int): Account? {
- for (account in accounts) {
- if (account.accountId == id) { return account}
- }
- return null
- }
- fun getAccountByName(name: String): Account? {
- for (account in accounts) {
- if (account.name == name) { return account}
- }
- return null
- }
- private fun printDetails(accountsForPrinting: List<Account>) {
- if (accountsForPrinting.isEmpty()) {
- println("\nNo accounts found!\n")
- return
- }
- val accountsIterator = accountsForPrinting.iterator()
- while (accountsIterator.hasNext()) {
- println(accountsIterator.next().printDetails())
- println("\n***************************\n")
- }
- }
- fun printAccounts() {
- println("""
- //----------------------------------------------------------
- All accounts
- //----------------------------------------------------------
- """.trimIndent())
- printDetails(accounts)
- }
- fun printAccountsInOverdraft() {
- println("""
- //----------------------------------------------------------
- Accounts that have negative balance
- //----------------------------------------------------------
- """.trimIndent())
- printDetails(accounts.filter { it.balance < 0 })
- }
- fun printAccountsCloseToLimit() {
- println("""
- //----------------------------------------------------------
- Accounts with balance less then 1000 close to their account limit
- //----------------------------------------------------------
- """.trimIndent())
- printDetails(accounts.filter { it.balance - 1000 <= it.lineOfCredit * -1 })
- }
- }
- fun main(args: Array<String>) {
- var bank = Bank()
- bank.printAccounts()
- var ac = bank.getAccountByName("Benni")
- if (ac != null) {
- ac.withdrow(1000.0) // cannot withdraw 1000. student account with 550 balance
- ac.deposit(1200.0)
- ac.withdrow(1000.0)
- }
- var bc = bank.getAccountByName("Yael")
- if (bc != null) {
- bc.withdrow(12000.0)
- bc.getLoan(32000.0)
- bc.withdrow(100000.0)
- }
- bank.printAccountsInOverdraft()
- bank.printAccountsCloseToLimit()
- }
Add Comment
Please, Sign In to add comment