Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Erica Matthews   on Sep 23 In Java Category.

  
Question Answered By: Boyce Fischer   on Sep 23

I made the code do a bit more, but I still don't know
where the customer data comes from. I've assumed for
now that it comes from a txt  file on the hard drive.

Here's the altered code, however it has a flaw in that
it doesn't ensure that the winning numbers  are all
unique. I'm sure that wouldn't be too hard to fix
though.

import java.sql.*;
import java.lang.*;
import java.util.*;

public class  lotteryTest
{
int[] winningNumbers = new int[4];

public static  void main(String args[])
{
lotteryTest runner = new lotteryTest();

//Connect to the customer data file.
connectionHandler connections = new
connectionHandler();
connections.connect();
Statement statement1 = connections.statement1;

//Get the customer's numbers from the data file
and compare them to the winnning numbers.
runner.getCustNumbers(statement1);

//Close the connection to the data file.
connections.closeConnection();
}

lotteryTest()
{
//Generate the four winning numbers. Note
presently this does Not eliminate the possibility of
same numbers occurring:
for(int u = 0;u<4; u++)
{
double a = Math.random() *100;
winningNumbers[u] = (int)Math.round(a);
}
Arrays.sort(winningNumbers);
}

void getCustNumbers(Statement statement1)
{
try
{
int[] custNumbers = new int[4];
boolean awinner = false;

String sql = "SELECT * FROM lotteryData.txt";
ResultSet rs = statement1.executeQuery(sql );
while(rs.next())
{
String firstNm = rs.getString(1);
String lastNm = rs.getString(2);

//grab the customer's numbers into an array
for(int i = 4; i<8;i++)
custNumbers[i-4] = rs.getInt(i);
Arrays.sort(custNumbers);

/*
//A way to ensure a winner, just for testing
winningNumbers[0] = 10;
winningNumbers[1] = 15;
winningNumbers[2] = 29;
winningNumbers[3] = 48;
*/

//Compare the customer's numbers to the
winning numbers:
if(custNumbers[0] == winningNumbers[0] &&
custNumbers[1] == winningNumbers[1] && custNumbers[2]
== winningNumbers[2] && custNumbers[3] ==
winningNumbers[3])
{
//Show the winning numbers:
for(int o = 1; o<5;o++)
System.out.println("winning number  "+ o +"
is "+winningNumbers[o-1]);

System.out.println("\nThe winner is " +
firstNm +" "+lastNm+"!");
awinner = true;
}
}
if(!awinner)
{
//Show the winning numbers:
for(int o = 1; o<5;o++)
System.out.println("winning number "+ o +" is
"+winningNumbers[o-1]);

System.out.println("\nNo winners this time!");
}
}
catch(Exception en)
{
System.out.println(en);
}
}
}

class connectionHandler
{
Statement statement1;
Connection connection1;

void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection1 =
DriverManager.getConnection("jdbc:odbc:txtDsn");
statement1 = connection1.createStatement();
}
catch (Exception e)
{
System.err.println(e);
}
}

void closeConnection()
{
try
{
statement1.close();
connection1.close();
}
catch (Exception ex)
{
System.err.println(ex);
}
}
}

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Lottery Program Or get search suggestion and latest updates.


Tagged: