Browse Source

working on final display

Luis Romero 6 years ago
parent
commit
a08a4cbc20

+ 30
- 10
src/main/java/io/zipcoder/ItemParser.java View File

@@ -3,13 +3,9 @@ package io.zipcoder;
3 3
 import apple.laf.JRSUIUtils;
4 4
 
5 5
 import java.rmi.activation.ActivationGroup_Stub;
6
-import java.util.ArrayList;
7
-import java.util.Arrays;
8
-import java.util.List;
6
+import java.util.*;
9 7
 import java.util.regex.Pattern;
10 8
 import java.util.regex.Matcher;
11
-import java.util.Map;
12
-import java.util.TreeMap;
13 9
 import java.util.Map.Entry;
14 10
 
15 11
 public class ItemParser {
@@ -96,8 +92,17 @@ public class ItemParser {
96 92
         return sb.toString();
97 93
     }
98 94
 
95
+    public String itemNameAndCountAsString(String name) {
96
+        StringBuilder sb = new StringBuilder();
97
+        for (Entry entry : itemOrganizer.entrySet()) {
98
+            sb.append("name: " + entry.getKey() + " " + "seen: " + itemOrganizer.get(entry.getKey()).size() + "\n");
99
+        }
100
+        sb.deleteCharAt(sb.length() - 1);
101
+        return sb.toString();
102
+    }
103
+
99 104
     public Map<Double, Integer> getPricesAndTheirCount(String name) {
100
-        Map<Double, Integer> pricesAndTheirCount = new TreeMap<>();
105
+        Map<Double, Integer> pricesAndTheirCount = new TreeMap<>(Collections.reverseOrder());
101 106
         for (String nameKey : itemOrganizer.keySet()) {
102 107
             if (nameKey.equals(name)) {
103 108
                 for (int i = 0; i < itemOrganizer.get(nameKey).size(); i++) {
@@ -114,11 +119,26 @@ public class ItemParser {
114 119
     }
115 120
 
116 121
     public String pricesAndTheirCountAsString(Map<Double, Integer> pricesAndTheirCount) {
117
-        StringBuilder sb = new StringBuilder();
118
-        String nameTest = "milk";
119 122
 
120
-
121
-        return null;
123
+        int count = 0;
124
+        StringBuilder sb = new StringBuilder();
125
+        sb.append("=============\t\t=============\n");
126
+        for (Double price : pricesAndTheirCount.keySet()) {
127
+            String timeOrTimes = "times";
128
+            if (pricesAndTheirCount.get(price) == 1) {
129
+                timeOrTimes = "time";
130
+            }
131
+            if (price != 0) {
132
+                sb.append("Price:   " + price + "\t\tseen: " + pricesAndTheirCount.get(price) + " " + timeOrTimes + "\n");
133
+                sb.append("-------------\t\t-------------\n");
134
+            }
135
+            count++;
136
+        }
137
+        if (count > 1) {
138
+            sb.delete(sb.length() - 29, sb.length());
139
+        }
140
+//        sb.deleteCharAt(sb.length() - 1);
141
+        return sb.toString();
122 142
     }
123 143
 
124 144
     public Item parseStringIntoItem(String rawItem) throws ItemParseException {

+ 9
- 1
src/main/java/io/zipcoder/Main.java View File

@@ -22,8 +22,16 @@ public class Main {
22 22
         itemParser.addNameAndItemsOfSameNameToItemOrganizer();
23 23
 
24 24
 //        System.out.println(itemParser.itemsAsString());
25
-
26 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")));
31
+
32
+
33
+
34
+
27 35
         System.out.println("*****************************");
28 36
         System.out.println(itemParser.getIpe().errorsAsString());
29 37
 

+ 23
- 7
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -26,6 +26,11 @@ public class ItemParserTest {
26 26
                                       +"naME:BreaD;price:3.23;type:Food;expiration:1/02/2016##"
27 27
                                       +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
28 28
 
29
+    private String rawMultipleItems3 = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
30
+                                      +"naME:BreaD;price:3.23;type:Food;expiration:1/02/2016##"
31
+                                      +"naME:BreaD;price:3.23;type:Food;expiration:1/02/2016##"
32
+                                      +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
33
+
29 34
     private ItemParser itemParser;
30 35
 
31 36
     @Before
@@ -98,6 +103,19 @@ public class ItemParserTest {
98 103
     }
99 104
 
100 105
     @Test
106
+    public void itemNameAndCountAsString() throws ItemParseException {
107
+        // Given
108
+        String nameForBread = "bread";
109
+        String expectedNameAndCount = "name:   Bread\t\tseen: 3 times";
110
+        itemParser.parseRawDataIntoItemStringArray(rawMultipleItems3);
111
+        itemParser.createItems();
112
+        // When
113
+        itemParser.addNameAndItemsOfSameNameToItemOrganizer();
114
+        String actualNameAndCount = "";
115
+        // Then
116
+    }
117
+
118
+    @Test
101 119
     public void getPricesAndTheirCountTest() throws ItemParseException {
102 120
         // Given
103 121
         String nameForBread = "bread";
@@ -120,20 +138,18 @@ public class ItemParserTest {
120 138
     public void pricesAndTheirCountAsStringTest() throws ItemParseException {
121 139
         // Given
122 140
         String nameForBread = "bread";
123
-        itemParser.parseRawDataIntoItemStringArray(rawMultipleItems2);
141
+        itemParser.parseRawDataIntoItemStringArray(rawMultipleItems3);
124 142
         itemParser.createItems();
125 143
         itemParser.addNameAndItemsOfSameNameToItemOrganizer();
126 144
         String expectedPricesAndTheirCount = "=============\t\t=============\n" +
127
-                                             "Price:   " + 3.23 + "\t\tseen: " + 5 + " " + "times" + "\n" +
145
+                                             "Price:   " + "3.23" + "\t\tseen: " + "2" + " " + "times" + "\n" +
128 146
                                              "-------------\t\t-------------\n" +
129
-                                             "Price:   " + 1.23 + "\t\tseen: " + 1 + " " + "time" + "\n" +
147
+                                             "Price:   " + "1.23" + "\t\tseen: " + "1" + " " + "time" + "\n" +
130 148
                                              "-------------\t\t-------------";
131 149
         // When
132
-        int actual323Count = itemParser.getPricesAndTheirCount(nameForBread).get(expected323Price);
133
-        int actual123Count = itemParser.getPricesAndTheirCount(nameForBread).get(expected123Price);
150
+        String actualPATC = itemParser.pricesAndTheirCountAsString(itemParser.getPricesAndTheirCount(nameForBread));
134 151
         // Then
135
-        Assert.assertEquals(expected323Count, actual323Count);
136
-        Assert.assertEquals(expected123Count, actual123Count);
152
+        Assert.assertEquals(expectedPricesAndTheirCount, actualPATC);
137 153
     }
138 154
 
139 155
     @Test