Skip to content

Commit ef532fb

Browse files
committed
CurrencyConverter.java draft
1 parent 46aae6e commit ef532fb

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package currency;
2+
3+
import java.io.IOException;
4+
import java.math.BigDecimal;
5+
import java.math.RoundingMode;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.time.LocalDate;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
13+
14+
/**
15+
* Created by mtumilowicz on 2019-05-15.
16+
* <p>
17+
* TO-DO
18+
*/
19+
public interface CurrencyConverter {
20+
21+
BigDecimal convert(BigDecimal amount);
22+
23+
interface BiFunction {
24+
BigDecimal convert(BigDecimal amount, String toCurrency);
25+
26+
default CurrencyConverter to(String toCurrency) {
27+
return amount -> convert(amount, toCurrency);
28+
}
29+
}
30+
31+
interface TriFunction {
32+
BigDecimal convert(BigDecimal amount, String fromCurrency, String toCurrency);
33+
34+
default BiFunction from(String fromCurrency) {
35+
return (amount, toCurrency) -> convert(amount, fromCurrency, toCurrency);
36+
}
37+
}
38+
39+
static TriFunction of(LocalDate date) {
40+
41+
return (amount, fromCurrency, toCurrency) -> {
42+
43+
Path path = Paths.get("src/main/resources/currency.txt");
44+
try (Stream<String> lines = Files.lines(path)) {
45+
46+
Map<String, BigDecimal> converterMap =
47+
lines.skip(1L)
48+
.collect(
49+
Collectors.toMap(
50+
line -> line.substring(0, 3),
51+
line -> new BigDecimal(line.substring(4))
52+
)
53+
);
54+
55+
return amount.multiply(converterMap.get(toCurrency).divide(converterMap.get(fromCurrency),
56+
RoundingMode.DOWN));
57+
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
}
61+
62+
return null;
63+
};
64+
}
65+
66+
static void main(String[] args) {
67+
System.out.println(CurrencyConverter.of(LocalDate.of(2018, 11, 5))
68+
.from("EUR")
69+
.to("GBP").convert(BigDecimal.valueOf(1000)));
70+
}
71+
}

src/main/resources/currency.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Date=2018/11/05
2+
EUR=1
3+
AUD=1.58125
4+
BRL=4.20735
5+
NOK=9.51682
6+
GBP=0.87749
7+
PLN=4.30044
8+
CAD=1.49181
9+
RUB=75.25596
10+
CNY=7.84552
11+
SAR=4.26496
12+
DKK=7.45445
13+
SGD=1.56431
14+
ZAR=16.2464
15+
HKD=8.89892
16+
SEK=10.3016
17+
HUF=320.75152
18+
CHF=1.1417
19+
INR=82.55699
20+
TWD=34.83463
21+
IDR=16993.81491
22+
THB=37.21937
23+
JPY=128.85457
24+
TRY=6.17017
25+
MXN=22.76093
26+
USD=1.13851

0 commit comments

Comments
 (0)