Re: Questions about CLOS



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)
(update-interest account))

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.
.



Relevant Pages

  • fact or fiction
    ... Hamid invested too in American bank, I thought he was anti US, so how is ... The information regarding the Fixed Deposit Accounts of Mahathir¡ös ... That vast amount of monies could have been derived through the ... Type of Account: Fixed Deposit ...
    (soc.culture.malaysia)
  • Re: fact or fiction
    ... THE ACCOUNT HAD BEEN CLROSS VERIFIED BY MY FRIEND IN MOSSAD ... Smart move ie to invest in Israel bank so that nobody suspects.......Syed Hamid invested too in American bank, I thought he was anti US, so how is going to explain on his connection with the US and Jews ... ... That vast amount of monies could have been derived through the following: ... Date of Maturity: 25/06/98 ...
    (soc.culture.malaysia)
  • Re: Aging Function
    ... bottom of your activity statement to show the AgeBuckets for each account. ... DebitID, and Amount. ... I would like to subtract the total credit ... > transactions starting with the oldest debit transactions, ...
    (microsoft.public.access.modulesdaovba)
  • Problem with credit card company
    ... receipt from the bank for this. ... On Friday last my card got declined at the supermarket. ... I checked my account online and discovered my payment to the credit ...
    (uk.legal)
  • Re: Desperate for business bank account
    ... I did a test online credit check. ... in the country so I get rated worse than if I did have CCJs. ... Another option is a Swiss bank account. ...
    (uk.consultants)