BP Forums
Website Giveaway Program - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=32)
+----- Thread: Website Giveaway Program (/showthread.php?tid=821)



Website Giveaway Program - brandonio21 - 01-06-2014

Many of the viewers of the website giveaway video have requested that I share the source code of the program used. In order to heed these requests and ensure that everyone knows that the giveaway is fair, here is the code for the website giveaway program.

Basically, it reads from a text file containing all the comments, parses the text file for usernames, and randomly selects one of them. Any forced waiting is simply for dramatic effect and time creation.

The winner selector:
[code2=java]import java.util.Scanner;
import java.util.Random;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.io.File;
public class comments
{
private static ArrayList<String> commentList;
private static ArrayList<String> removeList;
public static void main(String[] args)
{
System.out.println("Welcome to the giveaway calculator!");
System.out.println("This is for use by BrandonioProductions for his");
System.out.println("2013-2014 website giveaway! Press <ENTER> to begin");
Scanner waiter = new Scanner(System.in);
waiter.nextLine();
System.out.println("First, gathering YouTube comments, please wait.");
for (int i = 0; i < 5; i++)
{
System.out.print(".");
rest();
}
System.out.println();
commentList = new ArrayList<String>();
removeList = new ArrayList<String>();
//First, we need to read from the file
try
{
Scanner reader = new Scanner(new File("comments.txt"));
reader.useDelimiter(System.getProperty("line.separator"));
while (reader.hasNext())
{
String line = reader.next();
if (line.contains("week"))
{
//This is a username
int ind = line.indexOf("1 week ago");
String username = line.substring(0, ind);
if (commentList.contains(username))
removeList.add(username);
else
commentList.add(username);
}
}
reader.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
System.out.format("We found %d entries! Press <ENTER> to continue", commentList.size());
waiter.nextLine();
System.out.println("Removing duplicate entries, please wait..");
for (String s : removeList)
commentList.remove(s);
for (int i = 0; i < 5; i ++)
{
System.out.print(".");
rest();
}
System.out.println();
System.out.println("We are good to go! Press <ENTER> to get the first winner!");
waiter.nextLine();
Random rnd = new Random();
int winner1 = rnd.nextInt(commentList.size());
System.out.format("The first winner has entry # of %d", winner1);
for (int i = 0; i < 5; i++)
{
System.out.print(".");
rest();
}
System.out.println();
System.out.format("The first winner is: \n %s \n", commentList.get(winner1));
System.out.println("Press <ENTER> to get the second winner!");
waiter.nextLine();
int winner2 = rnd.nextInt(commentList.size());
System.out.format("The second winner has entry # of %d", winner2);
for (int i = 0; i < 5; i++)
{
System.out.print(".");
rest();
}
System.out.println();
System.out.format("The second winner is: \n %s", commentList.get(winner2));
}

public static void rest()
{
try
{
Thread.currentThread();
Thread.sleep(1000);
}
catch (Exception e) {}
}
}[/code2]

The text file is attached
[attachment=0]<!-- ia0 -->comments.zip<!-- ia0 -->[/attachment]

You can also find the program on Pastebin:
<!-- m --><a class="postlink" href="http://pastebin.com/DzLdPzGc">http://pastebin.com/DzLdPzGc</a><!-- m -->