-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a8e773
commit 207b29f
Showing
7 changed files
with
271 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...Server/src/main/java/de/deadlocker8/budgetmaster/transactions/csvimport/AmountParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.deadlocker8.budgetmaster.transactions.csvimport; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class AmountParser | ||
{ | ||
private static final Pattern PATTERN_AMOUNT = Pattern.compile("^\\s*([-+]?)\\s*(\\d+(,\\d+)?(\\.\\d+)?)"); | ||
|
||
private AmountParser() | ||
{ | ||
} | ||
|
||
public static Optional<Integer> parse(String amountString) | ||
{ | ||
if(amountString == null) | ||
{ | ||
return Optional.empty(); | ||
} | ||
|
||
final Matcher matcher = PATTERN_AMOUNT.matcher(amountString); | ||
boolean matchFound = matcher.find(); | ||
if(matchFound) | ||
{ | ||
final String sign = matcher.group(1); | ||
String amount = matcher.group(2); | ||
amount = amount.replace(',', '.'); | ||
|
||
final String parseableString = MessageFormat.format("{0}{1}", sign, amount); | ||
try | ||
{ | ||
final double parseDouble = Double.parseDouble(parseableString); | ||
return Optional.of((int) (parseDouble * 100)); | ||
} | ||
catch(NumberFormatException e) | ||
{ | ||
return Optional.empty(); | ||
} | ||
} | ||
else | ||
{ | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
...rc/test/java/de/deadlocker8/budgetmaster/unit/transaction/csvimport/AmountParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package de.deadlocker8.budgetmaster.unit.transaction.csvimport; | ||
|
||
import de.deadlocker8.budgetmaster.transactions.csvimport.AmountParser; | ||
import de.deadlocker8.budgetmaster.unit.helpers.LocalizedTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class AmountParserTest | ||
{ | ||
@Test | ||
void test_dot_positive_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("12.03")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_dot_negative_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("-18.41")) | ||
.isPresent() | ||
.get().isEqualTo(-1841); | ||
} | ||
|
||
@Test | ||
void test_dot_negativeWithSpace_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("- 200.30")) | ||
.isPresent() | ||
.get().isEqualTo(-20030); | ||
} | ||
|
||
@Test | ||
void test_dot_positive_currency() | ||
{ | ||
assertThat(AmountParser.parse("12.03 €")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_dot_negative_currency() | ||
{ | ||
assertThat(AmountParser.parse("-18.41€")) | ||
.isPresent() | ||
.get().isEqualTo(-1841); | ||
} | ||
|
||
@Test | ||
void test_dot_positiveWithSign_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("+12.03")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_dot_positiveWithSignWithSpace_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("+ 12.03")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_comma_positive_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("12,03")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_comma_negative_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("-18,41")) | ||
.isPresent() | ||
.get().isEqualTo(-1841); | ||
} | ||
|
||
@Test | ||
void test_comma_negativeWithSpace_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("- 200,30")) | ||
.isPresent() | ||
.get().isEqualTo(-20030); | ||
} | ||
|
||
@Test | ||
void test_comma_positive_currency() | ||
{ | ||
assertThat(AmountParser.parse("12,03 €")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_comma_negative_currency() | ||
{ | ||
assertThat(AmountParser.parse("-18,41€")) | ||
.isPresent() | ||
.get().isEqualTo(-1841); | ||
} | ||
|
||
@Test | ||
void test_comma_positiveWithSign_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("+12,03")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_comma_positiveWithSignWithSpace_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("+12,03")) | ||
.isPresent() | ||
.get().isEqualTo(1203); | ||
} | ||
|
||
@Test | ||
void test_invalid_null() | ||
{ | ||
assertThat(AmountParser.parse(null)) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
void test_invalid_empty() | ||
{ | ||
assertThat(AmountParser.parse("")) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
void test_invalid_empty2() | ||
{ | ||
assertThat(AmountParser.parse(" ")) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
void test_invalid() | ||
{ | ||
assertThat(AmountParser.parse("abc.42€")) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
void test_integer_positive_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("12")) | ||
.isPresent() | ||
.get().isEqualTo(1200); | ||
} | ||
|
||
@Test | ||
void test_integer_negative_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("-18")) | ||
.isPresent() | ||
.get().isEqualTo(-1800); | ||
} | ||
|
||
@Test | ||
void test_integer_negativeWithSpace_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("- 200")) | ||
.isPresent() | ||
.get().isEqualTo(-20000); | ||
} | ||
|
||
@Test | ||
void test_integer_positive_currency() | ||
{ | ||
assertThat(AmountParser.parse("12 €")) | ||
.isPresent() | ||
.get().isEqualTo(1200); | ||
} | ||
|
||
@Test | ||
void test_integer_negative_currency() | ||
{ | ||
assertThat(AmountParser.parse("-18€")) | ||
.isPresent() | ||
.get().isEqualTo(-1800); | ||
} | ||
|
||
@Test | ||
void test_integer_positiveWithSign_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("+12")) | ||
.isPresent() | ||
.get().isEqualTo(1200); | ||
} | ||
|
||
@Test | ||
void test_integer_positiveWithSignWithSpace_noCurrency() | ||
{ | ||
assertThat(AmountParser.parse("+ 12")) | ||
.isPresent() | ||
.get().isEqualTo(1200); | ||
} | ||
} |