Small example of a banking system written by clojure

Source: Internet
Author: User
; Bank (defstruct account-struct: ID: Owner: Balance-ref); account dimension (DEF account-Map-ref (sorted-map ))); save the collection of all accounts; create an account; @ ARGs: account ID; @ return: Account Information (defn open-Account "creates a new account, stores it in the account map and returns it "[owner] (dosync; required because a ref is being changed (let [Account-map @ account-Map-ref last-entry (last account-map) ID (if last-entry (INC (key last-entry) 1); if the last If an account exists, it is automatically added; otherwise, it is 1 account (struct account-struct ID owner (ref 0: validator integer?)]; Create an account Account-struct force ref digit type (alter account-Map-ref assoc ID account); update the newly created account-struct account to account-Map-ref account ); return to this account); save money; ARGs: Account Information amount; Return: nil (defn deposit [account amount] (dosync (thread/sleep 50 ); simulate a long-running operation (let [owner (account: owner); account balance-ref (account: Balance-ref); amount type (if (POS? Amount) "deposit" "Withdraw"); greater than 0 or less than 0 ction (if (POS? Amount) "to" "from") ABS-amount (Math/ABS amount)] (if (> = (+ @ balance-ref amount) 0 ); balance (do (alter balance-ref + amount); Modify balance cannot use commute (println (STR type "ing") ABS-amount direction owner )); print Information (throw (illegalargumentexception .; if the balance is insufficient, send a prompt and roll back the transaction (Str "insufficient balance for" owner "to withdraw" Abs-amount); get the money; ARGs: Account Information amount; return: nil (defn withdraw "removes money from an account" [account amount] (Deposit Account (-amount); Transfer; ARGs: The amount of the target account of the source account; return: nil (defn transfer [from-account to-account amount] (dosync (println "transferring" amount "from" (from-account: owner) "to" (to-account: owner) (withdraw from-account amount) (deposit to-account amount); Queries private functions of a single account; ARGs: account; return: nil (defn-report-pri-1 "prints information about a single account" [account] (let [balance-ref (account: Balance-ref)] (println "balance for" (account: owner) "is" @ balance-ref); Queries private functions of multiple accounts; ARGs: account list; return: nil (defn Report "prints information about any number of accounts" [accounts] (dosync (doseq [Account accounts] (report-pri-1 account); handle exceptions not handled by the current thread; ARGs: The current thread is abnormal; Return: nil (thread/setdefaultuncaughtexceptionhandler (proxy [thread $ uncaughtexceptionhandler] [] (uncaughtexception [thread throwable]; just print the message in the exception. (println (.. throwable. getcause. getmessage) "thread exception"); Call (let [Mark (open-Account "mark") Tami (open-Account "Tami") thread (thread. # (Transfer mark Tami 50)] (try (deposit mark 100); save 100 (deposit Tami 200); save 200 (. start thread); Transfer 50 (. join thread); wait for the sub-thread to exit; (withdraw mark 75); Take 75 (report [Mark Tami]); query (catch illegalargumentexception E (println (. getmessage E) "in main thread "))))

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.