Heto yung code.
Running as it is and maraming puwedeng i-improve.
Feel free to edit and improve.
(warm up coding sa klase kahpon)
---code start--
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class LottoGeneratorApp {
static Scanner input;
public static final int MAX_NUMBERS = 6;
public static final int MAX_MAXLOTTO = 45;
public static final int MAX_MEGA = 55;
static ArrayList<Integer> listOfNumbers;
public static void main(String[] args){
//1. display welcome
displayWelcome();
//2. display menu
displayMenu();
//3. get user choice [1 2 or 3]
int choice = getChoice();
//4. process the operation
processChoice(choice);
}
private static void processChoice(int choice) {
switch(choice){
case 1:
System.out.print("\nTayaan mo ito sa MAX LOTTO:\t");
generateNumbers(MAX_MAXLOTTO);
break;
case 2:
System.out.print("\nTayaan mo ito sa MEGA LOTTO:\t");
generateNumbers(MAX_MEGA);
break;
case 3: System.exit(0);
default: System.exit(0);
}
}
private static void generateNumbers(int maxNumber) {
listOfNumbers = new ArrayList<Integer>();
Random random = new Random();
boolean duplicated;
int generatedNumber = 0;
for(int i=0; i<MAX_NUMBERS; i++){
do{
generatedNumber = random.nextInt(maxNumber + 1);
if(listOfNumbers.contains(generatedNumber)){
duplicated = true;
} else {
listOfNumbers.add(generatedNumber);
duplicated = false;
}
}while(generatedNumber == 0 || duplicated);
System.out.print(generatedNumber + "\t");
}
}
private static int getChoice() {
input = new Scanner(System.in);
System.out.print("\n\nEnter choice [ 1, 2, 3]");
int choice = input.nextInt();
return choice;
}
private static void displayMenu() {
System.out.println("************************");
System.out.println("CHOOSE A LOTTO");
System.out.println("[ 1 ] Max Lotto 6/45");
System.out.println("[ 2 ] Mega Lotto 6/55");
System.out.println("[ 3 ] Exit");
}
private static void displayWelcome() {
System.out.println("Lotto Generator 1.0 - WELCOME!");
}
}
--code ends--