fin-lisp.lisp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. ;; Called like: (add-month '202107)
  12. (defun add-month (month-key)
  13. (setf (gethash month-key *records*) (make-hash-table :test 'equalp))
  14. month-key)
  15. ;;; Taken from practical common lisp
  16. (defun prompt-read (prompt)
  17. (format *query-io* "~a: " prompt)
  18. (force-output *query-io*)
  19. (read-line *query-io*))
  20. (defun prompt-for-expense ()
  21. (list
  22. (prompt-read "Enter expense name")
  23. (parse-integer
  24. (prompt-read "Enter expense value"))))
  25. (defun add-expense-to-month (month)
  26. (if (gethash month *records*)
  27. (let ((innerhash (gethash month *records*))
  28. (exp-l (prompt-for-expense)))
  29. (format t "Expense name is: ~a" (first exp-l))
  30. (terpri)
  31. (format t "Expense value is: ~a"(second exp-l))
  32. (terpri)
  33. (setf (gethash (first exp-l) innerhash) (second exp-l)))
  34. ;;NIL))
  35. (add-expense-to-month (add-month month))))
  36. ;;; Given key for *records* hash,
  37. ;;; print expenses/values for month
  38. (defun dump-month (month-key)
  39. (format t "~a~C" month-key #\linefeed)
  40. (let ((month-hash)
  41. (exp-keys))
  42. (setf month-hash (gethash month-key *records*))
  43. (setf exp-keys (loop for key being the hash-keys of month-hash collect key))
  44. (dolist (exp-key exp-keys)
  45. (format t "~a : ~a~C" exp-key (gethash exp-key month-hash) #\linefeed))))
  46. ;;; Dump all records.
  47. ;;; This will also be used for data serialization at some point
  48. (defun dump-records ()
  49. (let ((record-key-list (loop for key being the hash-keys of *records* collect key)))
  50. (dolist (month-key record-key-list) (dump-month month-key))))
  51. (defun serialize-records (filename)
  52. (cl-store:store *records* filename))
  53. (defun deserialize-records (filename)
  54. ;(setf *records* (cl-store:restore (pathname filename))))
  55. (setf *records* (cl-store:restore filename)))
  56. ;; Entry point
  57. (defun main ()
  58. (format t "Available options:")(terpri)
  59. (format t "1. Enter expense")(terpri)
  60. (format t "2. Display month")(terpri)
  61. (format t "3. Write records")(terpri)
  62. (let
  63. ((answer (prompt-read "Select an option")))
  64. answer))