fin-lisp.lisp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. (ql:quickload "cl-store")
  2. ;;;; Reimplementation of my bills tracker in Lisp
  3. (defun reload ()
  4. (load "/Users/swatson/Repos/lisp-files/fin-lisp/fin-lisp.lisp"))
  5. ;;; All records exist in this data structure
  6. ;;; nil on start and loaded in from file
  7. ;;; *records* represents as hash of months,
  8. ;;; where the key is the month stamp, eg 20210701
  9. ;;; and the value is the monthly expenses hash
  10. (defvar *records* (make-hash-table :test 'equalp))
  11. (defun reset-records ()
  12. (setf *records* (make-hash-table :test 'equalp)))
  13. ;; Called like: (add-month '202107)
  14. (defun add-month (month-key)
  15. (setf (gethash month-key *records*) (make-hash-table :test 'equalp))
  16. month-key)
  17. ;;; Taken from practical common lisp
  18. (defun prompt-read (prompt)
  19. (format *query-io* "~a: " prompt)
  20. (force-output *query-io*)
  21. (read-line *query-io*))
  22. (defun prompt-for-expense ()
  23. (list
  24. (prompt-read "Enter expense name")
  25. (parse-integer
  26. (prompt-read "Enter expense value"))))
  27. (defun add-expense-to-month (month)
  28. (if (gethash month *records*)
  29. (let ((innerhash (gethash month *records*))
  30. (exp-l (prompt-for-expense)))
  31. (setf (gethash (first exp-l) innerhash) (second exp-l)))
  32. ;;NIL))
  33. (add-expense-to-month (add-month month))))
  34. ;;; Given key for *records* hash,
  35. ;;; print expenses/values for month
  36. (defun dump-month (month-key)
  37. (format t "~a~C" month-key #\linefeed)
  38. (let ((month-hash)
  39. (exp-keys))
  40. (setf month-hash (gethash month-key *records*))
  41. (setf exp-keys (loop for key being the hash-keys of month-hash collect key))
  42. (dolist (exp-key exp-keys)
  43. (format t "~a : ~a~C" exp-key (gethash exp-key month-hash) #\linefeed))))
  44. ;;; Dump all records.
  45. ;;; This will also be used for data serialization at some point
  46. (defun dump-records ()
  47. (let ((record-key-list (loop for key being the hash-keys of *records* collect key)))
  48. (dolist (month-key record-key-list) (dump-month month-key))))
  49. (defun serialize-records (filename)
  50. (cl-store:store *records* filename))
  51. (defun deserialize-records (filename)
  52. ;(setf *records* (cl-store:restore (pathname filename))))
  53. (setf *records* (cl-store:restore filename)))
  54. ;; Entry point
  55. (defun main ()
  56. (format t "Available options:~C" #\linefeed)
  57. (format t "1. Enter expense~C" #\linefeed)
  58. (format t "2. Display month~C" #\linefeed)
  59. (format t "3. Write records~C" #\linefeed)
  60. (format t "4. Read records~C" #\linefeed)
  61. (format t "5. Quit~C" #\linefeed)
  62. (let
  63. ((answer (prompt-read "Select an option")))
  64. (if (string= answer "1")
  65. (add-expense-to-month
  66. (read-from-string
  67. (prompt-read "Enter month"))))
  68. (if (string= answer "2")
  69. (dump-month
  70. (read-from-string
  71. (prompt-read "Enter month"))))
  72. (if (string= answer "3")
  73. (serialize-records (prompt-read "Enter filename")))
  74. (if (string= answer "4")
  75. (deserialize-records (prompt-read "Enter filename")))
  76. (if (string= answer "5")
  77. (quit)))
  78. (main))