RareUI

A bank test

Nov 16th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.04 KB | None | 0 0
  1.  
  2. open System
  3.  
  4. type Scene = | Welcome
  5.              | Deposit of string
  6.              | Withdraw of string
  7.              | Exit
  8. type Cash = float
  9. type State = State of Scene * Cash
  10.  
  11. type SceneEvent = State -> State
  12.  
  13. let readLine = Console.ReadLine
  14. let readChar () = Console.ReadKey().KeyChar
  15. let printFormat form =
  16.     form
  17.     |> Printf.TextWriterFormat<_>
  18.     |> printfn
  19.  
  20. let (|Float|_|) str =
  21.     match Double.TryParse str with
  22.     | true, x -> Some x
  23.     | false, _ -> None
  24.  
  25. let setSceneMessage message =
  26.     function
  27.     | Deposit _ -> Deposit message
  28.     | Withdraw _ -> Withdraw message
  29.     | x -> x
  30.  
  31. let noEvent x = x
  32. let addCash amount (State (scene, cash)) =
  33.     State (scene, cash+amount)
  34. let setScene scene (State (_, cash)) =
  35.     State (scene, cash)
  36. let setMessage msg (State (scene, cash)) =
  37.     State (setSceneMessage msg scene, cash)
  38.  
  39. let screen text action message cash =
  40.     Console.Clear()
  41.     if message <> ""
  42.     then printFormat (text + "\n" + message) cash
  43.     else printFormat text cash
  44.     action cash
  45.  
  46. let welcomeText = """Welcome! Your account has balance of %.2f.
  47.  (d)eposit   Deposit funds.
  48.  (w)ithdraw  Withdraw funds.
  49.  (q)uit      Quit the program."""
  50.  
  51. let welcomeLogic cash =
  52.     match readChar() with
  53.     | 'd' -> setScene (Deposit "")
  54.     | 'w' -> setScene (Withdraw "")
  55.     | 'q' -> setScene Exit
  56.     | _ -> noEvent
  57.  
  58. let depositText = """Your account has balance of %.2f.
  59. How much would you like to deposit? (0 to cancel)"""
  60.  
  61. let depositNegativeText = "You can't deposit a negative amount!"
  62.  
  63. let depositLogic cash =
  64.     match readLine() with
  65.     | Float amount when amount >= 0.0 ->
  66.         addCash amount >> setScene Welcome
  67.     | Float amount when amount < 0.0 ->
  68.         setMessage depositNegativeText
  69.     | _ -> noEvent
  70.  
  71. let withdrawText = """Your account has balance of %.2f.
  72. How much would you like to withdraw? (0 to cancel)"""
  73.  
  74. let withdrawOverText = "You can't withdraw more than you own!"
  75. let withdrawNegativeText = "You can't withdraw a negative amount!"
  76.  
  77. let withdrawLogic cash =
  78.     match readLine() with
  79.     | Float amount when amount >= 0.0 && amount <= cash ->
  80.         addCash -amount >> setScene Welcome
  81.     | Float amount when amount >= 0.0 && amount > cash ->
  82.         setMessage withdrawOverText
  83.     | Float amount when amount < 0.0 ->
  84.         setMessage withdrawNegativeText
  85.     | _ -> noEvent
  86.  
  87. let welcomeScreen = screen welcomeText welcomeLogic
  88. let depositScreen = screen depositText depositLogic
  89. let withdrawScreen = screen withdrawText withdrawLogic
  90.  
  91. let showScreen (State (scene, cash)) =
  92.     match scene with
  93.     | Welcome -> welcomeScreen "" cash
  94.     | Deposit msg -> depositScreen msg cash
  95.     | Withdraw msg -> withdrawScreen msg cash
  96.     | Exit -> noEvent
  97.  
  98. let applyTo x f = f x
  99.  
  100. let update state =
  101.     state
  102.     |> showScreen
  103.     |> applyTo state
  104.  
  105. let rec updateLoop state =
  106.     match update state with
  107.     | State (Exit, _) -> 0
  108.     | newState -> updateLoop newState
  109.  
  110. [<EntryPoint>]
  111. let main argv =
  112.     State (Welcome, 0.0) |> updateLoop
Add Comment
Please, Sign In to add comment