Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Created November 19, 2019 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/d9625ad7575fcfddb22d1a990ebec038 to your computer and use it in GitHub Desktop.
Save bytecodeman/d9625ad7575fcfddb22d1a990ebec038 to your computer and use it in GitHub Desktop.
Lucky for Life Quick Pick Generater V2.0
/*
* Name: Prof. Antonio C. Silvestri
* Date: 11/19/2019
* Course Number: CSC-111
* Course Name: Intro to Java Programming
* Problem Number: NA
* Email: silvestri@stcc.edu
* Description: Generate Lucky for Life Numbers
*/
import java.util.Scanner;
public class LuckyForLife {
private static final String TITLE = "Lucky For Life Quick Pick Generator V2.0";
private static final String CONTINUE_PROMPT = "Do this again? [y/N] ";
private static int[] initArray(int number) {
int list[] = new int[number];
for (int i = 0; i < list.length; i++)
list[i] = i + 1;
return list;
}
private static void shuffle(int[] list) {
for (int i = 0; i < list.length; i++) {
int randomIndex = (int)(Math.random() * list.length);
int temp = list[i];
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
private static int[] dealOutRandoms(int[] list, int numberToDealOut) {
int[] luckies = new int[numberToDealOut];
for (int i = 0; i < numberToDealOut; i++)
luckies[i] = list[i];
return luckies;
}
private static void sort(int[] lucky) {
for (int i = 0; i < lucky.length - 1; i++)
for (int j = i + 1; j < lucky.length; j++)
if (lucky[j] < lucky[i]) {
int temp = lucky[i];
lucky[i] = lucky[j];
lucky[j] = temp;
}
}
private static int generateLuckyBall(int max) {
return (int)(Math.random() * max) + 1;
}
private static String produceAQuickPick(int[] lucky, int luckyBall) {
String qp = "";
for (int i = 0; i < lucky.length; i++)
qp += String.format("%02d%s", lucky[i], i < lucky.length - 1 ? "-" : "");
qp += " ";
qp += String.format("%02d", luckyBall);
return qp;
}
private static void printLotteryTicket(String[] quickPicks) {
for (int i = 0; i < quickPicks.length; i++) {
System.out.printf("%2d: %s\n", i+1, quickPicks[i]);
}
}
private static String[] generateQuickPicks(int total) {
String[] qps = new String[total];
for (int i = 0; i < total; i++) {
int list[] = initArray(43);
shuffle(list);
int lucky[] = dealOutRandoms(list, 5);
sort(lucky);
int luckyBall = generateLuckyBall(43);
qps[i] = produceAQuickPick(lucky, luckyBall);
}
return qps;
}
//**********************************************
private static void process(Scanner sc, String args[]) {
System.out.print("How many quick picks? ");
int total = sc.nextInt();
sc.nextLine();
String quickpicks[] = generateQuickPicks(total);
printLotteryTicket(quickpicks);
}
//**********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
//**********************************************
public static void main(String args[]) {
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment