property ancestor, pCastlibName, pMonies on new me, m1, m2 if voidp(m1) then m1 = VOID -- default if voidp(m2) then m2 = VOID -- default pCastlibName = "MoneySample" ancestor = script("IMoney", pCastlibName).new() pMonies = [] if not(voidp(m1)) and voidp(m2) then me.appendMoneyList(m1) else if not(voidp(m1)) and not(voidp(m2)) then me.appendMoneyPair(m1, m2) end if return me end on appendMoneyList me, moneyList repeat with i = 1 to moneyList.count if not(moneyList[i].isZero()) then me.appendMoney(moneyList[i]) end if end repeat end on appendMoneyPair me, anIMoney1, anIMoney2 anIMoney1.appendSelf(me) anIMoney2.appendSelf(me) end on add me, anIMoney return anIMoney.addMoneyBag(me) end on addMoney me, aMoney return (script("MoneyBag", pCastlibName).new(aMoney, me)).simplify() end on addMoneyBag me, aMoneyBag return (script("MoneyBag", pCastlibName).new(aMoneyBag, me)).simplify() end on appendBag me, aMoneyBag repeat with i = 1 to aMoneyBag.pMonies.count me.appendMoney(aMoneyBag.pMonies[i]) end repeat end on appendMoney me, aMoney anIMoneyOld = me.findMoney(aMoney.currency()) if voidp(anIMoneyOld) then pMonies.append(aMoney) return end if pMonies.deleteOne(anIMoneyOld) anIMoneySum = anIMoneyOld.add(aMoney) if anIMoneySum.isZero() then return pMonies.append(anIMoneySum) end on findMoney me, currency repeat with i = 1 to pMonies.count aMoney = pMonies[i] if aMoney.currency() = currency then return aMoney end if end repeat return VOID end on containsMoney me, aMoney aMoney2 = me.findMoney(aMoney.currency()) return aMoney2.amount() = aMoney.amount() end on hashCode me hash = 0 repeat with i = 1 to pMonies.count aMoney = pMonies[i] hash = bitxor(hash, aMoney.hashCode()) end repeat return hash end on isZero me return (0 = pMonies.count) end on multiply me, factor result = script("MoneyBag", pCastlibName).new() if factor <> 0 then repeat with i = 1 to pMonies.count aMoney = pMonies[i] result.appendMoney(aMoney.multiply(factor)) end repeat end if return result end on negate me result = script("MoneyBag", pCastlibName).new() repeat with i = 1 to pMonies.count aMoney = pMonies[i] result.appendMoney(aMoney.negate()) end repeat return result end on simplify me if pMonies.count = 1 then return pMonies[1] end if return me end on subtract me, anIMoney return me.add(anIMoney.negate()) end on toString me buffer = "" buffer = buffer & "{" repeat with i = 1 to pMonies.count buffer = buffer & pMonies.toString() end repeat buffer = buffer & "}" return buffer end on equals me, anIMoney if voidp(anIMoney) then return FALSE if not(anIMoney.instanceOf(#IMoney)) then return FALSE if me.isZero() then return anIMoney.isZero() end if if not(anIMoney.instanceOf(#MoneyBag)) then return FALSE aMoneyBag = anIMoney if aMoneyBag.pMonies.count <> pMonies.count then return FALSE end if repeat with i = 1 to pMonies.count m = pMonies[i] if not(aMoneyBag.containsMoney(m)) then return FALSE end if end repeat return TRUE end on appendSelf me, aMoneyBag aMoneyBag.appendBag(me) end on instanceOf me, typeSymbol if typeSymbol = #MoneyBag then return TRUE end if return ancestor.instanceOf(typeSymbol) end