Skip to content

Commit bb94f49

Browse files
committed
18: ECB Rate Provider not working in JavaMoney Demo Application
Task-Url: http://github.com/JavaMoney/javamoney-examples/issues/issue/18
1 parent 69c7dfa commit bb94f49

File tree

15 files changed

+589
-0
lines changed

15 files changed

+589
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target
2+
/.settings
3+
/.classpath
4+
/.project
5+
/.resourceCache/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>javamoney-examples-console</artifactId>
6+
<groupId>org.javamoney.examples.console</groupId>
7+
<version>1.0.1-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>javamoney-console-bp</artifactId>
10+
<name>JavaMoney Simple Console Examples for Java 7</name>
11+
12+
<properties>
13+
<!-- <exampleMainClass>org.javamoney.examples.console.simple.conversion.ConversionExample</exampleMainClass> -->
14+
<exampleMainClass>org.javamoney.examples.console.simple.core.AmountsDoCalculations</exampleMainClass>
15+
<impl.version>1.1</impl.version>
16+
<jdkVersion>1.7</jdkVersion>
17+
<maven.compile.targetLevel>${jdkVersion}</maven.compile.targetLevel>
18+
<maven.compile.sourceLevel>${jdkVersion}</maven.compile.sourceLevel>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.javamoney</groupId>
24+
<artifactId>moneta-bp</artifactId>
25+
<version>${impl.version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>com.jolira</groupId>
39+
<artifactId>onejar-maven-plugin</artifactId>
40+
<version>1.4.4</version>
41+
<executions>
42+
<execution>
43+
<configuration>
44+
<mainClass>${exampleMainClass}</mainClass>
45+
<attachToBuild>true</attachToBuild>
46+
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
47+
</configuration>
48+
<goals>
49+
<goal>one-jar</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.javamoney.examples.console.simple.core;
2+
3+
import org.javamoney.examples.console.simple.util.ConsoleUtils;
4+
import org.javamoney.moneta.Money;
5+
import org.javamoney.moneta.function.MonetaryFunctions;
6+
import org.javamoney.moneta.function.MonetaryOperators;
7+
import org.javamoney.moneta.function.MonetaryQueries;
8+
9+
import javax.money.Monetary;
10+
import javax.money.MonetaryAmount;
11+
import javax.money.RoundingQueryBuilder;
12+
13+
import java.math.RoundingMode;
14+
15+
/**
16+
* Showcase the usage of extension point mechanism.
17+
*/
18+
public class AmountsUseExtensionPoints {
19+
20+
public static void main(String... args){
21+
22+
MonetaryAmount amt = Money.of(1234.56234, "CHF");
23+
ConsoleUtils.printDetails("Base", amt);
24+
ConsoleUtils.printDetails("10.5 %", amt.with(MonetaryOperators.percent(10.5)));
25+
ConsoleUtils.printDetails("10.5 o/oo", amt.with(MonetaryOperators.permil(10.5)));
26+
ConsoleUtils.printDetails("Major Part", amt.with(MonetaryOperators.majorPart()));
27+
ConsoleUtils.printDetails("Minor Part", amt.with(MonetaryOperators.minorPart()));
28+
ConsoleUtils.printDetails("1/Base (Reciprocal)", amt.with(MonetaryOperators.reciprocal()));
29+
30+
System.out.println("Minor Part as long -> " + amt.query(MonetaryQueries.extractMinorPart()));
31+
System.out.println("Major Part as long -> " + amt.query(MonetaryQueries.extractMajorPart()));
32+
33+
ConsoleUtils.printDetails("Rounded (default)", amt.with(Monetary.getDefaultRounding()));
34+
ConsoleUtils.printDetails("Rounded (DOWN, 1 fraction digit)", amt.with(Monetary.getRounding(
35+
RoundingQueryBuilder.of().set(RoundingMode.DOWN).setScale(1).build()
36+
)));
37+
}
38+
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This example is based on Moneta 1.0.1 (not yet released)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (c) 2012, 2015, Anatole Tresch, Werner Keil and others by the @author tag.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javamoney.examples.console.simple.conversion;
17+
18+
import org.javamoney.moneta.Money;
19+
20+
import javax.money.MonetaryAmount;
21+
import javax.money.convert.ConversionQueryBuilder;
22+
import javax.money.convert.CurrencyConversion;
23+
import javax.money.convert.MonetaryConversions;
24+
25+
import java.text.MessageFormat;
26+
27+
/**
28+
* Showing accessing exchange rates and doing conversions.
29+
*/
30+
public class ConversionExample {
31+
private static final String DEFAULT_TERM_CURRENCY_CODE = "CHF";
32+
33+
public static void main(String... args) {
34+
String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE;
35+
if (args.length > 0) {
36+
termCurrencyCode = args[0];
37+
}
38+
final MonetaryAmount amt = Money.of(2000, "EUR");
39+
CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB");
40+
System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}",
41+
termCurrencyCode, amt.with(conv)));
42+
conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF");
43+
System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}",
44+
termCurrencyCode, amt.with(conv)));
45+
46+
System.out.println(MessageFormat.format(
47+
"2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}",
48+
termCurrencyCode, amt.with(MonetaryConversions
49+
.getConversion(ConversionQueryBuilder.of()
50+
.setTermCurrency(termCurrencyCode)
51+
//.set(LocalDate.of(2015, 01, 05))
52+
.build()))));
53+
}
54+
55+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.javamoney.examples.console.simple.conversion;
2+
3+
import javax.money.convert.ConversionQueryBuilder;
4+
import javax.money.convert.ExchangeRateProvider;
5+
import javax.money.convert.MonetaryConversions;
6+
7+
/**
8+
* Showing accessing exchange rates and doing conversions.
9+
*/
10+
public class ExchangeRateAccess {
11+
12+
public static void main(String... args){
13+
System.out.println("Default conversion chain -> " + MonetaryConversions.getDefaultConversionProviderChain());
14+
15+
ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider("IDENT");
16+
System.out.println("IDENT provider -> " + provider);
17+
provider = MonetaryConversions.getExchangeRateProvider("IDENT", "ECB");
18+
System.out.println("IDENT, ECB provider -> " + provider);
19+
20+
System.out.println(provider.getExchangeRate("CHF", "EUR"));
21+
System.out.println(provider.getExchangeRate("CHF", "CHF"));
22+
23+
provider = MonetaryConversions.getExchangeRateProvider("IDENT", "IMF");
24+
System.out.println(provider.getExchangeRate("TND", "BRL"));
25+
26+
provider = MonetaryConversions.getExchangeRateProvider("IDENT", "ECB", "ECB-HIST");
27+
System.out.println("CHF -> EUR (today) -> " + provider.getExchangeRate(ConversionQueryBuilder.of()
28+
.setBaseCurrency("EUR").setTermCurrency("CHF")
29+
//.set(new Date() LocalDate.of(2008, 1, 8))
30+
.build()));
31+
System.out.println("CHF -> EUR (1.8.2008) -> " + provider.getExchangeRate(ConversionQueryBuilder.of()
32+
.setBaseCurrency("EUR").setTermCurrency("CHF")
33+
//.set(LocalDate.of(2008, 1, 8))
34+
.build()));
35+
}
36+
37+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2012-2014, Werner Keil
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.simple.core;
17+
18+
import org.javamoney.examples.console.simple.util.ConsoleUtils;
19+
import org.javamoney.moneta.FastMoney;
20+
import org.javamoney.moneta.Money;
21+
22+
import javax.money.Monetary;
23+
import javax.money.MonetaryAmount;
24+
import javax.money.MonetaryAmountFactoryQueryBuilder;
25+
26+
27+
/**
28+
* @author Werner Keil
29+
* @version 0.8
30+
*/
31+
public class AmountsDoCalculations {
32+
33+
/**
34+
* @param args
35+
*/
36+
public static void main(String[] args) {
37+
MonetaryAmount amount = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(234).create();
38+
ConsoleUtils.printDetails(amount);
39+
40+
amount = Monetary.getAmountFactory(FastMoney.class).setCurrency("EUR").setNumber(234).create();
41+
ConsoleUtils.printDetails(amount);
42+
43+
amount = Monetary.getAmountFactory(
44+
MonetaryAmountFactoryQueryBuilder.of().setMaxScale(50).setPrecision(30).build())
45+
.setCurrency("EUR").setNumber(234).create();
46+
ConsoleUtils.printDetails(amount);
47+
48+
Money amt1 = Money.of(10.1234556123456789, "USD");
49+
FastMoney amt2 = FastMoney.of(123456789, "USD");
50+
51+
Money total = amt1.add(amt2).multiply(0.5)
52+
.remainder(1);
53+
ConsoleUtils.printDetails(total);
54+
}
55+
56+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2012-2014, Werner Keil
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.simple.core;
17+
18+
import javax.money.Monetary;
19+
20+
/**
21+
* Play around with the current CurrencyUnitProviders available.
22+
*/
23+
public class CurrenciesPlayWithProviders {
24+
25+
/**
26+
* @param args
27+
*/
28+
public static void main(String[] args) {
29+
System.out.println("Known CurrencyProviders: " + Monetary.getCurrencyProviderNames());
30+
System.out.println("CurrencyProviders, default chain: " + Monetary.getDefaultCurrencyProviderChain());
31+
}
32+
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2012-2014, Werner Keil
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.simple.core;
17+
18+
import org.javamoney.examples.console.simple.util.ConsoleUtils;
19+
import org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider;
20+
21+
import javax.money.CurrencyContext;
22+
import javax.money.CurrencyContextBuilder;
23+
import javax.money.CurrencyUnit;
24+
import javax.money.Monetary;
25+
26+
/**
27+
* Programmatically registers a on the fly CurrencyUnit into the current registry.
28+
*/
29+
public class CurrenciesProgrammaticallyRegister {
30+
31+
public static void main(String[] args) {
32+
CurrencyUnit onTheFlyUnit = new CurrencyUnit() {
33+
34+
private CurrencyContext context = CurrencyContextBuilder.of("GeeCon-onTheFly").build();
35+
36+
@Override
37+
public int compareTo(CurrencyUnit o) {
38+
return this.getCurrencyCode().compareTo(o.getCurrencyCode());
39+
}
40+
41+
@Override
42+
public String getCurrencyCode() {
43+
return "GeeCon-Special";
44+
}
45+
46+
@Override
47+
public int getNumericCode() {
48+
return 800;
49+
}
50+
51+
@Override
52+
public int getDefaultFractionDigits() {
53+
return 0;
54+
}
55+
56+
@Override
57+
public CurrencyContext getContext() {
58+
return context;
59+
}
60+
};
61+
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(onTheFlyUnit);
62+
ConsoleUtils.printDetails(Monetary.getCurrency("GeeCon-Special"));
63+
}
64+
65+
66+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.javamoney.examples.console.simple.format;
2+
3+
import org.javamoney.moneta.Money;
4+
import org.javamoney.moneta.format.AmountFormatParams;
5+
6+
import javax.money.MonetaryAmount;
7+
import javax.money.format.AmountFormatQueryBuilder;
8+
import javax.money.format.MonetaryFormats;
9+
import java.util.Locale;
10+
11+
/**
12+
* Created by Anatole on 14.05.2015.
13+
*/
14+
public class FormattingAmounts {
15+
16+
public static void main(String... args) {
17+
MonetaryAmount amt = Money.of(1234.5678, "EUR");
18+
System.out.println(amt.query(MonetaryFormats.getAmountFormat(Locale.GERMANY)));
19+
System.out.println(MonetaryFormats.getAmountFormat(Locale.GERMANY).format(amt));
20+
amt = Money.of(123412341234.5678, "INR");
21+
System.out.println(MonetaryFormats.getAmountFormat(new Locale("", "INR")).format(amt));
22+
23+
// no with adaptive groupings
24+
System.out.println(MonetaryFormats.getAmountFormat(
25+
AmountFormatQueryBuilder.of(new Locale("", "INR"))
26+
.set(AmountFormatParams.GROUPING_SIZES, new int[]{2, 3})
27+
.set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[]{',', '`'})
28+
.build())
29+
.format(amt));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)