From ee8d0383489a76fac7fafac0c2bfca56af9f7e25 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sun, 16 Sep 2018 12:56:35 +0300 Subject: [PATCH] create poll in model --- .../main/java/gr/thmmy/mthmmy/model/Poll.java | 85 +++++++++++++++++++ .../main/java/gr/thmmy/mthmmy/model/Post.java | 2 +- .../java/gr/thmmy/mthmmy/model/TopicItem.java | 4 + 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/model/Poll.java create mode 100644 app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java b/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java new file mode 100644 index 00000000..49fbba8b --- /dev/null +++ b/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; + } + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Post.java b/app/src/main/java/gr/thmmy/mthmmy/model/Post.java index 078386dc..1113ffbc 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Post.java +++ b/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 in addition to * previous fields.

*/ -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; diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java b/app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java new file mode 100644 index 00000000..9d38a85e --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/model/TopicItem.java @@ -0,0 +1,4 @@ +package gr.thmmy.mthmmy.model; + +public class TopicItem { +}