The code is missing some elements. I know there have been other similar questions asked. I just
Question:
The code is missing some elements. I know there have been other similar questions asked. I just need some assistance, an alternate method to get the code running. The lottery game is based on a graphics interface.
import javax.swing.*; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Random;
public class Project3 {
static ArrayList lottery_picks = new ArrayList<>(); static JFrame frame; static String message = "Welcome to the Lottery Game!" + " Instructions:" + " 1. Pick only up to 6 numbers from the selection below. " + " 2. Once done, you will have to confirm your picks. If you confirm, the lottery will begin." + " 3. The lottery will pick 6 non repeating winning numbers. If you match 3 or more picks, you will won a price." + " The prices are as follows:" + " 0, 1 or 2 number(s) matched: $0" + " 3 numbers matched: $100" + " 4 numbers matched: $10,000" + " 5 numbers matched: $100,000" + " 6 numbers matched: $1,000,000"+ " Good Luck!!!";
public static void main(String[] args) { frame = new JFrame(); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = 6; gbc.gridx = 1; gbc.gridy = 0;
panel.add(new JTextArea(message), gbc);
int number = 1;
for (int y = 1; y <= 5; y++) { for (int x = 1; x <= 6; x++) { gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = 1; gbc.gridx = x; gbc.gridy = y; JCheckBox checkBox = new JCheckBox(String.valueOf(number++));
checkBox.addItemListener(new CheckBoxActionHandler()); panel.add(checkBox, gbc); } }
frame.add(panel); frame.setTitle("Lottery GUI"); frame.setSize(300, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
public static class CheckBoxActionHandler implements ItemListener {
@Override public void itemStateChanged(ItemEvent e) { JCheckBox checkBox = (JCheckBox) e.getSource();
if (e.getStateChange() == ItemEvent.SELECTED) { if (lottery_picks.size() <= 6) { lottery_picks.add(Integer.parseInt(checkBox.getText())); checkBox.setSelected(true); } else {
} } else if (e.getStateChange() == ItemEvent.DESELECTED) { lottery_picks.remove((Object) Integer.parseInt(checkBox.getText())); }
if (lottery_picks.size() == 6) { System.out.println("lottery picks: " + lottery_picks.toString());
int i = JOptionPane.showConfirmDialog(frame, "Your picks are: " + lottery_picks.toString() + " Continue with this?", "Confirmation",
if (i == 0) {
int matches = getMatchNumbersCount(winning_numbers, lottery_picks);
double winning = 0; if (matches == 3) { winning = 100; } else if (matches == 4) { winning = 10000; } else if (matches == 5) { winning = 100000; } else if (matches == 6) { winning = 1000000; }
JOptionPane.showMessageDialog(frame, "The winning numbers are: " + winning_numbers + " You have picked " + matches + " number(s)." + " You won $" + winning, "Information", JOptionPane.INFORMATION_MESSAGE) ; System.out.println("winning numbers: " + getWinningNumbers()); } } } }
public static ArrayList getWinningNumbers() {
Random random = new Random();
ArrayList picked_winning_numbers = new ArrayList<>();
while (true) { int random_number = random.nextInt(30) + 1;
for (int number : picked_winning_numbers) { if (random_number == number) { isPickedAlready = true; break; } }
if (!isPickedAlready) { picked_winning_numbers.add(random_number);
if (picked_winning_numbers.size() == 6) { break; } } }
return picked_winning_numbers; }
public static int getMatchNumbersCount(ArrayList winning_numbers, ArrayList lottery_picks) {
int count = 0;
for (int pick : lottery_picks) {
if (number == pick) { count++; break; } } }
return count; } }
Foundations of Financial Management
ISBN: 978-1259024979
10th Canadian edition
Authors: Stanley Block, Geoffrey Hirt, Bartley Danielsen, Doug Short, Michael Perretta