Slide 20
Slide 20 text
class Human {
// …
var bankAccounts: [BankAccount]
// …
}
protocol BankAccountDelegate: class {
func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int
func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws
}
class BankAccount {
let id: Int
private(set) var hashedPassword: Int
private(set) weak var delegate: BankAccountDelegate?
// init(id: Int, encryptedPassword: Int, delegate: BankAccountDelegate) { … }
func changePassword(to newPassword: String) {
do {
guard let delegate = self.delegate else { throw NSError() }
let newHashedPassword = try delegate.account(self, changePasswordTo: newPassword)
self.hashedPassword = newHashedPassword
print("ύεϫʔυมߋޭ")
} catch {
print("ύεϫʔυมߋࣦഊ")
}
}
func transfer(_ amount: Int, to receiver: Int) {
do {
guard let delegate = self.delegate else { throw NSError() }
try delegate.account(self, transfer: amount, to: receiver)
print("ૹۚޭ")
} catch {
print("ૹࣦۚഊ")
}
}
}