Skip to content

Commit

Permalink
refactor: Refactor monitor patterns example code (#2560)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bakazhou authored Aug 20, 2023
1 parent becedc1 commit 24090e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
20 changes: 16 additions & 4 deletions monitor/src/main/java/com/iluwatar/monitor/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand All @@ -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.
*
Expand Down
7 changes: 4 additions & 3 deletions monitor/src/main/java/com/iluwatar/monitor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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();
Expand All @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions monitor/src/test/java/com/iluwatar/monitor/BankTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 24090e4

Please sign in to comment.