-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLottery.java
165 lines (125 loc) · 6.05 KB
/
Lottery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.*;
import javax.swing.*;
import java.util.concurrent.ThreadLocalRandom;
public class Lottery {
//Initialize all variables for the code
static int totalMatched = 0;
static double totalPrizeMoney = 0.0;
static int[] timesMatched = new int[5];
static int gamesPlayed = 0;
static final String WELCOME_MESSAGE =
"Welcome to the Pick 4 Lottery Program!\n" +
"This program simulates a pick 4 lottery.\n" +
"Choose four unique numbers and the computer will do the same.\n\n" +
"You'll gain money based on how many numbers are the same!";
static final String MENU = "Do you want to enter in a ticket (yes or no)?";
static HashMap<Integer, Double> matchTable = new HashMap<Integer, Double>();
static {
matchTable.put(0, 0.0);
matchTable.put(1, 100.00);
matchTable.put(2, 1000.00);
matchTable.put(3, 50000.00);
matchTable.put(4, 500000.00);
}
//Robust code to handle getting numbers from the user.
public static int getNumber(String message, int min, int max) {
String input;
Integer answer = -1;
message += "\n\nEnter a number between " + min + " and " + max;
do { //Show the user an input dialog with the message
input = JOptionPane.showInputDialog(null, message);
try { //Attempt to convert the input string into an integer
answer = Integer.valueOf(input);
} catch (NumberFormatException e) {
//in the console window, print out an error message
System.err.println("NumberFormatException: " + e.getMessage());
//If we can't typecast to an Integer, tell the user that they can only enter numbers
print("You didn't enter a number!");
}
if (answer < min || answer > max) {
print("The number wasn't within the bounds!");
}
} while (answer < min || answer > max); //Repeat until the integer is within the correct bounds
return answer;
}
//Generates a random integer with the specified range according to standard Java practices
public static int getRandomIntBetween(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max + 1);
}
public static double rewardMoney(Integer results) { //Rewards money and updates stats for the end of the game. Returns the money won this round of the game.
totalMatched += results;
timesMatched[results]++;
gamesPlayed++;
System.out.println(Arrays.toString(timesMatched));
System.out.println("results " + results + " this round!");
Double currentMoney = matchTable.get(results);
totalPrizeMoney += currentMoney;
System.out.println("You won " + currentMoney + " this round!");
System.out.println("You won " + totalPrizeMoney + " overall!");
return currentMoney;
}
static void print (String output) {
JOptionPane.showMessageDialog(null, output);
}
static void exit() {
//calculate and print everything
String output = "";
output += "*******************************\n";
output += " Number of games played: " + gamesPlayed + "\n";
output += "*******************************\n\n";
for (int i = 0; i < timesMatched.length; i++) {
output += "You matched " + i + " numbers " + timesMatched[i] + " times\n";
output += "Which means you matched " + i + " numbers about " + Math.round(((double) timesMatched[i] / (double) gamesPlayed)*100.00) + "% of the time\n\n";
}
output += "You won $" + totalPrizeMoney + " overall!";
print(output);
}
public static String shouldContinue(String message) {
String answer = "";
do { //Show the user an input dialog with the message
answer = JOptionPane.showInputDialog(null, message).toLowerCase();
System.out.println("answer was: " + answer);
if (!answer.equals("yes") && !answer.equals("no")) {
print("You didn't enter \"yes\" or \"no\"!");
}
} while (!answer.equals("yes") && !answer.equals("no")); //Repeat until we get a solid choice
return answer;
}
public static void main(String[] args) {
//System.out.println(matchTable);
print(WELCOME_MESSAGE);
String choice = "";
do {
choice = shouldContinue(MENU);
if (choice.equals("no")) { //the user chooses to exit
exit();
break;
} else if (choice.equals("yes")) { //the user chooses to play
Ticket userTicket = new Ticket();
Ticket computerTicket = new Ticket();
//Fill the user ticket with their inputs. Using a while loop allows for arbitrarily long tickets (ticket length changed with the TICKET_LENGTH constant in the Ticket class)
while (!userTicket.isFilled()) {
int input = getNumber("What number do you want (you can choose 4 for this ticket)?", 1, 40);
userTicket.add(input);
}
//Fill the computer ticket with random numbers. Makes sure they're all unique
while (!computerTicket.isFilled()) {
int input = getRandomIntBetween(1, 40);
if((!computerTicket.contains(input))) {
computerTicket.add(input);
System.out.println("Random Number added: " + input);
}
}
//used to test comparison code
/*
computerTicket.add(1);
computerTicket.add(2);
computerTicket.add(3);
computerTicket.add(4);
*/
Integer results = computerTicket.match(userTicket);
rewardMoney(results);
}
} while (choice.equals("yes"));
}
}