瀏覽代碼

4.3 still

Trinh Tong 7 年之前
父節點
當前提交
c0dd19bc5f
共有 2 個檔案被更改,包括 43 行新增1 行删除
  1. 12
    0
      pom.xml
  2. 31
    1
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java

+ 12
- 0
pom.xml 查看文件

7
     <groupId>io.zipcoder</groupId>
7
     <groupId>io.zipcoder</groupId>
8
     <artifactId>spring-demo</artifactId>
8
     <artifactId>spring-demo</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>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
     <parent>
22
     <parent>
11
         <groupId>org.springframework.boot</groupId>
23
         <groupId>org.springframework.boot</groupId>
12
         <artifactId>spring-boot-starter-parent</artifactId>
24
         <artifactId>spring-boot-starter-parent</artifactId>

+ 31
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java 查看文件

1
 package io.zipcoder.tc_spring_poll_application.controller;
1
 package io.zipcoder.tc_spring_poll_application.controller;
2
 
2
 
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
4
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
6
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
5
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
7
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.web.bind.annotation.RequestParam;
13
 import org.springframework.web.bind.annotation.RequestParam;
12
 import org.springframework.web.bind.annotation.RestController;
14
 import org.springframework.web.bind.annotation.RestController;
13
 
15
 
16
+import java.util.Collection;
17
+import java.util.HashMap;
18
+import java.util.HashSet;
19
+
14
 @RestController
20
 @RestController
15
 public class ComputeResultController {
21
 public class ComputeResultController {
16
 
22
 
26
         VoteResult voteResult = new VoteResult();
32
         VoteResult voteResult = new VoteResult();
27
         Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
33
         Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
28
 
34
 
35
+        // for each vote in all votes, get the option ID and set to Option ID of OptionCount
36
+        // increase the optioncount count field accordingly for each vote
37
+
38
+        // create Collection Option Counts
39
+        int voteCount = 0;
40
+        HashMap<Long, OptionCount> optionCountsMap = new HashMap<>();
41
+
42
+        for (Vote v: allVotes
43
+             ) {
44
+            voteCount++;
45
+            Long name = (v.getOption().getId());
46
+            if (!optionCountsMap.keySet().contains(name)) {
47
+                optionCountsMap.put(name, new OptionCount());
48
+            } else {
49
+                OptionCount temp = optionCountsMap.get(name);
50
+                temp.setCount(temp.getCount()+1);
51
+                optionCountsMap.replace(name, temp);
52
+            }
53
+        }
54
+
55
+        voteResult.setTotalVotes(voteCount);
56
+
57
+        voteResult.setResults(optionCountsMap.values());
58
+
29
         //TODO: Implement algorithm to count votes
59
         //TODO: Implement algorithm to count votes
30
-        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
60
+        return new ResponseEntity<>(voteResult, HttpStatus.OK);
31
 
61
 
32
     }
62
     }
33
 }
63
 }