ba-st / Aconcagua

Measures as first class objects, that is, an object that encapsulates a number with its unit.
MIT License
14 stars 3 forks source link

Converting MeasureBag to Amount of Unit #24

Open seandenigris opened 3 years ago

seandenigris commented 3 years ago

I tried the following and got aMeasureBag back, but I expected 10.21

amount := 10.21.
dollarAmt := amount // 1.
centsAmt := (amount \\ 1 * 100) asInteger.
total := (AmDollar new * dollarAmt) +
(centsAmt * AmCent new).
total / (1 * AmDollar new).

Also I tried convertTo: but got a subclassResponsibility error...

where:

AmDollar class>>#new
    ^ super new
        initializeBaseUnit: AmCent new
        conversionFactor: 100
        nameForOne: 'dollar'
        nameForMany: 'dollars'
        sign: 'USD'

and

AmCent class>>#new
    ^ super new
        initializeNameFomOne: 'cent'
        nameForMany: 'cents'
        sign: 'c USD'
gcotelli commented 3 years ago

Are AnDollar and AnCent available somewhere to look at them? Are AmDollar new creating a new unit each time or it's the same? If there are different instances probably the conversions will not simplify them because it will consider them as different units... I find a bit strange that you want a new class for every currency...

Maybe a model where you have a more generic class for cents and for currencies makes more sense.. and dollar/cents of dollars will be just instances.. Obviously you will need a place to have these well known instances so will allways get the same one for the dollar