Candidates get sorted descending by their votes within their categories

This commit is contained in:
2020-11-27 23:59:14 +01:00
parent 780448537b
commit ae1ba8b9c5
3 changed files with 65 additions and 41 deletions

View File

@@ -4,7 +4,7 @@ import javax.persistence.*;
@Entity @Entity
@Table(name = "candidates") @Table(name = "candidates")
public class Candidate { public class Candidate implements Comparable<Candidate>{
public Candidate() { public Candidate() {
super(); super();
@@ -41,4 +41,16 @@ public class Candidate {
} }
public Category getCategory() {return category;} public Category getCategory() {return category;}
public void votedFor() {
this.votes += 1;
}
@Override
public int compareTo(Candidate c) {
if (getVotes() == null || c.getVotes() == null) {
return 0;
}
return c.getVotes().compareTo(getVotes());
}
} }

View File

@@ -1,6 +1,7 @@
package com.github.cato447.AbizeitungVotingSystem.entities; package com.github.cato447.AbizeitungVotingSystem.entities;
import javax.persistence.*; import javax.persistence.*;
import java.util.Collections;
import java.util.List; import java.util.List;
@Entity @Entity
@@ -34,6 +35,11 @@ public class Category {
} }
public List<Candidate> getCandidateList() { public List<Candidate> getCandidateList() {
Collections.sort(candidateList);
return candidateList; return candidateList;
} }
public int getCandidateListSize(){
return candidateList.size();
}
} }

View File

@@ -8,9 +8,12 @@
</head> </head>
<body> <body>
<h1>Wähler Liste</h1>
<ul>
<li>
<h1>Wähler Liste</h1>
<div class="voterTable"> <div class="voterTable">
<table class="tableVoters" border="5"> <table class="tableVoters" border="5">
<tr> <tr>
<th>Id</th> <th>Id</th>
@@ -34,7 +37,8 @@
</tr> </tr>
</table> </table>
</div> </div>
</li>
<li>
<h1>Kandidaten Liste</h1> <h1>Kandidaten Liste</h1>
<div th:each="category : ${categories}" th:id="${category.id}"> <div th:each="category : ${categories}" th:id="${category.id}">
<h2 th:text="${category.name}"></h2> <h2 th:text="${category.name}"></h2>
@@ -52,6 +56,8 @@
</tr> </tr>
</table> </table>
</div> </div>
</li>
</ul>
</body> </body>
</html> </html>