Skip to content

Commit

Permalink
feat: show price in correct currency and decimals (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo-vrijswijk authored Jan 27, 2024
1 parent db9a2c7 commit b066fc2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ case class GetItemsResponse(items: Seq[ItemWrapper]) derives Decoder
case class ItemWrapper(item: Item, store: Store, items_available: Int, display_name: String) derives Decoder

given Show[ItemWrapper] = i =>
show"${i.store.store_name} has ${if i.item.name.isEmpty then "box" else i.item.name} available for ${i.item.price_including_taxes.toEuros}"
show"${i.store.store_name} has ${if i.item.name.isBlank() then "box" else i.item.name} available for ${i.item.item_price}"

case class Item(name: String, price_including_taxes: Price)
case class Price(minor_units: Int):
def toEuros = show"${minor_units / 100}"
case class Item(name: String, item_price: Price)

case class Price(code: String, minor_units: Int, decimals: Int)

given Show[Price] = Show.show: price =>
val currencySymbol = price.code match
case "EUR" => ""
case "USD" => "$"
case "GBP" => "£"
case _ => price.code
show"$currencySymbol ${price.minor_units / math.pow(10, price.decimals)}"

case class Store(store_name: String)

Expand Down
6 changes: 3 additions & 3 deletions project.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//> using scala "3"
//> using dep "org.typelevel::cats-effect::3.5.2"
//> using dep co.fs2::fs2-core::3.9.3
//> using dep co.fs2::fs2-io::3.9.3
//> using dep "org.typelevel::cats-effect::3.5.3"
//> using dep co.fs2::fs2-core::3.9.4
//> using dep co.fs2::fs2-io::3.9.4
//> using dep "com.softwaremill.sttp.client4::circe::4.0.0-M8"
//> using dep "com.softwaremill.sttp.client4::cats::4.0.0-M8"
//> using dep "io.chrisdavenport::rediculous::0.5.1"
Expand Down

0 comments on commit b066fc2

Please sign in to comment.