+1 (315) 557-6473 

Program To Create a Lottery System in C++ Language Assignment Solution.


Instructions

Objective

Write a program to create a lottery system in C++ language.

Requirements and Specifications

Advanced Java Lottery Assignment

200 Points

Due Date March 16 (nothing late accepted)

In this assignment you are to simulate a pick 4 lottery game. All methods and all data are to be members of a new user defined class called lottery. You must use dialog boxes for all data input.

This program will have 2 sections within the code:

Section A

You are to do the following:

  1.  Create a method to prompt the user to pick 4 numbers between 0 and 40 (inclusive).
  2. Create a method to have the computer generate 4 random winning numbers between 0 and 40 (inclusive).
  3. Create a method to check for matches of the user numbers with the computer generated numbers:
    • 0 numbers matched, 1 number matched, 2 numbers matched, 3 numbers matched and 4 numbers matched. You will have to keep track of these 5 counts as well. 
  1. Create a method to print the following on the screen:

The Player’s numbers, The computer generated numbers and the count of the numbers matched. So each time the game is played, using System.out statements, your output could look like this:

Player #                      Winner #                      # Matched

  48 32 11 56 78         28 47 11 78 48         3

At the end of the 10 games played, you are also to print the counts of 0,1,2,3,and 4 matches.

Section B

In this section of the program, you are going to use some of the code you already completed above, except you will allow the computer to generate the user’s numbers along with the winning numbers. You will also run the game 500 times and write the output to a text file. Therefore you will:

  1.  Create a method to have the computer generate 4 numbers for the player
  2. Use the code from method #2 above for generating the 4 willing numbers
  3.  Use the code from method #3 above for checking for a match
  4. Create a method to write all of the required data in method #4 above to a text file.

Please note:

  • Examples of Writing to a text data file is included with this assignment
  • Examples of generating Random number is included with this assignment

Source Code and Solution

import javax.swing.*;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.*;

import java.util.stream.Collectors;

public class lottery {

    private static final int N = 4;

    private static final int LOWER_BOUND = 0;

    private static final int UPPER_BOUND = 40;

    public List promptNumbers() {

        List result = new ArrayList<>();

        int curr = 0;

        while (curr < N) {

            try {

                String line = JOptionPane.showInputDialog("Please, input integer #" + (curr + 1) + " in range [" + LOWER_BOUND + ", " + UPPER_BOUND + "]: )");

                int parsed = Integer.parseInt(line);

                if (parsed < LOWER_BOUND || parsed > UPPER_BOUND || result.contains(parsed)) {

                    throw new IllegalArgumentException();

                }

                result.add(parsed);

                curr++;

            } catch (Exception e) {

                JOptionPane.showMessageDialog(null, "Invalid input");

            }

        }

        return result;

    }

    public List promptComputer() {

        return generateNumbers();

    }

    public List generateNumbers() {

        List result = new ArrayList<>();

        Random random = new Random();

        int curr = 0;

        while (curr < N) {

            int a = random.nextInt((UPPER_BOUND - LOWER_BOUND) + 1) + LOWER_BOUND;

            if (!result.contains(a)) {

                result.add(a);

                curr++;

            }

        }

        return result;

    }

    public int matches(List comb1, List comb2) {

        int count = 0;

        for (int a : comb1) {

            if (comb2.contains(a)) {

                count++;

            }

        }

        return count;

    }

    public void printResult(int tries, PrintWriter pw) {

        Map result = new HashMap<>();

        for (int i = 0; i < tries; i++) {

            List comb1 = promptComputer();

            List comb2 = generateNumbers();

            int matches = matches(comb1, comb2);

            pw.printf("%-20s%-20s%-20s%n", "Player #", "Winner #", "# Matched");

            pw.printf("%-20s%-20s%-20s%n", comb1.stream().map(j -> Integer.toString(j)).collect(Collectors.joining(" ")),

                    comb2.stream().map(j -> Integer.toString(j)).collect(Collectors.joining(" ")), matches);

            result.merge(matches, 1, Integer::sum);

        }

        pw.println();

        for (int i = 0; i <= N; i++) {

            pw.println("Match " + i + ": " + result.getOrDefault(i, 0));

        }

    }

    public static void main(String[] args) throws IOException {

        String filename = "output.txt";

        try (PrintWriter pw = new PrintWriter(filename)) {

            lottery lot = new lottery();

            lot.printResult(500, pw);

        }

    }

}