🔒 Closed Lotto numbers generator

Status
Not open for further replies.

codyscott

Honorary Poster
Isang uling halimbawa ng magandang code programming practice.
Malinis at readable na MAIN method. DO NOT OVERLOAD the MAIN method.
Hati-hatiin ang mga TASKS by using methods.
 

Attachments

  • cleancode2.webp
    cleancode2.webp
    100.9 KB · Views: 42
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--
 
Status
Not open for further replies.

About this Thread

  • 8
    Replies
  • 2K
    Views
  • 6
    Participants
Last reply from:
Kuyamad01

Trending Topics

Online now

Members online
1,178
Guests online
2,594
Total visitors
3,772

Forum statistics

Threads
2,285,579
Posts
29,031,387
Members
1,218,519
Latest member
kabalastugan3
Back
Top