import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Election
*/
public class Election extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Election() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>Welcome to the Online Voting Simulation System</title></head><body>");
out.println("<h2>Enter predicted percentages for Republican and Democrats in each state </h2>");
out.println("<h3>The sum should not exceed 100, votes not going to either party are automatically assumed to be 3rd party</h3>");
out.println("<form method=\"post\" action =\""
+ request.getContextPath() + "/Election\" >");
out.println("<table border=\"0\"><tr><td valign=\"top\">");
out.println("<h3>The State of Indiana </h3>");
out.println("<tr><td>Input Indiana's democrat percentage: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"IndianaD\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("<tr><td>Input Indiana's republican percentage: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"IndianaR\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("<h3>The State of Califorina </h3>");
out.println("<tr><td>Input Califorina's democrat percentage: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"CaliforinaD\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("<tr><td>Input Califorina's republican percentage: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"CaliforinaR\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("<h3>The State of Hawaii </h3>");
out.println("<tr><td>Input Hawaii's democrat percentage: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"HawaiiD\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("<tr><td>Input Hawaii's republican percentage: </td> <td valign=\"top\">");
out.println("<input type=\"text\" name=\"HawaiiR\" size=\"20\">");
out.println("</td></tr><tr><td valign=\"top\">");
out.println("<input type=\"submit\" value=\"Click here to see electorial results\"></td></tr>");
out.println("</table></form>");
out.println("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>Here are the election results!</title></head><body>");
double IndianaR = 0;
double IndianaD = 0;
double CaliforinaR = 0;
double CaliforinaD = 0;
double HawaiiD = 0;
double HawaiiR = 0;
// the error handling works by if-else statements. If the user inputs
// nothing, the program assumes 0 (see above int declarations) and will
// warn the user in the output. If the user input a valid number (ie
// else), the program computes the numbers.
if (request.getParameter("IndianaR") == null
|| request.getParameter("IndianaR").equals("")
|| request.getParameter("IndianaR").equals(101)) {
out.println("Warning there is an error in the Indiana Republican field! ");
} else {
IndianaR = Integer.parseInt(request.getParameter("IndianaR"));
}
if (request.getParameter("IndianaD") == null
|| request.getParameter("IndianaD").equals("")) {
out.println("Warning there is an error in the Indiana Democratic field! ");
} else {
IndianaD = Integer.parseInt(request.getParameter("IndianaD"));
}
if (request.getParameter("CaliforinaR") == null
|| request.getParameter("CaliforinaR").equals("")) {
out.println("Warning there is an error in the Califorina Republican field! ");
} else {
CaliforinaR = Integer.parseInt(request.getParameter("CaliforinaR"));
}
if (request.getParameter("CaliforinaD") == null
|| request.getParameter("CaliforinaD").equals("")) {
out.println("Warning there is an error in the Califorina Democratic field! ");
} else {
CaliforinaD = Integer.parseInt(request.getParameter("CaliforinaD"));
}
if (request.getParameter("HawaiiD") == null
|| request.getParameter("HawaiiD").equals("")) {
out.println("Warning there is an error in the Califorina Republican field! ");
} else {
HawaiiR = Integer.parseInt(request.getParameter("HawaiiD"));
if (request.getParameter("HawaiiR") == null
|| request.getParameter("HawaiiR").equals("")) {
out.println("Warning there is an error in the Hawaii Republican field! ");
} else {
HawaiiR = Integer.parseInt(request.getParameter("HawaiiR"));
}
}
double democratoverall;
double republicoverall;
double thirdparty;
String result_indiana;
String result_califorina;
String result_hawaii;
double HawaiiNumVotes = 4;
double CaliforinaNumVotes = 55;
double IndianaNumVotes = 11;
int HawaiiVoters =Integer.parseInt( String.valueOf(1374810 * .10));
int IndianaVoters = Integer.parseInt( String.valueOf(6516922 * .10));
int CaliforinaVoters = Integer.parseInt( String.valueOf(37691912 * .10));
double voterturnout = 1.10;
int d=0;
int r=0;
int t=0;
int resultHawaii[] = getVotes(HawaiiVoters, HawaiiD, HawaiiR);
int resultCaliforina[] = getVotes (CaliforinaVoters, CaliforinaD, CaliforinaR);
int resultIndiana[] = getVotes (IndianaVoters, IndianaD, IndianaR);
for(int i =0;i<resultHawaii.length;i++){
if(resultHawaii[i]==0){
d=d+1;
}else if(resultHawaii[i] <0){
r=r+1;
}else {
t=t+1;
}
out.println("Hawaii's Projected winner is " + result_hawaii + ".");
out.println("<br />");
out.println("Indiana's Projected winner is " + result_califorina + ".");
out.println("<br />");
out.println("Califorina's Projected winner is " + result_indiana + ".");
out.println("<br />");
}
}
/*
* This method will generate an integer array of simulated election votes by
* generating random numbers according to the predicted voter rates
* Parameters: 1. number of voters, 2. predicted Democratic party support
* rate, and 3. predicted Republican party support rate Return: an array
* with a value for each voter, where the values will be 0 if it represents
* a vote for the Democratic party 1 if it represents a vote for the
* Republican party 2 if it represents a vote for any other "third party"
*/
public int[] getVotes(int numvoters, double democraticrate,
double republicanrate) {
// declare and allocate an array to hold votes
int[] predictedvotes = new int[numvoters];
Random random = new Random();
for (int i = 0; i < numvoters; i++) {
// generate a random vote between 0 and 1
double vote = random.nextDouble();
// if the vote is between 0 and the democratic rate, assign 0 for a
// democratic vote
if (vote <= democraticrate) {
predictedvotes[i] = 0;
}
// if the vote is between democratic rate and its sum with the
// republican rate,
// assign 1 for a republican vote
else {
if (vote > democraticrate
&& vote <= democraticrate + republicanrate) {
predictedvotes[i] = 1;
}
// otherwise, assign 2 for all other parties
else {
predictedvotes[i] = 2;
}
}
}
return predictedvotes;
}
}