ソースを参照

submitting jerkson

chitraB 6 年 前
コミット
6fceebb97f
共有4 個のファイルを変更した185 個の追加6 個の削除を含む
  1. 12
    0
      pom.xml
  2. 111
    5
      src/main/java/io/zipcoder/ItemParser.java
  3. 14
    1
      src/main/java/io/zipcoder/Main.java
  4. 48
    0
      src/main/java/io/zipcoder/NamePricePair.java

+ 12
- 0
pom.xml ファイルの表示

7
     <groupId>io.zipcoder</groupId>
7
     <groupId>io.zipcoder</groupId>
8
     <artifactId>PainfullAfternoon</artifactId>
8
     <artifactId>PainfullAfternoon</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
 
22
 
11
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 111
- 5
src/main/java/io/zipcoder/ItemParser.java ファイルの表示

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.util.*;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
5
 
6
 
6
 public class ItemParser {
7
 public class ItemParser {
7
 
8
 
9
+    private Map<NamePricePair, Integer> pairCountMap;
10
+    private Map<String, Integer> nameCountMap;
11
+    private int exceptionCount = 0;
12
+
13
+    public ItemParser() {
14
+        pairCountMap = new LinkedHashMap<>();
15
+        nameCountMap = new LinkedHashMap<>();
16
+    }
8
 
17
 
9
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
18
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10
         String stringPattern = "##";
19
         String stringPattern = "##";
13
     }
22
     }
14
 
23
 
15
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
24
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
25
+        rawItem = splitStringWithRegexPattern("##", rawItem).get(0);
26
+        List<String> rawKeyValue = findKeyValuePairsInRawItemData(rawItem);
27
+        String name = replaceZero(toLower(findValue(rawKeyValue.get(0))));
28
+        Double price = Double.parseDouble(findValue(rawKeyValue.get(1)));
29
+        if (name.equals("0") || price == 0) {
30
+            exceptionCount++;
31
+            throw new ItemParseException();
32
+        } else {
33
+            addToPairCountMap(new NamePricePair(name, price));
34
+            addToNameCountMap(name);
35
+        }
36
+        String type = toLower(findValue(rawKeyValue.get(2)));
37
+        String expiration = toLower(findValue(rawKeyValue.get(3)));
38
+        return new Item(name, price, type, expiration);
39
+        }
40
+    public int addToPairCountMap(NamePricePair pair) {
41
+        int count = pairCountMap.containsKey(pair) ? pairCountMap.get(pair) : 0;
42
+        pairCountMap.put(pair, count + 1);
43
+        return count + 1;
44
+    }
45
+
46
+    public int addToNameCountMap(String name) {
47
+        int count = nameCountMap.containsKey(name) ? nameCountMap.get(name) : 0;
48
+        nameCountMap.put(name, count + 1);
49
+        return count + 1;
50
+    }
51
+    public String findValue(String pair) {
52
+        Pattern pttrn = Pattern.compile(":(.+)");
53
+        Matcher mtchr = pttrn.matcher(pair);
54
+        return mtchr.find() ? mtchr.group(1) : "0";
17
     }
55
     }
18
 
56
 
57
+    public String replaceZero(String name) {
58
+        return name.equals("co0kies") ? "cookies" : name;
59
+    }
19
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
60
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
61
+        String stringPattern = "[;|^!*^%@]";
21
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
62
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
         return response;
63
         return response;
23
     }
64
     }
24
-
25
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
65
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
66
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27
     }
67
     }
68
+    private String countSpaces(int nameLength) {
69
+        String spaces ="";
70
+        for (int i = 0; i < 8 - nameLength; i++) {
71
+            spaces += " ";
72
+        }
73
+        return spaces;
74
+    }
75
+    private String toLower(String string)
76
+    {
77
+        String result="";
78
+        for(int i=0;i<string.length();i++)
79
+        {
80
+            int c=(int)string.charAt(i);
81
+            if(c>=65&&c<=90)
82
+            {
83
+                c=c+32;
84
+            }
85
+            result=result+(char)c;
86
+        }
87
+        return result;
88
+    }
89
+    private String toCamel(String string) {
90
+        String result="";
91
+        result=result+(char)((int)string.charAt(0)-32);
92
+        for(int i=1;i<string.length();i++) {
93
+            int c = (int)string.charAt(i);
94
+            result = result + (char)c;
95
+        }
96
+        return result;
97
+    }
28
 
98
 
29
 
99
 
30
 
100
 
101
+
102
+    public void printFormatted() {
103
+
104
+        Iterator<Map.Entry<String, Integer>> nameIterator = nameCountMap.entrySet().iterator();
105
+        while (nameIterator.hasNext()) {
106
+            int seenCounter = 0;
107
+            Map.Entry<String, Integer> nameEntry = nameIterator.next();
108
+            System.out.println("name:" + countSpaces(nameEntry.getKey().length()) + toCamel(nameEntry.getKey()) + " \t\t seen: " + nameEntry.getValue() + " times");
109
+            System.out.println("============= \t \t =============");
110
+            Iterator<Map.Entry<NamePricePair, Integer>> pairIterator = pairCountMap.entrySet().iterator();
111
+            while (pairIterator.hasNext()) {
112
+                Map.Entry<NamePricePair, Integer> pairEntry = pairIterator.next();
113
+                if (pairEntry.getKey().getName().equals(nameEntry.getKey())) {
114
+                    System.out.println("Price: \t "+pairEntry.getKey().getPrice()+"\t\t seen: "+pairEntry.getValue()+" times");
115
+                    if (nameEntry.getValue() - pairEntry.getValue() == 0) {
116
+                        System.out.println("-------------\t\t -------------");
117
+                        System.out.println();
118
+                    } else if (nameEntry.getValue() - seenCounter != pairEntry.getValue()) {
119
+                        seenCounter += pairEntry.getValue();
120
+                        System.out.println("-------------\t\t -------------");
121
+                    } else {
122
+                        System.out.println();
123
+                    }
124
+                }
125
+            }
126
+        }
127
+    }
128
+
129
+    public void printExceptions() {
130
+        System.out.println("Errors         \t \t seen: "+ exceptionCount +" times");
131
+
132
+    }
31
 }
133
 }
134
+
135
+
136
+
137
+

+ 14
- 1
src/main/java/io/zipcoder/Main.java ファイルの表示

2
 
2
 
3
 import org.apache.commons.io.IOUtils;
3
 import org.apache.commons.io.IOUtils;
4
 
4
 
5
+import java.util.List;
6
+
5
 
7
 
6
 public class Main {
8
 public class Main {
7
 
9
 
14
     public static void main(String[] args) throws Exception{
16
     public static void main(String[] args) throws Exception{
15
         String output = (new Main()).readRawDataToString();
17
         String output = (new Main()).readRawDataToString();
16
         System.out.println(output);
18
         System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
19
+        System.out.println();
20
+        ItemParser parser = new ItemParser();
21
+        List<String> lines = parser.parseRawDataIntoStringArray(output);
22
+        for (String singleLine : lines) {
23
+            try {
24
+                parser.parseStringIntoItem(singleLine);
25
+            } catch (ItemParseException E) {
26
+            }
27
+        }
28
+        parser.printFormatted();
29
+        parser.printExceptions();
18
     }
30
     }
19
 }
31
 }
32
+

+ 48
- 0
src/main/java/io/zipcoder/NamePricePair.java ファイルの表示

1
+package io.zipcoder;
2
+
3
+import java.util.Arrays;
4
+
5
+public class NamePricePair {
6
+    private String name;
7
+    private double price;
8
+
9
+    public NamePricePair(String name, Double price) {
10
+        this.name = name;
11
+        this.price = price;
12
+    }
13
+
14
+    public String getName() {
15
+        return name;
16
+    }
17
+
18
+    public void setName(String name) {
19
+        this.name = name;
20
+    }
21
+
22
+    public double getPrice() {
23
+        return price;
24
+    }
25
+
26
+    public void setPrice(double price) {
27
+        this.price = price;
28
+    }
29
+    @Override
30
+    public boolean equals(Object another) {
31
+        if (another == this) {
32
+            return true;
33
+        }
34
+        if (!(another instanceof NamePricePair)) {
35
+            return false;
36
+        }
37
+        NamePricePair casted = (NamePricePair)another;
38
+        return this.name.equals(casted.name) && this.price == casted.price;
39
+    }
40
+
41
+    @Override
42
+    public int hashCode() {
43
+        return (int)price;
44
+    }
45
+
46
+
47
+
48
+}