Skip to content

how to associate prices with items? #1

@aryairani

Description

@aryairani

I ended up modeling the price as a property of an item:

case class Item(id: String, price: Dollars)    // alternative 1

The disadvantage here is that you need to take steps (out of scope) to make sure you don't end up with a shopping cart that has multiple copies of an item with different prices; e.g. one $1.99 apple and one $2.00 apple. You could end up with a cart full of $1.99 apples and not be able to take advantage of a bundle deal on $2 apples. On this other hand, this behavior might be fine or even desirable.

The alternative is to decouple the price from the item but provide a "pricer" to look up a price for an item. (I think the problem statement hints at going this route.) The disadvantage here is that you need to take steps (out of scope) to make sure you don't end up with a shopping cart that contains items that are unknown to the pricer.

case class Item(id: String)
type TotalPricer = Item => Dollars             // alternative 2
type PartialPricer = NonEmptyMap[Item,Dollars] // alternative 3

Alternative 2 seemed unrealistic, alternative 3 seemed unnecessarily clumsy, and I stuck with alternative 1. In a real-world scenario we could clarify requirements or view the system in a broader context to inform this decision.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions