Skip to content

Commit 24090e4

Browse files
authored
refactor: Refactor monitor patterns example code (iluwatar#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
1 parent becedc1 commit 24090e4

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

monitor/src/main/java/com/iluwatar/monitor/Bank.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,21 @@ public Bank(int accountNum, int baseAmount) {
7272
*
7373
* @param accountA - source account
7474
* @param accountB - destination account
75-
* @param amount - amount to be transferred
75+
* @param amount - amount to be transferred
7676
*/
7777
public synchronized void transfer(int accountA, int accountB, int amount) {
78-
if (accounts[accountA] >= amount) {
78+
if (accounts[accountA] >= amount && accountA != accountB) {
7979
accounts[accountB] += amount;
8080
accounts[accountA] -= amount;
8181
if (LOGGER.isDebugEnabled()) {
8282
LOGGER.debug(
83-
"Transferred from account: {} to account: {} , amount: {} , balance: {}",
83+
"Transferred from account: {} to account: {} , amount: {} , bank balance at: {}, source account balance: {}, destination account balance: {}",
8484
accountA,
8585
accountB,
8686
amount,
87-
getBalance());
87+
getBalance(),
88+
getBalance(accountA),
89+
getBalance(accountB));
8890
}
8991
}
9092
}
@@ -102,6 +104,16 @@ public synchronized int getBalance() {
102104
return balance;
103105
}
104106

107+
/**
108+
* Get the accountNumber balance.
109+
*
110+
* @param accountNumber - accountNumber number
111+
* @return accounts[accountNumber]
112+
*/
113+
public synchronized int getBalance(int accountNumber) {
114+
return accounts[accountNumber];
115+
}
116+
105117
/**
106118
* Get all accounts.
107119
*

monitor/src/main/java/com/iluwatar/monitor/Main.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.concurrent.CountDownLatch;
2929
import java.util.concurrent.Executors;
3030
import lombok.extern.slf4j.Slf4j;
31-
3231
/**
3332
* The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion.
3433
*
@@ -41,6 +40,8 @@
4140
public class Main {
4241

4342
private static final int NUMBER_OF_THREADS = 5;
43+
private static final int BASE_AMOUNT = 1000;
44+
private static final int ACCOUNT_NUM = 4;
4445

4546
/**
4647
* Runner to perform a bunch of transfers and handle exception.
@@ -54,7 +55,7 @@ public static void runner(Bank bank, CountDownLatch latch) {
5455
Thread.sleep(random.nextInt(1000));
5556
LOGGER.info("Start transferring...");
5657
for (int i = 0; i < 1000000; i++) {
57-
bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt());
58+
bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt(0, BASE_AMOUNT));
5859
}
5960
LOGGER.info("Finished transferring.");
6061
latch.countDown();
@@ -70,7 +71,7 @@ public static void runner(Bank bank, CountDownLatch latch) {
7071
* @param args command line args
7172
*/
7273
public static void main(String[] args) throws InterruptedException {
73-
var bank = new Bank(4, 1000);
74+
var bank = new Bank(ACCOUNT_NUM, BASE_AMOUNT);
7475
var latch = new CountDownLatch(NUMBER_OF_THREADS);
7576
var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
7677

monitor/src/test/java/com/iluwatar/monitor/BankTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@ void TransferMethodHaveToTransferAmountFromAnAccountToOtherAccount() {
6969
void BalanceHaveToBeOK() {
7070
assertEquals(4000, bank.getBalance());
7171
}
72+
73+
@Test
74+
void ReturnBalanceWhenGivenAccountNumber() {
75+
bank.transfer(0, 1, 1000);
76+
assertEquals(0, bank.getBalance(0));
77+
assertEquals(2000, bank.getBalance(1));
78+
}
7279
}

0 commit comments

Comments
 (0)