Skip to content

Instantly share code, notes, and snippets.

@depy
Created April 10, 2020 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save depy/2bb88dec0e5ff19d8b025ff7eeb13155 to your computer and use it in GitHub Desktop.
Save depy/2bb88dec0e5ff19d8b025ff7eeb13155 to your computer and use it in GitHub Desktop.
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