diff --git a/lisp/finance/interest-compounder-test.lisp b/lisp/finance/interest-compounder-test.lisp index aad6e27..7c73145 100644 --- a/lisp/finance/interest-compounder-test.lisp +++ b/lisp/finance/interest-compounder-test.lisp @@ -7,74 +7,97 @@ (defun principal-initialized () (setf compounder (interest-compounder 1000 0)) - (assert= 1000 (funcall compounder :get-principal))) + (assert= 1000 (call compounder :get-principal))) (defun interest-rate-initialized () (setf compounder (interest-compounder 0 10)) - (assert= 10 (funcall compounder :get-interest-rate))) + (assert= 10 (call compounder :get-interest-rate))) (defun many-years-with-no-interest-rate () (setf compounder (interest-compounder 1000 0)) - (funcall compounder :move-forward-years 83) - (assert= 1000 (funcall compounder :get-principal))) + (call compounder :move-forward-years 83) + (assert= 1000 (call compounder :get-principal))) (defun no-years-with-positive-interest-rate () (setf compounder (interest-compounder 1000 10)) - (assert= 1000 (funcall compounder :get-principal))) + (assert= 1000 (call compounder :get-principal))) (defun one-year-with-positive-interest-rate () (setf compounder (interest-compounder 100000 5)) - (funcall compounder :move-forward-one-year) - (assert= 105000 (funcall compounder :get-principal))) + (call compounder :move-forward-years 1) + (assert= 105000 (call compounder :get-principal))) (defun two-years-with-positive-interest-rate () (setf compounder (interest-compounder 100000 5)) - (funcall compounder :move-forward-one-year) - (funcall compounder :move-forward-one-year) - (assert= 110250 (funcall compounder :get-principal))) + (call compounder :move-forward-years 2) + (assert= 110250 (call compounder :get-principal))) (defun three-years-with-positive-interest-rate () (setf compounder (interest-compounder 100000 5)) - (funcall compounder :move-forward-years 3) - (assert= 115763 (funcall compounder :get-principal))) + (call compounder :move-forward-years 3) + (assert= 115763 (call compounder :get-principal))) (defun four-years-with-positive-interest-rate () (setf compounder (interest-compounder 100000 5)) - (funcall compounder :move-forward-years 4) - (assert= 121551 (funcall compounder :get-principal))) + (call compounder :move-forward-years 4) + (assert= 121551 (call compounder :get-principal))) (defun one-year-with-negative-interest-rate () (setf compounder (interest-compounder 100000 -5)) - (funcall compounder :move-forward-years 1) - (assert= 95000 (funcall compounder :get-principal))) + (call compounder :move-forward-years 1) + (assert= 95000 (call compounder :get-principal))) (defun two-years-with-negative-interest-rate () (setf compounder (interest-compounder 100000 -5)) - (funcall compounder :move-forward-years 2) - (assert= 90250 (funcall compounder :get-principal))) + (call compounder :move-forward-years 2) + (assert= 90250 (call compounder :get-principal))) (defun three-years-with-negative-interest-rate () (setf compounder (interest-compounder 100000 -5)) - (funcall compounder :move-forward-years 3) - (assert= 85737 (funcall compounder :get-principal))) + (call compounder :move-forward-years 3) + (assert= 85737 (call compounder :get-principal))) (defun four-years-with-negative-interest-rate () (setf compounder (interest-compounder 100000 -5)) - (funcall compounder :move-forward-years 4) - (assert= 81450 (funcall compounder :get-principal))) + (call compounder :move-forward-years 4) + (assert= 81450 (call compounder :get-principal))) (defun negative-number-of-years-does-nothing () (setf compounder (interest-compounder 100000 5)) - (funcall compounder :move-forward-years -4) - (assert= 100000 (funcall compounder :get-principal)) - (funcall compounder :move-forward-years 1) - (funcall compounder :move-forward-years -4) - (assert= 105000 (funcall compounder :get-principal))) + (call compounder :move-forward-years -4) + (assert= 100000 (call compounder :get-principal)) + (call compounder :move-forward-years 1) + (call compounder :move-forward-years -4) + (assert= 105000 (call compounder :get-principal))) (defun zero-number-of-years-does-nothing () (setf compounder (interest-compounder 100000 5)) - (funcall compounder :move-forward-years 0) - (assert= 100000 (funcall compounder :get-principal)) - (funcall compounder :move-forward-years 1) - (funcall compounder :move-forward-years 0) - (assert= 105000 (funcall compounder :get-principal)))))) + (call compounder :move-forward-years 0) + (assert= 100000 (call compounder :get-principal)) + (call compounder :move-forward-years 1) + (call compounder :move-forward-years 0) + (assert= 105000 (call compounder :get-principal))) + + (defun variable-interest-rate () + (setf compounder (interest-compounder 100000 5)) + (call compounder :move-forward-years 2) + (call compounder :set-interest-rate 10) + (call compounder :move-forward-years 2) + (assert= 133403 (call compounder :get-principal))) + + (defun years-are-updated () + (setf compounder (interest-compounder 100000 5)) + (call compounder :move-forward-years 27) + (assert= 27 (call compounder :get-years-passed))) + + (defun negative-number-of-years-does-not-update-years () + (setf compounder (interest-compounder 100000 5)) + (call compounder :move-forward-years 27) + (call compounder :move-forward-years -2) + (assert= 27 (call compounder :get-years-passed))) + + (defun zero-number-of-years-does-not-update-years () + (setf compounder (interest-compounder 100000 5)) + (call compounder :move-forward-years 27) + (call compounder :move-forward-years 0) + (assert= 27 (call compounder :get-years-passed)))))) diff --git a/lisp/finance/interest-compounder.lisp b/lisp/finance/interest-compounder.lisp index 932fb52..d04f82a 100644 --- a/lisp/finance/interest-compounder.lisp +++ b/lisp/finance/interest-compounder.lisp @@ -3,12 +3,17 @@ (defun interest-compounder (initial-principal initial-interest-rate) (let ((private) (public) (principal initial-principal) - (interest-rate initial-interest-rate)) + (interest-rate initial-interest-rate) + (years-passed 0)) (setf private (eval (dlambda + (:add-years (years) + (if (> years 0) + (setf years-passed (+ years-passed years)))) + (:percent-of-number (n percentage) (if (> percentage 0) (/ (+ (* n percentage) 50) 100) @@ -26,14 +31,19 @@ (eval (dlambda + (:get-years-passed () + years-passed) + (:get-principal () principal) (:get-interest-rate () interest-rate) - (:move-forward-one-year () - (call private :compound-interest 1)) + (:set-interest-rate (new-interest-rate) + (setf interest-rate new-interest-rate)) (:move-forward-years (years) - (call private :compound-interest years))))))) + (progn + (call private :add-years years) + (call private :compound-interest years))))))))