Advertisement
gvantsakikandze

BankAccount

Jul 21st, 2023
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.04 KB | None | 0 0
  1. class BankAccount {
  2.     private let accountNumber: String
  3.     private var balance: Double
  4.    
  5.     init(balance: Double) {
  6.         self.accountNumber = UUID().uuidString
  7.         self.balance = balance
  8.     }
  9.    
  10.     var balanceChanged : ((Double) -> Void)?
  11.     func deposit(amount: Double) {
  12.         balance += amount
  13.         notifyBalanceChange()
  14.        
  15.     }
  16.     func amountWithdraw(amount: Double) {
  17.         if amount <= balance {
  18.             balance -= amount
  19.             notifyBalanceChange()
  20.            
  21.         }
  22.         else {
  23.             print("There is not enough money !!")
  24.         }
  25.     }
  26.     private func notifyBalanceChange() {
  27.         if let balanceChanged = balanceChanged {
  28.                  balanceChanged(balance)
  29.             print("Balance has changed !!")
  30.              }
  31.     }
  32. }
  33.  
  34.  
  35. let bankAccount = BankAccount(balance: 1000)
  36. bankAccount.balanceChanged = { newBalance in
  37.     print("Balance changed...New balance: \(newBalance)")
  38. }
  39. bankAccount.deposit(amount: 500)
  40. bankAccount.amountWithdraw(amount: 200)
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement