Browse Source

almost there, just need to clean up an extra space in the display

Luis Romero 6 years ago
parent
commit
ae1ace36c8

+ 16
- 3
src/main/java/io/zipcoder/ItemParser.java View File

@@ -93,11 +93,24 @@ public class ItemParser {
93 93
     }
94 94
 
95 95
     public String itemNameAndCountAsString(String name) {
96
+        String timeOrTimes = "times";
96 97
         StringBuilder sb = new StringBuilder();
97
-        for (Entry entry : itemOrganizer.entrySet()) {
98
-            sb.append("name: " + entry.getKey() + " " + "seen: " + itemOrganizer.get(entry.getKey()).size() + "\n");
98
+        for (String keyName : itemOrganizer.keySet()) {
99
+            if (itemOrganizer.get(keyName).size() == 1) {
100
+                timeOrTimes = "time";
101
+            }
102
+            if (keyName.equals(name)) {
103
+                sb.append("name:   " + capitalizeFirstLetterOnly(name));
104
+                sb.append("\t\tseen: " + itemOrganizer.get(name).size() + " " + timeOrTimes);
105
+            }
99 106
         }
100
-        sb.deleteCharAt(sb.length() - 1);
107
+        return sb.toString();
108
+    }
109
+
110
+    public String capitalizeFirstLetterOnly(String word) {
111
+        StringBuilder sb = new StringBuilder();
112
+        String firstLetterOfWord = word.substring(0,1);
113
+        sb.append(firstLetterOfWord.toUpperCase() + word.substring(1, word.length()));
101 114
         return sb.toString();
102 115
     }
103 116
 

+ 8
- 7
src/main/java/io/zipcoder/Main.java View File

@@ -21,13 +21,14 @@ public class Main {
21 21
         itemParser.createItems();
22 22
         itemParser.addNameAndItemsOfSameNameToItemOrganizer();
23 23
 
24
-//        System.out.println(itemParser.itemsAsString());
25
-        System.out.println(itemParser.itemOrganizerAsString());
26
-
27
-//        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("milk")));
28
-//        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("bread")));
29
-//        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("cookies")));
30
-//        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("apples")));
24
+        System.out.println(itemParser.itemNameAndCountAsString("milk"));
25
+        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("milk")));
26
+        System.out.println(itemParser.itemNameAndCountAsString("bread"));
27
+        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("bread")));
28
+        System.out.println(itemParser.itemNameAndCountAsString("cookies"));
29
+        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("cookies")));
30
+        System.out.println(itemParser.itemNameAndCountAsString("apples"));
31
+        System.out.println(itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount("apples")));
31 32
 
32 33
 
33 34
 

+ 13
- 1
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -111,8 +111,20 @@ public class ItemParserTest {
111 111
         itemParser.createItems();
112 112
         // When
113 113
         itemParser.addNameAndItemsOfSameNameToItemOrganizer();
114
-        String actualNameAndCount = "";
114
+        String actualNameAndCount = itemParser.itemNameAndCountAsString(nameForBread);
115 115
         // Then
116
+        Assert.assertEquals(expectedNameAndCount, actualNameAndCount);
117
+    }
118
+
119
+    @Test
120
+    public void capitalizeFirstLetterOnlyTest() {
121
+        // Given
122
+        String word = "word";
123
+        String expectedWord = "Word";
124
+        // When
125
+        String actualWord = itemParser.capitalizeFirstLetterOnly(word);
126
+        // Then
127
+        Assert.assertEquals(expectedWord, actualWord);
116 128
     }
117 129
 
118 130
     @Test