aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiederick de Vries <diederick@diederickdevries.net>2019-03-13 09:31:13 +0100
committerDiederick de Vries <diederick@diederickdevries.net>2019-03-13 09:31:13 +0100
commit7ab79e1d631ae0937ff040299ac4ced2262a97c5 (patch)
tree660e025e061f76b344c2d998dbb435074768f8e2
parentb92728eb7ebc91eba574ed469fc10deb4c744139 (diff)
downloadboodschapi-7ab79e1d631ae0937ff040299ac4ced2262a97c5.tar.gz
boodschapi-7ab79e1d631ae0937ff040299ac4ced2262a97c5.tar.bz2
boodschapi-7ab79e1d631ae0937ff040299ac4ced2262a97c5.zip
fix: unit names can contain hyphens
-rw-r--r--src/main/java/net/diederickdevries/boodschapi/util/ItemUtil.java11
-rw-r--r--src/test/java/net/diederickdevries/boodschapi/util/ItemUtilTest.java13
2 files changed, 21 insertions, 3 deletions
diff --git a/src/main/java/net/diederickdevries/boodschapi/util/ItemUtil.java b/src/main/java/net/diederickdevries/boodschapi/util/ItemUtil.java
index 8362b4a..f9978c6 100644
--- a/src/main/java/net/diederickdevries/boodschapi/util/ItemUtil.java
+++ b/src/main/java/net/diederickdevries/boodschapi/util/ItemUtil.java
@@ -18,6 +18,8 @@
18 */ 18 */
19package net.diederickdevries.boodschapi.util; 19package net.diederickdevries.boodschapi.util;
20 20
21import java.util.Arrays;
22import java.util.List;
21import net.diederickdevries.boodschapi.model.Grocery; 23import net.diederickdevries.boodschapi.model.Grocery;
22import net.diederickdevries.boodschapi.model.Item; 24import net.diederickdevries.boodschapi.model.Item;
23 25
@@ -26,7 +28,7 @@ import net.diederickdevries.boodschapi.model.Item;
26 * @author diederick 28 * @author diederick
27 */ 29 */
28public class ItemUtil 30public class ItemUtil
29{ 31{
30 /** 32 /**
31 * Returns an Item from the given input String. When input contains a name, optionally 33 * Returns an Item from the given input String. When input contains a name, optionally
32 * an amount and optionally a unit, parseItem will attempt to figure out what is meant. For 34 * an amount and optionally a unit, parseItem will attempt to figure out what is meant. For
@@ -44,9 +46,12 @@ public class ItemUtil
44 String value = input.trim(); 46 String value = input.trim();
45 char[] chars = value.toCharArray(); 47 char[] chars = value.toCharArray();
46 48
49 List<Character> charsAllowedInNames = Arrays.asList('-', ' ', '\'');
50 List<Character> charsAllowedInAmounts = Arrays.asList(',', '.');
51
47 // Find the name of the grocery 52 // Find the name of the grocery
48 int i = 0; 53 int i = 0;
49 while (i < chars.length && (Character.isAlphabetic(chars[i]) || ' ' == chars[i] || '\'' == chars[i])) 54 while (i < chars.length && (Character.isAlphabetic(chars[i]) || charsAllowedInNames.contains(chars[i])))
50 { 55 {
51 i++; 56 i++;
52 } 57 }
@@ -59,7 +64,7 @@ public class ItemUtil
59 } 64 }
60 65
61 int j = i; 66 int j = i;
62 while (j < chars.length && (Character.isDigit(chars[j]) || ',' == chars[j] || '.' == chars[j])) 67 while (j < chars.length && (Character.isDigit(chars[j]) || charsAllowedInAmounts.contains(chars[j])))
63 { 68 {
64 j++; 69 j++;
65 } 70 }
diff --git a/src/test/java/net/diederickdevries/boodschapi/util/ItemUtilTest.java b/src/test/java/net/diederickdevries/boodschapi/util/ItemUtilTest.java
index 0a207eb..e56e944 100644
--- a/src/test/java/net/diederickdevries/boodschapi/util/ItemUtilTest.java
+++ b/src/test/java/net/diederickdevries/boodschapi/util/ItemUtilTest.java
@@ -81,6 +81,19 @@ public class ItemUtilTest
81 grocery.setUnit("x"); 81 grocery.setUnit("x");
82 grocery.setName("Apostrophe's"); 82 grocery.setName("Apostrophe's");
83 assertEquals(item, ItemUtil.parseItem("Apostrophe's (1x)")); 83 assertEquals(item, ItemUtil.parseItem("Apostrophe's (1x)"));
84 }
85
86 @Test
87 public void testParseWithHyphen()
88 {
89 Grocery grocery = new Grocery();
90 grocery.setUnit("x");
91 grocery.setName("with-hyphen");
92
93 Item item = new Item();
94 item.setGrocery(grocery);
95 item.setAmount(1F);
84 96
97 assertEquals(item, ItemUtil.parseItem("with-hyphen (1x)"));
85 } 98 }
86} 99}