From 24090e47d043d4a53017becf1a0e9feff75581e7 Mon Sep 17 00:00:00 2001 From: bakazhou <81795984+bakazhou@users.noreply.github.com> Date: Mon, 21 Aug 2023 02:04:43 +0800 Subject: [PATCH] refactor: Refactor monitor patterns example code (#2560) * feat:adjust the interval of amount because it is not reasonable before and add condition when transfer TianLeZhou 9 minutes ago * feat:add ReturnBalanceWhenGivenAccountNumber test * feat:adjust order of import package --- .../main/java/com/iluwatar/monitor/Bank.java | 20 +++++++++++++++---- .../main/java/com/iluwatar/monitor/Main.java | 7 ++++--- .../java/com/iluwatar/monitor/BankTest.java | 7 +++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/monitor/src/main/java/com/iluwatar/monitor/Bank.java b/monitor/src/main/java/com/iluwatar/monitor/Bank.java index 3f19e0f593a7..76f85b303ab3 100644 --- a/monitor/src/main/java/com/iluwatar/monitor/Bank.java +++ b/monitor/src/main/java/com/iluwatar/monitor/Bank.java @@ -72,19 +72,21 @@ public Bank(int accountNum, int baseAmount) { * * @param accountA - source account * @param accountB - destination account - * @param amount - amount to be transferred + * @param amount - amount to be transferred */ public synchronized void transfer(int accountA, int accountB, int amount) { - if (accounts[accountA] >= amount) { + if (accounts[accountA] >= amount && accountA != accountB) { accounts[accountB] += amount; accounts[accountA] -= amount; if (LOGGER.isDebugEnabled()) { LOGGER.debug( - "Transferred from account: {} to account: {} , amount: {} , balance: {}", + "Transferred from account: {} to account: {} , amount: {} , bank balance at: {}, source account balance: {}, destination account balance: {}", accountA, accountB, amount, - getBalance()); + getBalance(), + getBalance(accountA), + getBalance(accountB)); } } } @@ -102,6 +104,16 @@ public synchronized int getBalance() { return balance; } + /** + * Get the accountNumber balance. + * + * @param accountNumber - accountNumber number + * @return accounts[accountNumber] + */ + public synchronized int getBalance(int accountNumber) { + return accounts[accountNumber]; + } + /** * Get all accounts. * diff --git a/monitor/src/main/java/com/iluwatar/monitor/Main.java b/monitor/src/main/java/com/iluwatar/monitor/Main.java index 923c720d8ef1..7324e6e7ac9b 100644 --- a/monitor/src/main/java/com/iluwatar/monitor/Main.java +++ b/monitor/src/main/java/com/iluwatar/monitor/Main.java @@ -28,7 +28,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import lombok.extern.slf4j.Slf4j; - /** * The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion. * @@ -41,6 +40,8 @@ public class Main { private static final int NUMBER_OF_THREADS = 5; + private static final int BASE_AMOUNT = 1000; + private static final int ACCOUNT_NUM = 4; /** * Runner to perform a bunch of transfers and handle exception. @@ -54,7 +55,7 @@ public static void runner(Bank bank, CountDownLatch latch) { Thread.sleep(random.nextInt(1000)); LOGGER.info("Start transferring..."); for (int i = 0; i < 1000000; i++) { - bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt()); + bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt(0, BASE_AMOUNT)); } LOGGER.info("Finished transferring."); latch.countDown(); @@ -70,7 +71,7 @@ public static void runner(Bank bank, CountDownLatch latch) { * @param args command line args */ public static void main(String[] args) throws InterruptedException { - var bank = new Bank(4, 1000); + var bank = new Bank(ACCOUNT_NUM, BASE_AMOUNT); var latch = new CountDownLatch(NUMBER_OF_THREADS); var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS); diff --git a/monitor/src/test/java/com/iluwatar/monitor/BankTest.java b/monitor/src/test/java/com/iluwatar/monitor/BankTest.java index 75588ad2148d..6d0c671ebe6f 100644 --- a/monitor/src/test/java/com/iluwatar/monitor/BankTest.java +++ b/monitor/src/test/java/com/iluwatar/monitor/BankTest.java @@ -69,4 +69,11 @@ void TransferMethodHaveToTransferAmountFromAnAccountToOtherAccount() { void BalanceHaveToBeOK() { assertEquals(4000, bank.getBalance()); } + + @Test + void ReturnBalanceWhenGivenAccountNumber() { + bank.transfer(0, 1, 1000); + assertEquals(0, bank.getBalance(0)); + assertEquals(2000, bank.getBalance(1)); + } }