Skip to content

Commit 1529c7b

Browse files
authored
[BAEL-8493] by sgrverma23 - Changes for conversation of currency code to symbol in java (#18427)
* BAEL-8493:sgrverma23 - Changes for coversation of currency code to symbol in java * fixing release version * refactoring test methods and version number * moving to currency package from default
1 parent f324b4d commit 1529c7b

File tree

9 files changed

+131
-0
lines changed

9 files changed

+131
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#core-java-currency
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-currency</artifactId>
7+
<packaging>jar</packaging>
8+
<name>core-java-currency</name>
9+
10+
<parent>
11+
<groupId>com.baeldung.core-java-modules</groupId>
12+
<artifactId>core-java-modules</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.projectlombok</groupId>
19+
<artifactId>lombok</artifactId>
20+
<version>${lombok.version}</version>
21+
</dependency>
22+
</dependencies>
23+
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-surefire-plugin</artifactId>
29+
<version>${maven-surefire-plugin.version}</version>
30+
<configuration>
31+
<argLine>--enable-preview</argLine>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
37+
<properties>
38+
<maven.compiler.source.version>17</maven.compiler.source.version>
39+
<maven.compiler.target.version>17</maven.compiler.target.version>
40+
<lombok.version>1.18.24</lombok.version>
41+
</properties>
42+
43+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.currency.utils;
2+
3+
import java.util.Currency;
4+
import java.util.Locale;
5+
6+
public class CurrencyLocaleUtil {
7+
public String getSymbolForLocale(Locale locale) {
8+
Currency currency = Currency.getInstance(locale);
9+
return currency.getSymbol();
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.currency.utils;
2+
3+
import java.util.Map;
4+
5+
public class CurrencyMapUtil {
6+
private static final Map<String, String> currencymap = Map.of(
7+
"USD", "$",
8+
"EUR", "€",
9+
"INR", "₹"
10+
);
11+
12+
public static String getSymbol(String currencyCode) {
13+
return currencymap.getOrDefault(currencyCode, "Unknown");
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.currency.utils;
2+
3+
import java.util.Currency;
4+
5+
public class CurrencyUtil {
6+
public static String getSymbol(String currencyCode) {
7+
Currency currency = Currency.getInstance(currencyCode);
8+
return currency.getSymbol();
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.currency.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
import java.util.Locale;
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class CurrencyLocaleUtilTest {
8+
private final CurrencyLocaleUtil currencyLocale = new CurrencyLocaleUtil();
9+
10+
@Test
11+
void givenLocale_whenGetSymbolForLocale_thenReturnsLocalizedSymbol() {
12+
assertEquals("$", currencyLocale.getSymbolForLocale(Locale.US));
13+
assertEquals("€", currencyLocale.getSymbolForLocale(Locale.FRANCE));
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.currency.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
class CurrencyMapUtilTest {
7+
@Test
8+
void givenValidCurrencyCode_whenGetSymbol_thenReturnsCorrectSymbol() {
9+
assertEquals("$", CurrencyMapUtil.getSymbol("USD"));
10+
assertEquals("€", CurrencyMapUtil.getSymbol("EUR"));
11+
assertEquals("₹", CurrencyMapUtil.getSymbol("INR"));
12+
}
13+
14+
@Test
15+
void givenInvalidCurrencyCode_whenGetSymbol_thenReturnsUnknown() {
16+
assertEquals("Unknown", CurrencyMapUtil.getSymbol("XYZ"));
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.currency.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
class CurrencyUtilTest {
7+
@Test
8+
void givenValidCurrencyCode_whenGetSymbol_thenReturnsCorrectSymbol() {
9+
assertEquals("$", CurrencyUtil.getSymbol("USD"));
10+
assertEquals("€", CurrencyUtil.getSymbol("EUR"));
11+
}
12+
13+
@Test
14+
void givenInvalidCurrencyCode_whenGetSymbol_thenThrowsException() {
15+
assertThrows(IllegalArgumentException.class, () -> CurrencyUtil.getSymbol("INVALID"));
16+
}
17+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
<module>core-java-concurrency-collections</module>
125125
<module>core-java-concurrency-collections-2</module>
126126
<module>core-java-console</module>
127+
<module>core-java-currency</module>
127128
<module>core-java-datetime-string-2</module>
128129
<module>core-java-date-operations</module>
129130
<module>core-java-date-operations-2</module>

0 commit comments

Comments
 (0)