Browse Source

create poll in model

pull/55/head
Thodoris1999 6 years ago
parent
commit
ee8d038348
  1. 85
      app/src/main/java/gr/thmmy/mthmmy/model/Poll.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/model/Post.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java

85
app/src/main/java/gr/thmmy/mthmmy/model/Poll.java

@ -0,0 +1,85 @@
package gr.thmmy.mthmmy.model;
import java.text.DecimalFormat;
public class Poll {
private final String question;
private Entry[] entries;
private int availableVoteCount;
private String pollFormUrl, sc;
public Poll(String question, Entry[] entries, int availableVoteCount, String pollFormUrl, String sc) {
this.question = question;
this.entries = entries;
this.availableVoteCount = availableVoteCount;
this.pollFormUrl = pollFormUrl;
this.sc = sc;
}
public String getQuestion() {
return question;
}
public Entry[] getEntries() {
return entries;
}
public int getAvailableVoteCount() {
return availableVoteCount;
}
public String getPollFormUrl() {
return pollFormUrl;
}
public String getSc() {
return sc;
}
public int totalVotes() {
int sum = 0;
for (Entry entry : entries) {
sum += entry.votes;
}
return sum;
}
public String getVotePercentage(int index) {
DecimalFormat format = new DecimalFormat(".#");
double percentage = 100 * ((double) entries[index].votes / (double) totalVotes());
return format.format(percentage);
}
static class Entry {
private final String entryName;
private int votes;
public Entry(String entryName, int votes) {
this.entryName = entryName;
this.votes = votes;
}
/**
* Constructor for entry with unknown number of votes
*
* @param entryName
* The name of the entry
*/
public Entry(String entryName) {
this.entryName = entryName;
votes = -1;
}
public String getEntryName() {
return entryName;
}
public int getVotes() {
return votes;
}
public void setVotes(int votes) {
this.votes = votes;
}
}
}

2
app/src/main/java/gr/thmmy/mthmmy/model/Post.java

@ -15,7 +15,7 @@ import java.util.Objects;
* gender, number of posts, personal text and number of start to be described <b>in addition to
* previous fields</b>.</p>
*/
public class Post {
public class Post extends TopicItem {
public static final int TYPE_POST = 0;
public static final int TYPE_QUICK_REPLY = 1;
public static final int TYPE_EDIT = 2;

4
app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java

@ -0,0 +1,4 @@
package gr.thmmy.mthmmy.model;
public class TopicItem {
}
Loading…
Cancel
Save