Re: Questions about CLOS
- From: Kent M Pitman <pitman@xxxxxxxxxxx>
- Date: 28 Jul 2007 23:25:08 -0400
Justin Paston-Cooper <defcon8@xxxxxxxxx> writes:
Hello. I'm trying to think of a way to implement interest rates on
bank accounts. Each time withdraw, credit or is called, I would like
it to be checked whether interest should be added to the account. Or
maybe another solution is better.
The domain description sounds odd to me, since my understanding of
banks is that interest is more typically computed by simply examining
the balance in an account on a given day (or possibly the average
balance over an interval) and then applying an explicit transaction.
Building it into the low-level mechanism is going to have some curious
issues that are independent of the language you code this in.
But basically, you want to do define some class for your account
(defclass account ()
((balance :initform 0 :initarg :initial-balance :accessor balance)))
And then write some methods
(defmethod credit ((account account) amount)
(incf (balance account) amount))
(defmethod debit ((account account) amount)
(decf (balance account) amount))
You could either embed your update-interest operation in those definitions
before the incf or decf, or you might want to write a :before method.
(defmethod credit :before ((account account) amount)
However, the problem I mentioned is that you probably want some
dataflow that remembers when you last updated interest and that knows
how long it's been sitting in the account. I don't know any bank that
posts interest that way, but I guess it's possible. It just wasn't
clear to me how you want to provide that.
It also isn't clear to me that any kind of demand-driven interest rate
calculation can really work unless you also put in checks to make sure
that no one ever goes back in time and injects a credit or debit (which
happens a lot to me as I enter credits/debits in the wrong order in
Quickbooks). If you do, I don't know what you'll use as your interval
against which to compute (or uncompute) interest.
But in any case, I guess you're just asking for notational help.
I assume this is just a toy example. And this should get you started.
As to "enforcement", Lisp doesn't keep someone from violating your
abstraction. You're expected to advertise which functions should be
used, via package exports, but someone who wants to violate your
abstraction is free to do so. Packages are just ways of doing
structured naming in a concise way.
- Prev by Date: Re: CFFI tests problem
- Next by Date: No Vacancy
- Previous by thread: Re: Questions about CLOS
- Next by thread: Re: Questions about CLOS