I need some help with implementation of a GUI application with arrays and using
ID: 3546934 • Letter: I
Question
I need some help with implementation of a GUI application with arrays and using the random class.
First the user is asked how many tickets he would like to buy. Then you use Powerball rules and randomly generate 6 numbers (values 1-59 for the first 5, then the Powerball is 1-35) and compare it to the randomly generated numbers of the user (If he bought 3 tickets then it should compare 3 tickets to the 1 winning number). It should keep a record of how much the user spent in tickets vs the amount that he won.
If 1 number matches they should get $4
2 numbers they get $4
3 numbers they get $7
4 they get $100
5 they get $100,000
If all match they have won the lottery.
So far I have this started for a GUI application but the arrays are really beyond me right now
import javax.swing.*;
import java.awt.event.*;
public class PowerballGui extends JFrame
{
private JPanel panel;
private JTextField lottoTextField;
private JLabel messageLabel;
private JButton playButton;
private final int WINDOW_WIDTH = 500;
private final int WINDOW_HEIGHT = 150;
public PowerballGui()
{
setTitle("PowerBall Jackpot!");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel ("How many tickets would you like to purchase?");
lottoTextField = new JTextField(10);
playButton = new JButton("Play!");
playButton.addActionListener(new playButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(lottoTextField);
panel.add(playButton);
}
private class playButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Is this where I reference the Lottery class for action?
}
}
public static void main(String[] args)
{
new PowerballGui();
}
}
Explanation / Answer
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class PowerballGui extends JFrame {
private JPanel panel;
private JTextField lottoTextField;
private JLabel messageLabel;
private JButton playButton;
private final int WINDOW_WIDTH = 500;
private final int WINDOW_HEIGHT = 150;
private final int JACKPOT = Integer.MAX_VALUE;
private final int TICKET_PRICE = 2;
private final ArrayList<Integer> pool;
private int spent;
private int won;
private int hitJackpot;
private JLabel winNumbersLabel;
private JLabel resultLabel;
public PowerballGui() {
setTitle("PowerBall Jackpot!");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
pool = new ArrayList<Integer>(59);
for (int i = 0; i < 59; ++i) pool.add(i+1);
spent = won = 0;
hitJackpot = 0;
}
private void buildPanel() {
winNumbersLabel = new JLabel();
resultLabel = new JLabel();
messageLabel = new JLabel("How many tickets would you like to purchase?");
lottoTextField = new JTextField(10);
playButton = new JButton("Play!");
playButton.addActionListener(new playButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(lottoTextField);
panel.add(playButton);
panel.add(winNumbersLabel);
panel.add(resultLabel);
}
private class playButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Generate a new winning numbers
Powerball winNumbers = new Powerball();
winNumbersLabel.setText("Winning numbers: " + winNumbers);
try {
String input = lottoTextField.getText();
int ticketCount = Integer.parseInt(input);
for (int i = 0; i < ticketCount; ++i) {
Powerball ticket = new Powerball();
spent += TICKET_PRICE;
int winAmount = winNumbers.getWinAmount(ticket);
if (winAmount == JACKPOT) ++hitJackpot;
else won += winAmount;
}
StringBuilder s = new StringBuilder();
s.append("Spent: $" + spent + ' ');
s.append(" Won: $" + won + ' ');
s.append(" Jackpot: " + hitJackpot);
resultLabel.setText(s.toString());
} catch(NumberFormatException ex) {
System.out.println(ex);
}
}
}
private class Powerball {
private final int[] numbers;
private final int powerball;
public Powerball() {
// Generate first 5 numbers
numbers = new int[5];
// shuffle pool of 59 numbers
Collections.shuffle(pool);
// choose the first 5 numbers
for (int i = 0; i < 5; ++i)
numbers[i] = pool.get(i);
// Generate powerball
// Math.random() return random float 0.000 to 0.999
// Math.random() * 35 will yield 0.000 to 34.999
// + 1 will yield 1.000 to 35.999
// cast them to int will yield 1 to 35
powerball = (int) (Math.random() * 35 + 1);
}
public int getMatchNumbers(Powerball other) {
int match = 0;
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j)
if (numbers[i] == other.numbers[j]) { ++match; break; }
return match;
}
public boolean withPowerball(Powerball other) {
return powerball == other.powerball;
}
public int getWinAmount(Powerball other) {
int match = getMatchNumbers(other);
boolean wPowerball = withPowerball(other);
if (match == 0 && wPowerball) return 4;
if (match == 1 && wPowerball) return 4;
if (match == 2 && wPowerball) return 7;
if (match == 3 && !wPowerball) return 7;
if (match == 3 && wPowerball) return 100;
if (match == 4 && !wPowerball) return 100;
if (match == 4 && wPowerball) return 10000;
if (match == 5 && !wPowerball) return 1000000;
if (match == 5 && wPowerball) return JACKPOT;
return 0;
}
public String toString() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < 5; ++i) {
s.append(numbers[i]);
s.append(" ");
}
s.append("Powerball ");
s.append(powerball);
return s.toString();
}
}
public static void main(String[] args) {
new PowerballGui();
}
}