Game lottery

The problem

Generate a lottery of a three digit numbers. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:

  1. If the user input matches the lottery number in the exact order, the award is $10,000.
  2. If all the digits in the user input match all the digits in the lottery number, the award is $3,000.
  3. If one digit in the user input matches a digit in the lottery number, the award is $1,000.

Breaking it down

Using the input from the user combine with lotteryNumber we need to check each digit. Using java 8 stream and predicates will help in this routine. Let's walk through each condition. First checking if there is an exact match by comparing values. Next, creating a Stream.of() using Stream.allMatch will check if each of the digits equal the digit of the user. Next, using anyMatch will check if one of the digits match. If no digits match, give the sorry message.

public static void main(String[] strings) {

    int lotteryNumber = new Random().nextInt(1000);

    Scanner input = new Scanner(System.in);
    System.out.print("Enter your lottery pick (three digits): ");
    String guess = input.next();
    input.close();

    System.out.println("The lottery number is " + lotteryNumber);

    String lotNumberAsString = String.valueOf(lotteryNumber);

    if (guess.equals(lotNumberAsString)) {

        System.out.println("Exact match: you win $10,000");
    } else if (Stream.of(lotNumberAsString.split("")).allMatch(
            checkLottoMatch(guess))) {

        System.out.println("Match all digits: you win $3,000");
    } else if (Stream.of(lotNumberAsString.split("")).anyMatch(
            checkLottoMatch(guess))) {

        System.out.println("Match one digit: you win $1,000");
    } else {

        System.out.println("Sorry, no match");
    }
}

private static Predicate<? super String> checkLottoMatch(String guess) {
    return p -> {
        String[] guessBroken = guess.split("");
        for (String x : guessBroken) {
            if (p.equalsIgnoreCase(x)) {
                return true;
            }
        }
        return false;
    };
}

Output

Enter your lottery pick (three digits): 234
The lottery number is 184
Match one digit: you win $1,000