Last active
April 6, 2017 10:18
-
-
Save chriswebb09/9246114e6154e0d1b88dd0a9089c26e0 to your computer and use it in GitHub Desktop.
Bank example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Transaction { | |
| case deposit, withdraw | |
| } | |
| class Bank { | |
| typealias Account = (String, Double) | |
| typealias AccountTransaction = (Double, Account) -> Account | |
| static func deposit(amount : Double, account : (name : String, balance : Double)) -> Account { | |
| let newBalance : Double = account.balance + amount | |
| return (account.name, newBalance) | |
| } | |
| static func withdraw(amount : Double, account : (name : String, balance : Double)) -> Account { | |
| let newBalance : Double = account.balance - amount | |
| return (account.name, newBalance) | |
| } | |
| static func chooseTransaction(transaction: Transaction) -> AccountTransaction { | |
| switch transaction { | |
| case .deposit: | |
| return Bank.deposit | |
| case .withdraw: | |
| return Bank.withdraw | |
| } | |
| } | |
| } | |
| class Vault { | |
| static func accessVault(password: String) -> String { | |
| func openVault() -> String { | |
| return "Vault opened" | |
| } | |
| func closeVault() -> String { | |
| return "Vault closed" | |
| } | |
| if password == "secret" { | |
| return openVault() | |
| } else { | |
| return closeVault() | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var account1 = ("State Bank Personal", 1011.10) | |
| var account2 = ("State Bank Business", 24309.63) | |
| Bank.chooseTransaction(transaction: .deposit)(63.17, account1) | |
| Vault.accessVault(password: "secret") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment