module coffeemachine

 

// (c) MJP 2007

//

// This is just a demo of a coffeemachine programmed with iTasks combinators

 

import StdEnv, iTasks, iDataTrivial

 

Start world = doHtmlServer (singleUserTask 0 True (foreverTask CoffeeMachine)) world

 

CoffeeMachine :: Task (String,Int)

CoffeeMachine 

=                                   [Txt "Choose product:",Br,Br]

                                    ?>> chooseTask    [("Coffee: 100",    return_V (100,"Coffee"))

                                                      ,("Cappucino: 150", return_V (150,"Cappucino"))

                                                      ,("Tea: 50",        return_V (50, "Tea"))

                                                      ,("Chocolate: 100", return_V (100,"Chocolate"))

                                                      ]

=>> \(toPay,product) ->       [Txt ("Chosen product: " <+++ product),Br,Br]

                                    ?>> getCoins (toPay,0)

=>> \(cancel,returnMoney) ->  let nproduct = if cancel "Cancelled" product in

                                    [Txt ("product = " <+++ nproduct <+++ ", returned money = " <+++ returnMoney),Br,Br]

                                    ?>> buttonTask "Thanks" (return_V (nproduct,returnMoney))

 

getCoins :: (Int,Int) -> Task (Bool,Int)

getCoins (cost,paid) = newTask "getCoins" getCoins`

where

      getCoins`        

=                       [Txt ("To pay: " <+++ cost),Br,Br]

                              ?>> chooseTask [(c +++> " cents", return_V (False,c)) \\ c <- coins]

                              -||-

                              buttonTask "Cancel" (return_V (True,0))

            =>> handleMoney

 

      handleMoney (cancel,coin)

      | cancel          = return_V (cancel,   paid)

      | cost > coin     = getCoins (cost-coin,paid+coin)

      | otherwise       = return_V (cancel,   coin-cost)

 

      coins             = [5,10,20,50,100,200]