Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BankAccount {
- private let accountNumber: String
- private var balance: Double
- init(balance: Double) {
- self.accountNumber = UUID().uuidString
- self.balance = balance
- }
- var balanceChanged : ((Double) -> Void)?
- func deposit(amount: Double) {
- balance += amount
- notifyBalanceChange()
- }
- func amountWithdraw(amount: Double) {
- if amount <= balance {
- balance -= amount
- notifyBalanceChange()
- }
- else {
- print("There is not enough money !!")
- }
- }
- private func notifyBalanceChange() {
- if let balanceChanged = balanceChanged {
- balanceChanged(balance)
- print("Balance has changed !!")
- }
- }
- }
- let bankAccount = BankAccount(balance: 1000)
- bankAccount.balanceChanged = { newBalance in
- print("Balance changed...New balance: \(newBalance)")
- }
- bankAccount.deposit(amount: 500)
- bankAccount.amountWithdraw(amount: 200)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement