Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String addVote(String voterID, String vote) {
- StringBuilder builder = new StringBuilder();
- boolean userFound = false;
- int position = -1;
- ArrayList<String> userVotes;
- ArrayList<String> newVotes = new ArrayList<>(Arrays.asList(vote.split(" ")));
- for (int i = 0; i < voting.size(); i++) {
- String[] testVotes = voting.get(i).split(",");
- if (voterID.equals(testVotes[0])) {
- position = i;
- userFound = true;
- }
- }
- if (userFound) {
- userVotes = new ArrayList<>(Arrays.asList(voting.get(position).split(",")));
- } else {
- userVotes = new ArrayList<>();
- }
- if (userVotes.size() == Globals.voteLimit++) {
- return "> You have already used up all of your votes.";
- }
- int x = 0;
- builder.append("> Your votes for entries: \n");
- for (int i = 0; userVotes.size() > i; i++) {
- if (x < Globals.voteLimit) {
- try {
- int entry = Integer.parseInt(newVotes.get(i));
- if (entry > Globals.compEntries) {
- builder.append(" **Vote Not counted. Reason: Number too high**\n");
- } else {
- userVotes.add(""+entry);
- builder.append(" Entry: **" + entry + "**\n");
- x++;
- }
- } catch (NumberFormatException e) {
- builder.append(" **Vote Not counted. Reason: Not a number**\n");
- }
- }
- }
- if (newVotes.size() > Globals.voteLimit){
- builder.append(" **Rest of Votes not counted: Reason Max votes = " + Globals.voteLimit + "**\n");
- }
- builder.append(" Have been saved.");
- StringBuilder finalVotes = new StringBuilder();
- for (String s : userVotes){
- finalVotes.append(s + ",");
- }
- finalVotes.delete(finalVotes.length()-1,finalVotes.length());
- if (userFound){
- voting.set(position,finalVotes.toString());
- }else {
- voting.add(finalVotes.toString());
- }
- return builder.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment