---
# System prepended metadata

title: Untitled

---

We deal with money at Bynk. How would you model a data structure that handles money?

## Solution 1

```scala
case class Money(value: Double, currencyName: String, symbol: String, code: String)  {
  def +(m2:Money): Money = {
      if (currencyName == m2.currencyName) this.copy(value = value + m2.value)
      else {
          val fxrate = FxRateService.getFXRate(currencyName, m2.currencyName)
          this.copy(value = value + (m2.value *fxrate) )
      }
  }
  
  //You don't have access to this exchange rate service
  def add(m2: Money): Try[Money] = {  //Option => either None , Some(Money)
      if (currencyName!= m2.currencyName) throw new IncompatibleCurrencyException("Not compatible to Add operation")
      else Success(this.copy(value = value + m2.value))
  }
  
}

//100 USD
val m: Money = Money(100, "Dollar", "$", "USD")
val m2: Money = Money(100, "Euro", "€", "EUR")


//What would this return?
addMoney(m, m2)


def addMoney(m1: Money, m2: Money): Money = m1 + m2

```