Skip to content

Commit 11c0654

Browse files
committed
Add banking implementation
1 parent f620123 commit 11c0654

3 files changed

Lines changed: 111 additions & 3 deletions

File tree

hexagonal/src/main/java/com/iluwatar/hexagonal/banking/WireTransfers.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424

2525
/**
2626
*
27-
* Interface to the lottery service provider's bank account.
27+
* Interface to bank accounts.
2828
*
2929
*/
3030
public interface WireTransfers {
3131

32-
int getCurrentFundsAmount();
33-
boolean transferFunds(int amount, String receiverBankAccountNumber);
32+
void setFunds(String bankAccount, int amount);
33+
int getFunds(String bankAccount);
34+
boolean transferFunds(int amount, String sourceBackAccount, String destinationBankAccount);
3435

3536
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.banking;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
/**
29+
*
30+
* Banking implementation
31+
*
32+
*/
33+
public class WireTransfersImpl implements WireTransfers {
34+
35+
private static Map<String, Integer> accounts = new HashMap<>();
36+
37+
@Override
38+
public void setFunds(String bankAccount, int amount) {
39+
accounts.put(bankAccount, amount);
40+
}
41+
42+
@Override
43+
public int getFunds(String bankAccount) {
44+
return accounts.getOrDefault(bankAccount, 0);
45+
}
46+
47+
@Override
48+
public boolean transferFunds(int amount, String sourceBackAccount, String destinationBankAccount) {
49+
if (accounts.getOrDefault(sourceBackAccount, 0) >= amount) {
50+
accounts.put(sourceBackAccount, accounts.get(sourceBackAccount) - amount);
51+
accounts.put(destinationBankAccount, accounts.get(destinationBankAccount) + amount);
52+
return true;
53+
} else {
54+
return false;
55+
}
56+
}
57+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.banking;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
27+
28+
import org.junit.Test;
29+
30+
/**
31+
*
32+
* Tests for banking
33+
*
34+
*/
35+
public class WireTransfersTest {
36+
37+
private final WireTransfers bank = new WireTransfersImpl();
38+
39+
@Test
40+
public void testInit() {
41+
assertEquals(bank.getFunds("foo"), 0);
42+
bank.setFunds("foo", 100);
43+
assertEquals(bank.getFunds("foo"), 100);
44+
bank.setFunds("bar", 150);
45+
assertEquals(bank.getFunds("bar"), 150);
46+
assertTrue(bank.transferFunds(50, "bar", "foo"));
47+
assertEquals(bank.getFunds("foo"), 150);
48+
assertEquals(bank.getFunds("bar"), 100);
49+
}
50+
}

0 commit comments

Comments
 (0)