Skip to content

Instantly share code, notes, and snippets.

Created April 10, 2020 08:28
What would you like to do?
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
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