Lottery Ticket Java Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

import java.util.

*;
public class CIS129_WhitneyHenderson_Lab4{
static Scanner keyboard = new Scanner(System.in);

static final int FIVE_NUM_ONE_POWER = 100000000;


static final int FIVE_NUM_NO_POWER = 1000000;
static final int FOUR_NUMS_ONE_POWER = 50000;
static final int FOUR_NUMS_NO_POWER = 100;
static final int THREE_NUMS_ONE_POWER = 100;
static final int THREE_NUMS_NO_POWER = 7;
static final int TWO_NUMS_ONE_POWER = 7;
static final int ONE_NUMS_ONE_POWER =4;
static final int ZERO_NUMS_ONE_POWER = 4;
static final int LOTTERY_TICKET = 5;
static final int STARTING_MONEY = 1000;
static final int TICKET = 2;
static final int TICKET_LOW = 1;
static final int TICKET_HIGH = 69;
static final int POWERBALL_HIGH = 26;

public static void main (String [] args){

int purchase;
int newMoney;
int powerball;

int [] userTickets = new int [LOTTERY_TICKET];


int [] winningTicket = new int [LOTTERY_TICKET];
//boolean [] ticket = new boolean [LOTTERY_TICKET];
displayPowerBallMenu();

//initBoolArray(ticket);
System.out.println("You have $"+STARTING_MONEY);
purchase = getValidPurchase("How many $"+TICKET+" lottery cards would you like to
purchase?");
newMoney = getMoney(purchase);
System.out.println(newMoney);
powerball = initializePowerball();
initializeTicket(winningTicket);
sortLotteryTicket(winningTicket);
winningTicket(winningTicket, powerball);
System.out.println("\n");
//how many tickets the user purchases
for(int x = 0; x < purchase; x++){
initializeTicket(userTickets);
sortLotteryTicket(userTickets);
powerball = initializePowerball();
printTicketNumbers(winningTicket, powerball, userTickets);
}

}//end main

public static void displayPowerBallMenu(){


System.out.println("**************************************************");
System.err.println("\t Let's play Powerball");
System.out.println("**************************************************");
System.out.println("5 numbers correct plus powerball = $"+FIVE_NUM_ONE_POWER);
System.out.println("5 numbers correct, no powerball = $"+FIVE_NUM_NO_POWER);
System.out.println("4 numbers correct plus powerball = $"+FOUR_NUMS_ONE_POWER);
System.out.println("4 numbers correct, no powerball = $"+FOUR_NUMS_NO_POWER);
System.out.println("3 numbers correct plus powerball = $"+THREE_NUMS_ONE_POWER);
System.out.println("3 numbers correct, no powerball = $"+THREE_NUMS_NO_POWER);
System.out.println("2 numbers correct plus powerball = $"+TWO_NUMS_ONE_POWER);
System.out.println("1 number correct plus powerball = $"+ONE_NUMS_ONE_POWER);
System.out.println("0 numbers correct plus powerball = $"+ZERO_NUMS_ONE_POWER);
System.out.println("**************************************************");
System.out.println("\n");
}//end display

public static void initializeTicket(int [] arr){


for (int x = 0; x < arr.length; x++){
arr[x] = getValidRando(arr);
}// end for
}// end initialize

public static int getValidRando(int [] arr){


int random = IR.getRandomNumber(TICKET_LOW, TICKET_HIGH);
while (validRandom(arr, random)){
random = IR.getRandomNumber(TICKET_LOW, TICKET_HIGH);
}
return random;
}
public static boolean validRandom(int [] arr, int random){
for(int x = 0; x < arr.length; x++){
if(random == arr[x]){
return true;
}
}
return false;
}
public static void winningTicket(int [] winTick, int powerball){
for(int x = 0; x < winTick.length; x++){
if(winTick[x]<10){
System.out.print(winTick[x]+", ");
}else{
System.out.print(winTick[x]+", ");
}
}
System.out.print(" "+powerball);
System.out.println();
}// end winningTicket

public static void printTicketNumbers(int [] winTick, int powerball, int [] userTickets){


for (int x = 0; x < userTickets.length; x++){
if ((userTickets[x]<10) && (searchTickets(winTick, userTickets[x]))){
System.err.print(userTickets[x]+", ");
}else if(searchTickets(winTick, userTickets[x])){
System.err.print(userTickets[x]+", ");
}
else{
System.out.print(userTickets[x]+", ");

}
}
System.out.print(" "+powerball);
System.out.println();
}

public static int initializePowerball(){


int powerball = IR.getRandomNumber(TICKET_LOW, TICKET_HIGH);
return powerball;
}

public static boolean searchTickets(int [] winningTicket, int userNum){


for (int x = 0; x < winningTicket.length; x++){
if(userNum == winningTicket[x]){
return true;
}
}
return false;

}// end search


public static int getValidPurchase(String msg){
int purchase = IR.getInteger(msg);
while(validInput(purchase)){
System.err.println("Invalid value, you have entered a number out of range. Please try
again.");
purchase = IR.getInteger(msg);
}
return purchase;
}// end valid purchase

public static boolean validInput(int purchase){


if(purchase < 0){
return true;
}
if((TICKET*purchase) > STARTING_MONEY){
return true;
}else{
return false;
}
}// end valid purchase

public static int getMoney(int purchase){


int newMoney;
int ticksPurchased;
ticksPurchased = purchase * TICKET;
newMoney = STARTING_MONEY - ticksPurchased;
return newMoney;
}
public static void sortLotteryTicket(int [] arr){
int temp = 0;
for (int i = 0; i < arr.length; i++){
for (int j = i+1; j < arr.length; j++){
if(arr[i]>arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

}//end sort

}//end program

You might also like