implementation module cashModel /* Pieter Koopman, Radboud University, 2017 - 2018 pieter@cs.ru.nl Advanced programming A simple state model for an automated cash register */ import StdEnv, Data.GenEq class euro a :: a -> Euro instance euro Product where euro Pizza = euro (4,99) euro Beer = euro (0,65) euro _ = euro 1 instance euro Int where euro e = {euro = e, cent = 0} instance euro (Int, Int) where euro (e,c) = {euro = e, cent = c} instance euro [e] | euro e where euro l = sum (map euro l) instance euro Euro where euro e = e instance + Euro where + x y = fromCents (toCents x + toCents y) instance - Euro where - x y = fromCents (toCents x - toCents y) instance zero Euro where zero = {euro = 0, cent = 0} derive gEq Euro, Product instance == Product where (==) p q = p === q instance == Euro where (==) p q = p === q instance ~ Euro where ~ e = {euro = ~e.euro, cent = ~e.cent} toCents :: Euro -> Int toCents e = if (e.euro < 0 || e.cent < 0) (~) id (abs e.euro*100 + abs e.cent) fromCents :: Int -> Euro fromCents c = if (c < 0) (~) id {euro=abs c / 100, cent=abs c rem 100} model :: [Product] Action -> ([Product],[Euro]) model list (Add p) = ([p:list],[euro p]) model list (Rem p) | isMember p list = (removeMember p list,[~ (euro p)]) = (list,[]) model list Pay = ([],[euro list])