In the last days I've written a number guessing game that I've posted on this site.
I have now written a lottery simulation in which I tried to acknowledge the criticisms of the project mentioned above. I also tried to add error handling. If the user enters invalid numbers or complete bullshit, the program will not crash any longer.
You can test the program here. Dont wonder: The online interpreter needs a little time before it start the program.
Have I sufficiently implemented the criticism of the last Code Review? Are there new things that should be better?
Thank you for answers!
Main.java
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
LotteryGame game = new LotteryGame(6, 1, 49);
System.out.println(game.getExplanationString());
boolean run = true;
while (run) {
game.play();
System.out.print("Continue (Y / N): ");
System.out.flush();
run = scanner.nextLine().equalsIgnoreCase("Y");
System.out.println();
}
}
}
LotteryGame.java
import java.util.Scanner;
public class LotteryGame {
private Scanner scanner = new Scanner(System.in);
private final int drawSize;
private final int drawLowerLimit;
private final int drawUpperLimit;
public LotteryGame(int drawSize, int drawLowerLimit, int drawUpperLimit) {
this.drawSize = drawSize;
this.drawLowerLimit = drawLowerLimit;
this.drawUpperLimit = drawUpperLimit;
}
public void play() {
// generate new random draw
Draw draw = Draw.generateRandomDraw(drawSize, drawLowerLimit, drawUpperLimit);
// let the user guess
Draw userDraw = guess();
// compare the draws and print result
int rightNumbers = draw.compare(userDraw);
System.out.println("Your guess: " + userDraw.getStringRepresentation());
System.out.println("Draw: " + draw.getStringRepresentation());
System.out.println("You guessed " + rightNumbers + " right!");
System.out.println();
}
public Draw guess() {
int numbers[] = new int[drawSize];
while (true) {
try {
for (int i = 0; i < drawSize; i++) {
numbers[i] = HelpfulFunctions.saveIntInput("Number " + (i+1) + ": ");
}
System.out.println();
return new Draw(numbers);
} catch (IllegalArgumentException e) {
System.out.println("The numbers have to be unique\n");
}
}
}
public String getExplanationString() {
StringBuilder sb = new StringBuilder();
sb.append("You have to guess the numbers of a lottery draw.\n");
sb.append("A draw consists of 6 different numbers.\n");
sb.append("Each number is in a range between 1 and 49.\n");
sb.append("The more numbers you guess right, the luckier you can be!\n");
return sb.toString();
}
}
Draw.java
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Draw {
public int[] numbers;
public Draw(int[] numbers) throws IllegalArgumentException {
// check if all numbers are unique
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers.length; j++) {
if (i == j) continue;
if (numbers[i] == numbers[j]) {
throw new IllegalArgumentException("All numbers have to be unique.");
}
}
}
this.numbers = numbers;
}
public static Draw generateRandomDraw(int numberOfEntries, int lowerLimit, int upperLimit) {
// generate list that contains possible values
List<Integer> possibleValues = new ArrayList<>();
for (int i = lowerLimit; i <= upperLimit; i++) {
possibleValues.add(i);
}
// fill draw with randomly picked values
int[] numbers = new int[numberOfEntries];
Random random = new Random();
for (int i = 0; i < numberOfEntries; i++) {
int randomIndex = random.nextInt(possibleValues.size());
Integer pickedValue = possibleValues.get(randomIndex);
possibleValues.remove(pickedValue);
numbers[i] = pickedValue;
}
Arrays.sort(numbers);
return new Draw(numbers);
}
// returns count of equal numbers
public int compare(Draw draw) throws IllegalArgumentException {
if (draw.numbers.length != numbers.length) {
throw new IllegalArgumentException("The draws dont have the same length.");
}
int count = 0;
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers.length; j++) {
if (numbers[i] == draw.numbers[j]) {
count++;
}
}
}
return count;
}
public String getStringRepresentation() {
StringBuilder sb = new StringBuilder();
sb.append("[" + numbers[0]);
for (int i = 1; i < numbers.length; i++) {
sb.append(", " + numbers[i]);
}
sb.append("]");
return sb.toString();
}
}
HelpfulFunctions.java
import java.util.Scanner;
public class HelpfulFunctions {
private static Scanner scanner = new Scanner(System.in);
public static int saveIntInput(String message) {
while (true) {
try {
System.out.print(message);
System.out.flush();
return Integer.valueOf(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Not a valid number.");
}
}
}
}