This file contains 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
import Foundation | |
protocol Addable { | |
func add(tx: Transaction) -> Double | |
} | |
enum TransactionType { | |
case credit | |
case debit | |
} | |
struct Transaction: Addable { | |
var amount: Double | |
var type: TransactionType | |
private func signedAmount() -> Double { | |
switch type { | |
case .debit: | |
return -amount | |
default: | |
return amount | |
} | |
} | |
func add(tx: Transaction) -> Double { | |
return signedAmount() + tx.signedAmount() | |
} | |
} | |
let deposit = Transaction(amount: 100.00, type: .credit) | |
let withdrawal = Transaction(amount: 50.00, type: .debit) | |
deposit.add(tx: withdrawal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment