Bläddra i källkod

Done with formatting the output

Kibret Tecle 6 år sedan
förälder
incheckning
6418a07b31

+ 12
- 0
pom.xml Visa fil

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>PainfullAfternoon</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 0
- 2
src/main/java/io/zipcoder/Item.java Visa fil

@@ -1,7 +1,5 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Map;
5 3
 
6 4
 public class Item {
7 5
     private  String name;

+ 10
- 20
src/main/java/io/zipcoder/ItemParser.java Visa fil

@@ -85,7 +85,7 @@ public class ItemParser {
85 85
 
86 86
     }
87 87
 
88
-    public HashMap<String, ArrayList<Item>> getMap() throws Exception {
88
+    public HashMap<String, ArrayList<Item>> buildMap() throws Exception {
89 89
         Main main = new Main();
90 90
 
91 91
         ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
@@ -107,20 +107,20 @@ public class ItemParser {
107 107
     }
108 108
 
109 109
     public String generateReport() throws Exception {
110
-        groceryList = getMap();
110
+        groceryList = buildMap();
111 111
         StringBuilder builder = new StringBuilder();
112 112
 
113 113
         for(Map.Entry<String,ArrayList<Item>>groceryItems:groceryList.entrySet()){
114 114
             builder.append("\nname: ");
115
-            builder.append(captitalizeFirstLetter(groceryItems.getKey()));
115
+            builder.append(String.format("%8s",captitalizeFirstLetter(groceryItems.getKey())));
116 116
             builder.append("\t\t\t\tseen: "+getOccurencesOfItems(groceryItems.getValue())+" times\n");
117
-            builder.append("=========="+"\t\t\t\t============\n");
117
+            builder.append("==============="+"\t\t\t\t===============\n");
118 118
             String priceReport = generatePriceReport(groceryItems);
119 119
             builder.append(priceReport);
120
-            builder.append("-------------\t\t\t\t--------------\n");
120
+            builder.append("---------------"+"\t\t\t\t---------------\n");
121 121
         }
122 122
 
123
-        builder.append("\nErrors\t\t\t\tseen: "+counter+" times\n");
123
+        builder.append("\nErrors\t\t\t\t\t\tseen: "+counter+" times\n");
124 124
 
125 125
         return builder.toString();
126 126
     }
@@ -144,26 +144,16 @@ public class ItemParser {
144 144
         ArrayList<Double> nonDupPrices = getUiniquePrices(input);
145 145
         for(int i=0;i<nonDupPrices.size();i++){
146 146
             priceReport+="Price";
147
-            priceReport+=(String.format("%6s",nonDupPrices.get(i)));
148
-            priceReport+=("\t\t\tseen: "+ getOccurencesOfPrices(input.getValue(),nonDupPrices.get(i))+
149
-                        "times\n");
147
+            priceReport+=(String.format("%10s",nonDupPrices.get(i)));
148
+            priceReport+=("\t\t\t\tseen: "+ getOccurencesOfPrices(input.getValue(),nonDupPrices.get(i))+
149
+                        " times\n");
150 150
         }
151 151
         return priceReport;
152 152
 
153 153
     }
154 154
 
155
-    public String getNameReport(HashMap<String, ArrayList<Item>> input, String key) {
156
-        String nameReport = "";
157
-        nameReport += "name:" + "       " + key + "         seen" + input.get(key).size() + "       times";
158
-        return nameReport;
159
-    }
160
-
161
-    public ArrayList<Item> getArrayList(Map<String, ArrayList<Item>> input, String key) {
162
-        return input.get(key);
163
-    }
164
-
165 155
     public ArrayList<Double> getUiniquePrices(Map.Entry<String, ArrayList<Item>> input) {
166
-        ArrayList<Double> uniquePrices = new ArrayList<Double>();
156
+        ArrayList<Double> uniquePrices = new ArrayList<>();
167 157
         for (int i=0;i<input.getValue().size();i++) {
168 158
             if (!uniquePrices.contains(input.getValue().get(i).getPrice())) {
169 159
                 uniquePrices.add(input.getValue().get(i).getPrice());

+ 0
- 21
src/main/java/io/zipcoder/Main.java Visa fil

@@ -24,27 +24,6 @@ public class Main {
24 24
 
25 25
         ArrayList<String> itemList = parser.parseRawDataIntoStringArray(output);
26 26
 
27
-
28
-//        for (String item : itemList) {
29
-//
30
-//            System.out.println(parser.parseRawDataIntoStringArray(main.readRawDataToString()));
31
-//             System.out.println(parser.parseStringIntoItem(item));
32
-//        }
33
-//        System.out.println(ItemParser.counter);
34
-//        Map<String, ArrayList<Item>> grocery = parser.getMap();
35
-//        for (String key : grocery.keySet()) {
36
-//            System.out.println(key);
37
-//        }
38
-//        ArrayList<String> itemListCopy = parser.parseRawDataIntoStringArray(output);
39
-//        for (Map.Entry<String, ArrayList<Item>> mapKey : parser.getMap().entrySet()) {
40
-//            System.out.println(mapKey.getKey());
41
-//            for (Item item : mapKey.getValue()) {
42
-//                System.out.println(item.getPrice());
43
-//            }
44
-//        }
45
-//        System.out.println(ItemParser.counter);
46
-
47
-
48 27
         System.out.println(parser.generateReport());
49 28
    }
50 29
 

+ 74
- 0
src/test/java/io/zipcoder/ItemParserTest.java Visa fil

@@ -5,6 +5,8 @@ import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7 7
 import java.util.ArrayList;
8
+import java.util.HashMap;
9
+import java.util.Map;
8 10
 
9 11
 import static org.junit.Assert.*;
10 12
 
@@ -59,4 +61,76 @@ public class ItemParserTest {
59 61
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 62
         assertEquals(expected, actual);
61 63
     }
64
+    @Test
65
+    public void findNameTest(){
66
+        String expected = "milk";
67
+        String actual = itemParser.findName(rawSingleItem);
68
+        Assert.assertEquals(expected,actual);
69
+    }
70
+    @Test
71
+    public void findPriceTest(){
72
+        String expected = "3.23";
73
+        String actual = itemParser.findPrice(rawSingleItem);
74
+        Assert.assertEquals(expected,actual);
75
+    }
76
+    @Test
77
+    public void findTypeTest(){
78
+        String expected = "food";
79
+        String actual = itemParser.findType(rawBrokenSingleItem);
80
+        Assert.assertEquals(expected,actual);
81
+    }
82
+    @Test
83
+    public void findExpirationDate(){
84
+        String expected = "1/11/2016";
85
+        String actual= itemParser.findExpirationDate(rawSingleItemIrregularSeperatorSample);
86
+        Assert.assertEquals(expected,actual);
87
+    }
88
+    @Test
89
+    public void buildMapTest1 ()throws Exception{
90
+        Map<String,ArrayList<Item>>testMap= new HashMap<String, ArrayList<Item>>();
91
+        testMap = itemParser.buildMap();
92
+        boolean actual = testMap.containsKey("milk");
93
+        Assert.assertTrue(actual);
94
+    }
95
+    @Test
96
+    public void buildMapTest2() throws Exception{
97
+        Map<String,ArrayList<Item>>testMap= new HashMap<String, ArrayList<Item>>();
98
+        testMap = itemParser.buildMap();
99
+        int expected = 4;
100
+        int actual = testMap.get("apples").size();
101
+        Assert.assertEquals(expected,actual);
102
+
103
+    }
104
+    @Test
105
+    public void getOccurencesOfItemsTest() throws ItemParseException {
106
+        ArrayList<Item>test = new ArrayList<Item>();
107
+
108
+        Item item1 = itemParser.parseStringIntoItem(rawSingleItem);
109
+        String rawItem2 =("naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##");
110
+        Item item2 = itemParser.parseStringIntoItem(rawItem2);
111
+        test.add(item1);
112
+        test.add(item2);
113
+        int expected = 2;
114
+        int actual = itemParser.getOccurencesOfItems(test);
115
+        Assert.assertEquals(expected,actual);
116
+
117
+    }
118
+    @Test
119
+    public void getOccurencesOfPricesTest() throws ItemParseException {
120
+
121
+        ArrayList<Item>test = new ArrayList<Item>();
122
+
123
+        Item item1 = itemParser.parseStringIntoItem(rawSingleItem);
124
+        String rawItem2 =("naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##");
125
+        String rawItem3 = "naMe:Bread;price:1.23;type:Food^expiration:1/11/2016##";
126
+        Item item2 = itemParser.parseStringIntoItem(rawItem2);
127
+        Item item3 = itemParser.parseStringIntoItem(rawItem3);
128
+        test.add(item1);
129
+        test.add(item2);
130
+        test.add(item3);
131
+        int expected =2;
132
+        int actual = itemParser.getOccurencesOfPrices(test,1.23);
133
+        Assert.assertEquals(expected,actual);
134
+    }
135
+
62 136
 }