{"id":5041,"date":"2024-03-31T12:33:54","date_gmt":"2024-03-31T12:33:54","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5041"},"modified":"2024-03-31T12:37:54","modified_gmt":"2024-03-31T12:37:54","slug":"simple-banking-system-handling-transactions-with-threads","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/simple-banking-system-handling-transactions-with-threads\/","title":{"rendered":"Simple Banking System handling Transactions with Threads"},"content":{"rendered":"<p><strong class=\"ctaHeeader2\">Banking System<\/strong><\/p>\n<ol>\n<li>We have Bank Account with 2 Fields &#8211; balance and Account Number<\/li>\n<li>We have Transaction class implementing Runnable<\/li>\n<li>We create object for account with some initial balance and try to pass as parameter to runnable Transaction Object<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/03\/BankSys.png\" alt=\"\" \/><\/p>\n<p><strong>BankAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.concurrent.locks.Lock;\r\nimport java.util.concurrent.locks.ReentrantLock;\r\n\r\npublic class BankAccount {\r\n    private Integer balance;\r\n    private Integer accountNumber;\r\n\r\n    private final Lock reLock = new ReentrantLock();\r\n\r\n    public BankAccount(Integer balance, Integer accountNumber){\r\n        this.balance = balance;\r\n        this.accountNumber = accountNumber;\r\n    }\r\n\r\n    public void debitAmount(Integer amount){\r\n        reLock.lock();\r\n\r\n        try{\r\n            balance -= amount;\r\n        }finally {\r\n            reLock.unlock();\r\n        }\r\n\r\n    }\r\n\r\n    public void creditAmount(Integer amount){\r\n        reLock.lock();\r\n\r\n        try{\r\n            balance += amount;\r\n        }finally {\r\n            reLock.unlock();\r\n        }\r\n    }\r\n\r\n    public Integer getAccountNumber(){\r\n        return this.accountNumber;\r\n    }\r\n\r\n    public Integer getBalance(){\r\n        return this.balance;\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><strong>BankTransaction.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class BankTransaction implements Runnable{\r\n    public Integer transAmount;\r\n    public BankAccount bankAccount;\r\n\r\n    public BankTransaction(Integer transAmount, BankAccount bankAccount){\r\n        this.transAmount  = transAmount;\r\n        this.bankAccount  = bankAccount;\r\n    }\r\n\r\n\r\n    @Override\r\n    public void run() {\r\n        if(transAmount &gt;= 0){\r\n            bankAccount.creditAmount(transAmount);\r\n        }else{\r\n            bankAccount.debitAmount(Math.abs(transAmount));\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>BankSystem.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class BankSystem {\r\n    public static void main(String&#x5B;] args) {\r\n        BankAccount objAcc1 = new BankAccount(1000, 101);\r\n        BankAccount objAcc2 = new BankAccount(2000, 102);\r\n\r\n        Thread objThread1 = new Thread(new BankTransaction(50, objAcc1));\r\n        Thread objThread2 = new Thread(new BankTransaction(-150, objAcc2));\r\n        Thread objThread3 = new Thread(new BankTransaction(250, objAcc2));\r\n        Thread objThread4 = new Thread(new BankTransaction(250, objAcc1));\r\n\r\n        objThread1.start();\r\n        objThread2.start();\r\n        objThread3.start();\r\n        objThread4.start();\r\n\r\n        try{\r\n            objThread1.join();\r\n            objThread2.join();\r\n            objThread3.join();\r\n            objThread4.join();\r\n        } catch (InterruptedException e) {\r\n            Thread.currentThread().interrupt();\r\n        }\r\n\r\n        System.out.println(&quot;Final Balance in Account &quot; + objAcc1.getAccountNumber() + &quot; with balance &quot; + objAcc1.getBalance());\r\n        System.out.println(&quot;Final Balance in Account &quot; + objAcc2.getAccountNumber() + &quot; with balance &quot; + objAcc2.getBalance());\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nFinal Balance in Account 101 with balance 1300\r\nFinal Balance in Account 102 with balance 2100\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Banking System We have Bank Account with 2 Fields &#8211; balance and Account Number We have Transaction class implementing Runnable We create object for account with some initial balance and try to pass as parameter to runnable Transaction Object BankAccount.java import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class BankAccount { private Integer balance; private Integer accountNumber; private&hellip; <a href=\"https:\/\/codethataint.com\/blog\/simple-banking-system-handling-transactions-with-threads\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[271],"tags":[],"class_list":["post-5041","post","type-post","status-publish","format-standard","hentry","category-code-examples-threads"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5041"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5041\/revisions"}],"predecessor-version":[{"id":5044,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5041\/revisions\/5044"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}