Skip to content

Commit 96672fb

Browse files
authoredNov 3, 2020
Add files via upload
1 parent ed73c0e commit 96672fb

10 files changed

+1559
-0
lines changed
 

‎Charlie.java

+583
Large diffs are not rendered by default.

‎Date.java

+260
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
2+
import javax.swing.JOptionPane;
3+
4+
public class Date {
5+
6+
private int day;
7+
private int month;
8+
private int year;
9+
10+
public Date() {
11+
makeDate();
12+
}
13+
14+
public Date(int day, int month, int year) {
15+
16+
setDay(day);
17+
setMonth(month);
18+
setYear(year);
19+
}
20+
21+
public int getDay() {
22+
return day;
23+
}
24+
25+
public void setDay(int day) {
26+
27+
String propmt = "Type here";
28+
String message = "";
29+
30+
//makes sure that the limit of dates is not violated
31+
switch(month) {
32+
//Special case
33+
34+
case 2: //28 or 29 days
35+
boolean leap = isLeapYear();
36+
37+
//if year is leap year
38+
if(leap == true) {
39+
while(day > 29 || day < 1) {
40+
message = "Please enter a valid number between 1 and 29...";
41+
day = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
42+
}
43+
44+
}
45+
else {//if year is not leap year
46+
while(day > 28 || day < 1) {
47+
message = "Please enter a valid number between 1 and 28...";
48+
day = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
49+
}
50+
}
51+
break;
52+
case 1://31 days
53+
case 3://31 days
54+
case 5://31 days
55+
case 7://31 days
56+
case 8://31 days
57+
case 10://31 days
58+
case 12://31 days
59+
while(day > 31 || day < 1) {
60+
message = "Please enter a valid number for the month selected between 1 and 31...";
61+
day = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
62+
}
63+
break;
64+
65+
default:
66+
while(day > 30 || day < 1) {
67+
message = "Please enter a valid number for the month selected between 1 and 30...";
68+
day = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
69+
}
70+
break;
71+
}
72+
this.day = day;
73+
}
74+
75+
public int getMonth() {
76+
return month;
77+
}
78+
79+
public void setMonth(int month) {
80+
81+
String message = "Please enter the month of your birth";
82+
String prompt = "Type here";
83+
boolean error = false;
84+
85+
//month can only be between 1 and 12
86+
while(month > 12 || month < 1) {
87+
88+
JOptionPane.showMessageDialog(null, "Please enter a number between 1 and 12 for month");
89+
do {
90+
91+
try {//make sure the input entered is valid
92+
month = Integer.parseInt(JOptionPane.showInputDialog(message, prompt));
93+
error = false;
94+
95+
}catch(java.lang.NumberFormatException e) {
96+
JOptionPane.showMessageDialog(null, "Please enter a valid integer for month");
97+
error = true;
98+
}
99+
}while(error == true);
100+
101+
}
102+
this.month = month;
103+
}
104+
105+
public int getYear() {
106+
return year;
107+
}
108+
109+
public void setYear(int year) {
110+
111+
this.year = year;
112+
}
113+
114+
public String toString() {
115+
116+
String result;
117+
if(month < 10) {
118+
result = "0" + month + "/";
119+
}
120+
else {
121+
result = month + "/";
122+
}
123+
if(day < 10) {
124+
result += "0" + day + "/" + year;
125+
}
126+
else {
127+
result += day + "/" + year;
128+
}
129+
return result;
130+
}
131+
/////////////////////////////////////////////////////////////////////////////////////////////
132+
public void makeDate() {
133+
134+
//variables
135+
String userInput = "";
136+
int integerMonth = 0;
137+
int integerYear = 0;
138+
int integerDay = 0;
139+
140+
//return a valid string of 10 digits
141+
userInput = getValidDateString();
142+
143+
//parse string to get month, day and year in different variables as a integer
144+
//month
145+
integerMonth = validateMonth(userInput, integerMonth);
146+
setMonth(integerMonth);
147+
148+
//year
149+
integerYear = validateYear(userInput, integerYear);
150+
setYear(integerYear);
151+
this.year = integerYear;//called this here to make leap year constrains work
152+
153+
//day
154+
integerDay = validateDay(userInput, integerDay);
155+
setDay(integerDay);
156+
157+
}
158+
//////////////////////////////////////////////////////////////////////////////////////////////
159+
public String getValidDateString() {
160+
161+
String userInput = "";
162+
int userInputLength = 0;
163+
String message = "";
164+
boolean error = false;
165+
//gets the date string of with a length of 10 characters
166+
do {
167+
try {
168+
message = "";
169+
message = "Enter your birth date (mm/dd/yyyy): ";
170+
userInput = JOptionPane.showInputDialog(message);
171+
userInputLength = userInput.length();
172+
173+
//makes sure it is entered in the correct format
174+
if(userInputLength != 10 || userInput.charAt(5) != '/' || userInput.charAt(2) != '/') {
175+
message = "Please enter a date in the format of 'MM/DD/YYYY'";
176+
JOptionPane.showMessageDialog(null, message);
177+
error = true;
178+
}
179+
else {
180+
//it makes sure that element corresponding MM, DD and YYYY are all digits before going to each setter
181+
if((Character.isDigit(userInput.charAt(0)) && Character.isDigit(userInput.charAt(1))
182+
&& Character.isDigit(userInput.charAt(3)) && Character.isDigit(userInput.charAt(4))
183+
&& Character.isDigit(userInput.charAt(6)) && Character.isDigit(userInput.charAt(7))
184+
&& Character.isDigit(userInput.charAt(8)) && Character.isDigit(userInput.charAt(9))) == false) {
185+
186+
message = "Please enter valid numbers in the format of 'MM/DD/YYYY'";
187+
JOptionPane.showMessageDialog(null, message);
188+
error = true;
189+
}else {
190+
error = false;
191+
}
192+
}
193+
}catch(java.lang.NullPointerException e) {
194+
JOptionPane.showMessageDialog(null, "Enter a valid selection");
195+
error = true;
196+
}
197+
}while(error == true);
198+
199+
return userInput;
200+
}
201+
202+
////////////////////////////////////////////////////////////////////////////////////////
203+
public boolean isLeapYear() {
204+
205+
boolean isLeap = false;
206+
207+
//if divisible by 4 check if divisible by 100
208+
if(year % 4 == 0) {
209+
210+
//if divisible by 100 check if divisible by 400
211+
if(year % 100 == 0) {
212+
213+
//if divisible by 400
214+
if(year % 400 == 0) {
215+
isLeap = true;
216+
}
217+
//if not, not a leap year
218+
else {
219+
isLeap = false;
220+
}
221+
}
222+
//if only divisible by four, its a leap year
223+
else {
224+
isLeap = true;
225+
}
226+
}
227+
//if not divisible by 4, not a leap year
228+
else {
229+
isLeap = false;
230+
}
231+
232+
return isLeap;
233+
}
234+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
235+
public int validateMonth(String userInput, int integerMonth) {
236+
237+
String month = "" + userInput.substring(0,2);
238+
integerMonth = Integer.parseInt(month);
239+
240+
return integerMonth;
241+
}//end of validateMonth
242+
243+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
244+
public int validateYear(String userInput, int integerYear) {
245+
246+
String year = "" + userInput.substring(6,10);
247+
integerYear = Integer.parseInt(year);
248+
249+
return integerYear;
250+
}//end of validateYear
251+
252+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
253+
public int validateDay(String userInput, int integerDay) {
254+
255+
String day = "" + userInput.substring(3,5);
256+
integerDay = Integer.parseInt(day);
257+
258+
return integerDay;
259+
}//end of validateDay
260+
}

‎DateUML.png

31.8 KB
Loading

‎DesignTool.jpg

525 KB
Loading

‎Name.java

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
2+
import javax.swing.JOptionPane;
3+
4+
5+
public class Name {
6+
7+
private String firstName;
8+
private char middleInitial;
9+
private String lastName;
10+
11+
public Name() {
12+
getName();
13+
}
14+
15+
public Name(String firstName) {
16+
17+
}
18+
19+
public Name(String firstName, char middleInitial, String lastName) {
20+
21+
this.firstName = firstName;
22+
this.middleInitial = middleInitial;
23+
this.lastName = lastName;
24+
}
25+
26+
public String getFirstName() {
27+
return firstName;
28+
}
29+
30+
public void setFirstName(String firstName) {
31+
32+
//capitalize the first letter of first name and leaves the rest in lower case
33+
firstName = firstName.substring(0, 1).toUpperCase() + firstName.substring(1).toLowerCase();
34+
35+
this.firstName = firstName;
36+
}
37+
38+
public char getMiddleInitial() {
39+
return middleInitial;
40+
}
41+
42+
public void setMiddleInitial(char middleInitial) {
43+
44+
//checks to see if the player has a middle name or not
45+
if(middleInitial == 'X') {
46+
middleInitial = ' ';
47+
48+
}
49+
50+
this.middleInitial = middleInitial;
51+
}
52+
53+
54+
public String getLastName() {
55+
return lastName;
56+
}
57+
58+
59+
public void setLastName(String lastName) {
60+
61+
//capitalize the first letter of last name and leaves the rest in lower case
62+
lastName = lastName.substring(0, 1).toUpperCase() + lastName.substring(1).toLowerCase();
63+
64+
this.lastName = lastName;
65+
}
66+
67+
public String toString() {
68+
69+
String message = "";
70+
71+
//if middle name is inexistent, print a blank space
72+
if(middleInitial == ' ') {
73+
message += firstName + middleInitial + lastName;
74+
}
75+
else {//if exists, print first letter followed by a dot
76+
message += firstName + " " + middleInitial +"." + " " + lastName;
77+
}
78+
79+
return message;
80+
}
81+
82+
public void getName() {
83+
84+
firstName = validateFirstName(firstName);
85+
setFirstName(firstName);
86+
87+
middleInitial = validateMiddleInitial(middleInitial);
88+
setMiddleInitial(middleInitial);
89+
90+
lastName = validateLastName(lastName);
91+
setLastName(lastName);
92+
93+
}
94+
//checks every char in a string with the regex expression and makes sure all of them are alphabet letters
95+
//I added the ' sign and 'space' to be a valid char because many names use it
96+
public static boolean isStringOnlyAlphabet(String name) {
97+
98+
return ((!name.equals("")) && (name != null) && (name.matches("^[a-zA-Z'\' ']*$")));
99+
}
100+
101+
//validate first name
102+
public static String validateFirstName(String firstName) {
103+
104+
String message = "What is your first name: ";
105+
boolean isOnlyAlphabet = true;
106+
107+
do{
108+
try {//catches if user clics cancel or X
109+
firstName = JOptionPane.showInputDialog(message);
110+
111+
//checks if all elements of the string are letters
112+
isOnlyAlphabet = isStringOnlyAlphabet(firstName);
113+
114+
//lets the user know they did not enter letters
115+
if(isOnlyAlphabet == false) {
116+
JOptionPane.showMessageDialog(null, "Please enter a name that contains only letters.");
117+
}
118+
else {
119+
//if they entered space as the first letter, make them enter the name again
120+
if(firstName.charAt(0) == ' ') {
121+
JOptionPane.showMessageDialog(null, "First letter of your name cannot be a space");
122+
isOnlyAlphabet = false;
123+
}
124+
}
125+
}catch(java.lang.NullPointerException e) {
126+
JOptionPane.showMessageDialog(null, "Please enter a valid selection");
127+
isOnlyAlphabet = false;
128+
}
129+
130+
}while(isOnlyAlphabet == false);
131+
132+
return firstName;
133+
}
134+
135+
//validate middleInitial
136+
public static char validateMiddleInitial(char middleInitial) {
137+
138+
String message ="What is your middle name: Enter X if inexistent ";
139+
String tempString = "";
140+
boolean isOnlyAlphabet = true;
141+
142+
do{
143+
try {//catches if user clics cancel or X
144+
145+
//take all of the characters entered to check if they are all letters
146+
tempString = JOptionPane.showInputDialog(message);
147+
148+
//checks if all elements of the string are letters
149+
isOnlyAlphabet = isStringOnlyAlphabet(tempString);
150+
151+
//lets the user know they did not enter letters
152+
if(isOnlyAlphabet == false) {
153+
JOptionPane.showMessageDialog(null, "Please enter a name that contains only letters.");
154+
}
155+
else {// if they are all letters, take the first letter
156+
157+
//if they entered space as the first letter, make them enter the name again
158+
if(tempString.charAt(0) == ' ') {
159+
JOptionPane.showMessageDialog(null, "First letter of your name cannot be a space");
160+
isOnlyAlphabet = false;
161+
}
162+
else {
163+
middleInitial = tempString.toUpperCase().charAt(0);
164+
}
165+
}
166+
167+
}catch(java.lang.NullPointerException e) {
168+
JOptionPane.showMessageDialog(null, "Please enter a valid selection");
169+
isOnlyAlphabet = false;
170+
}
171+
172+
}while(isOnlyAlphabet == false);
173+
174+
return middleInitial;
175+
}
176+
177+
//validate last Name
178+
public static String validateLastName(String lastName) {
179+
180+
String message = "What is your last name: ";
181+
boolean isOnlyAlphabet = true;
182+
183+
do{
184+
try {//catches if user clics cancel or X
185+
lastName = JOptionPane.showInputDialog(message);
186+
187+
//checks if all elements of the string are letters
188+
isOnlyAlphabet = isStringOnlyAlphabet(lastName);
189+
190+
//lets the user know they did not enter letters
191+
if(isOnlyAlphabet == false) {
192+
JOptionPane.showMessageDialog(null, "Please enter a name that contains only letters.");
193+
}
194+
else {
195+
//if they entered space as the first letter, make them enter the name again
196+
if(lastName.charAt(0) == ' ') {
197+
JOptionPane.showMessageDialog(null, "First letter of your name cannot be a space");
198+
isOnlyAlphabet = false;
199+
}
200+
}
201+
}catch(java.lang.NullPointerException e) {
202+
JOptionPane.showMessageDialog(null, "Please enter a valid selection");
203+
isOnlyAlphabet = false;
204+
}
205+
206+
}while(isOnlyAlphabet == false);
207+
208+
return lastName;
209+
}
210+
211+
}

‎NameUML.png

39.2 KB
Loading

‎Player.java

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
import java.text.DecimalFormat;
3+
import javax.swing.JOptionPane;
4+
5+
public class Player {
6+
7+
private Name name;
8+
private Date dob;
9+
private int moneyBalance;
10+
11+
12+
public Player() {
13+
makePlayer();
14+
}
15+
16+
public Player(Name name, Date dob, int moneyBalance) {
17+
18+
setName(name);
19+
setDob(dob);
20+
setMoneyBalance(moneyBalance);
21+
22+
}
23+
24+
public Name getName() {
25+
return name;
26+
}
27+
28+
public void setName(Name name) {
29+
this.name = name;
30+
}
31+
32+
public Date getDob() {
33+
return dob;
34+
}
35+
36+
public void setDob(Date dob) {
37+
this.dob = dob;
38+
}
39+
40+
public int getMoneyBalance() {
41+
return moneyBalance;
42+
}
43+
44+
public void setMoneyBalance(int moneyBalance) {
45+
46+
String message = "";
47+
String prompt = "Type Here";
48+
49+
//if money available is negative ask for a positive value
50+
while(moneyBalance <= 0) {
51+
52+
//make sure that the answer is a valid input
53+
try {
54+
moneyBalance = Integer.parseInt(JOptionPane.showInputDialog("Enter a POSITIVE money balance", prompt));
55+
56+
if(moneyBalance < 1) {
57+
JOptionPane.showMessageDialog(null, "Please enter a Positive integer.");
58+
}
59+
60+
}catch(java.lang.NumberFormatException e) {
61+
message = "Please enter a valid integer";
62+
JOptionPane.showMessageDialog(null, message);
63+
}
64+
}
65+
66+
this.moneyBalance = moneyBalance;
67+
}
68+
69+
public String toString() {
70+
71+
//format for money balance
72+
DecimalFormat df = new DecimalFormat("#,###,###$");
73+
String message = "";
74+
75+
//player information displayed
76+
message += "Player's Name: "+name+"\n";
77+
message += "Player's Date of Birth: "+dob+"\n";
78+
message += "Player's Money Balance: "+ df.format(moneyBalance) +"\n";
79+
80+
return message;
81+
}
82+
83+
public void makePlayer() {
84+
85+
boolean exit = false;
86+
String message = "";
87+
String propmt = "Type Here";
88+
89+
//generates a name for the player
90+
name = new Name();
91+
setName(name);
92+
93+
//generates a date of birth for the player
94+
dob = new Date();
95+
setDob(dob);
96+
97+
//adds money balance to the player
98+
do {
99+
//makes sure a valid number is entered
100+
try {
101+
message = "";
102+
message = "Enter your money balance";
103+
moneyBalance = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
104+
exit = false;
105+
106+
}catch(java.lang.NumberFormatException e) {
107+
message = "Please enter a valid number for money balance";
108+
JOptionPane.showMessageDialog(null, message);
109+
exit = true;
110+
}
111+
112+
}while(exit == true);
113+
114+
//validates money balances to make sure it is > 0
115+
setMoneyBalance(moneyBalance);
116+
}
117+
118+
}// end Player

‎PlayerUML.png

25.3 KB
Loading

‎SlotMachine.java

+387
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
import java.text.DecimalFormat;
2+
3+
import javax.swing.JOptionPane;
4+
5+
//coded by Jean and Anh
6+
public class SlotMachine {
7+
8+
//format for money ammounts
9+
DecimalFormat df = new DecimalFormat("#,###,###$");
10+
//format for quantity ammounts
11+
DecimalFormat df1 = new DecimalFormat("#,###,###");
12+
13+
private String name;
14+
private int machineBalance;
15+
private int jackpotOdds;
16+
private int jackpotPayout;
17+
private int regularOdds;
18+
private int regularPayouts;
19+
private int numJackpots;
20+
private int numRegs;
21+
22+
public SlotMachine() {
23+
makeSlotMachine();
24+
}
25+
26+
public SlotMachine(String name, int machineBalance, int jackpotOdds, int jackpotPayout, int regularOdds, int regularPayouts, int numJackpots, int numRegs) {
27+
28+
setName(name);
29+
setMachineBalance(machineBalance);
30+
setJackpotOdds(jackpotOdds);
31+
setJackpotPayout(jackpotPayout);
32+
setRegularOdds(regularOdds);
33+
setRegularPayouts(regularPayouts);
34+
setNumJackpots(numJackpots);
35+
setNumRegs(numRegs);
36+
37+
}
38+
39+
public SlotMachine(String name, int machineBalance, int jackpotOdds, int jackpotPayout, int regularOdds, int regularPayouts) {
40+
41+
setName(name);
42+
setMachineBalance(machineBalance);
43+
setJackpotOdds(jackpotOdds);
44+
setJackpotPayout(jackpotPayout);
45+
setRegularOdds(regularOdds);
46+
setRegularPayouts(regularPayouts);
47+
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public void setName(String name) {
55+
56+
//capitalize the first letter of name and leave the rest in lower case
57+
name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
58+
59+
this.name = name;
60+
}
61+
62+
public int getMachineBalance() {
63+
return machineBalance;
64+
}
65+
66+
public void setMachineBalance(int machineBalance) {
67+
68+
String prompt = "Type Here";
69+
70+
//ask machine balance again if user enter a negative number
71+
while(machineBalance <= 0) {
72+
73+
try {//makes sure input is valid
74+
machineBalance = Integer.parseInt(JOptionPane.showInputDialog("Enter a POSITIVE value for Machine balance:",prompt));
75+
76+
if(machineBalance < 1) {
77+
JOptionPane.showMessageDialog(null, "Please enter a Positive integer.");
78+
}
79+
80+
}catch(java.lang.NumberFormatException e) {
81+
JOptionPane.showMessageDialog(null, "Please enter a valid integer");
82+
}
83+
}
84+
85+
this.machineBalance = machineBalance;
86+
}
87+
88+
public int getJackpotOdds() {
89+
return jackpotOdds;
90+
}
91+
92+
public void setJackpotOdds(int jackpotOdds) {
93+
94+
String prompt = "Type Here";
95+
96+
//ask machine balance again if user enter a negative number
97+
while(jackpotOdds < 1) {
98+
99+
try {//makes sure input is valid
100+
jackpotOdds = Integer.parseInt(JOptionPane.showInputDialog("Enter a POSITIVE value for jackpot odds:",prompt));
101+
102+
}catch(java.lang.NumberFormatException e) {
103+
JOptionPane.showMessageDialog(null, "Please enter a valid integer");
104+
}
105+
}
106+
107+
this.jackpotOdds = jackpotOdds;
108+
}
109+
110+
public int getJackpotPayout() {
111+
return jackpotPayout;
112+
}
113+
114+
public void setJackpotPayout(int jackpotPayout) {
115+
116+
String prompt = "Type Here";
117+
118+
while(jackpotPayout < 1) {
119+
120+
try {//makes sure input is valid
121+
jackpotPayout = Integer.parseInt(JOptionPane.showInputDialog("Enter a POSITIVE value for jackpot payout:",prompt));
122+
123+
}catch(java.lang.NumberFormatException e) {
124+
JOptionPane.showMessageDialog(null, "Please enter a valid integer");
125+
}
126+
}
127+
128+
this.jackpotPayout = jackpotPayout;
129+
}
130+
131+
public int getRegularOdds() {
132+
return regularOdds;
133+
}
134+
135+
public void setRegularOdds(int regularOdds) {
136+
137+
String prompt = "Type Here";
138+
139+
while(regularOdds < 1) {
140+
141+
try {//makes sure input is valid
142+
regularOdds = Integer.parseInt(JOptionPane.showInputDialog("Enter a POSITIVE value for regular odds:",prompt));
143+
144+
}catch(java.lang.NumberFormatException e) {
145+
JOptionPane.showMessageDialog(null, "Please enter a valid integer");
146+
}
147+
}
148+
149+
this.regularOdds = regularOdds;
150+
}
151+
152+
public int getRegularPayouts() {
153+
return regularPayouts;
154+
}
155+
156+
public void setRegularPayouts(int regularPayouts) {
157+
158+
String prompt = "Type Here";
159+
160+
while(regularPayouts < 1) {
161+
162+
try {//makes sure input is valid
163+
regularPayouts = Integer.parseInt(JOptionPane.showInputDialog("Enter a POSITIVE value for regular payout:",prompt));
164+
165+
}catch(java.lang.NumberFormatException e) {
166+
JOptionPane.showMessageDialog(null, "Please enter a valid integer");
167+
}
168+
}
169+
170+
this.regularPayouts = regularPayouts;
171+
}
172+
173+
public int getNumJackpots() {
174+
return numJackpots;
175+
}
176+
177+
178+
public void setNumJackpots(int numJackpots) {
179+
this.numJackpots = numJackpots;
180+
}
181+
182+
public int getNumRegs() {
183+
return numRegs;
184+
}
185+
186+
public void setNumRegs(int numRegs) {
187+
this.numRegs = numRegs;
188+
}
189+
190+
191+
public String toString() {
192+
193+
194+
String message = "";
195+
196+
message += "Slot Machine's name: "+name+"\n";
197+
message += "Slot Machine's Balance: "+df.format(machineBalance)+"\n";
198+
message += "Slot Machine's Jackpot Odds: 1 in "+df1.format(jackpotOdds)+"\n";
199+
message += "Slot Machine's Jackpot Payout: "+df.format(jackpotPayout)+"\n";
200+
message += "Slot Machine's Regular Odds: 1 in "+df1.format(regularOdds)+"\n";
201+
message += "Slot Machine's Regular Payout: "+df.format(regularPayouts)+"\n";
202+
203+
return message;
204+
}
205+
206+
public void makeSlotMachine() {
207+
208+
//validation is not neccesary for name
209+
name = validateName(name);
210+
setName(name);
211+
212+
//enter valid machine balance
213+
machineBalance = validateMachineBalance(machineBalance);
214+
setMachineBalance(machineBalance);
215+
216+
//enter valid jackpot odds value
217+
jackpotOdds = validateJackpotOdds(jackpotOdds);
218+
setJackpotOdds(jackpotOdds);
219+
220+
//enter valid jackpot payout value
221+
jackpotPayout = validateJackpotPayout(jackpotPayout);
222+
setJackpotPayout(jackpotPayout);
223+
224+
//enter valid regular odds value
225+
regularOdds = validateRegularOdds(regularOdds);
226+
setRegularOdds(regularOdds);
227+
228+
//enter valid regular payout value
229+
regularPayouts = validateRegularPayout(regularPayouts);
230+
setRegularPayouts(regularPayouts);
231+
232+
}//end of makeSlotMachine
233+
234+
//checks every char in a string with the regex expression and makes sure all of them are alphanumeric characters
235+
public static boolean isStringOnlyAlphanumeric(String name) {
236+
237+
return ((!name.equals("")) && (name != null) && (name.matches("^[a-zA-Z0-9]*$")));
238+
}
239+
240+
//make sure the name of the slot machine is alphanumeric
241+
public String validateName(String name) {
242+
243+
String propmt = "Type here";
244+
String message = "Enter the name of the Slot Machine:";
245+
boolean isOnlyAlphanumeric;
246+
boolean error = false;
247+
248+
do {
249+
try {
250+
name = JOptionPane.showInputDialog(message, propmt);
251+
252+
//checks for only alphanumeric values
253+
isOnlyAlphanumeric = isStringOnlyAlphanumeric(name);
254+
255+
//if only alphanumeric is entered, move on if not let the user know
256+
if(isOnlyAlphanumeric == false) {
257+
JOptionPane.showMessageDialog(null, "Please enter a name that contains only letters and numbers.");
258+
error = true;
259+
}
260+
else {
261+
error = false;
262+
}
263+
264+
}catch(java.lang.NullPointerException e) {
265+
JOptionPane.showMessageDialog(null, "Please enter a valid selection");
266+
error = true;
267+
}
268+
}while(error == true);
269+
270+
return name;
271+
}
272+
273+
//validate machine balance
274+
public int validateMachineBalance(int machineBalance) {
275+
276+
String propmt = "Type here";
277+
String message = "Enter the money balance of the Slot Machine:";
278+
279+
boolean error = false;
280+
281+
//handle format mismatch exception
282+
do {
283+
try {
284+
machineBalance = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
285+
error = false;
286+
287+
}catch(java.lang.NumberFormatException e) {
288+
JOptionPane.showMessageDialog(null, "Please enter a valid integer.");
289+
error = true;
290+
}
291+
}while(error == true);
292+
293+
return machineBalance;
294+
}
295+
296+
//validate jackpot odds
297+
public int validateJackpotOdds(int jackpotOdds) {
298+
299+
String propmt = "Type here";
300+
String message = "Enter the jackpot Odds of the Slot Machine:";
301+
302+
boolean error = false;
303+
304+
do {
305+
//handle format mismatch exception
306+
try {
307+
jackpotOdds = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
308+
error = false;
309+
310+
}catch(java.lang.NumberFormatException e) {
311+
JOptionPane.showMessageDialog(null, "Please enter a valid integer.");
312+
error = true;
313+
}
314+
}while(error == true);
315+
316+
return jackpotOdds;
317+
}
318+
319+
//validate jackpot payouts
320+
public int validateJackpotPayout(int jackpotPayout) {
321+
322+
String propmt = "Type here";
323+
String message = "Enter the jackpot payout of the Slot Machine:";
324+
325+
boolean error = false;
326+
327+
do {
328+
//handle format mismatch exception
329+
try {
330+
jackpotPayout = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
331+
error = false;
332+
333+
}catch(java.lang.NumberFormatException e) {
334+
JOptionPane.showMessageDialog(null, "Please enter a valid integer.");
335+
error = true;
336+
}
337+
}while(error == true);
338+
339+
return jackpotPayout;
340+
}
341+
342+
//validate regular odds
343+
public int validateRegularOdds(int regularOdds) {
344+
345+
String propmt = "Type here";
346+
String message = "Enter the regular Odds of the Slot Machine:";
347+
348+
boolean error = false;
349+
350+
do {
351+
//handle format mismatch exception
352+
try {
353+
regularOdds = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
354+
error = false;
355+
356+
}catch(java.lang.NumberFormatException e) {
357+
JOptionPane.showMessageDialog(null, "Please enter a valid integer.");
358+
error = true;
359+
}
360+
}while(error == true);
361+
362+
return regularOdds;
363+
}
364+
365+
//validate regular payouts
366+
public int validateRegularPayout(int regularPayout) {
367+
368+
String propmt = "Type here";
369+
String message = "Enter the regular payout of the Slot Machine:";
370+
371+
boolean error = false;
372+
373+
do {
374+
//handle format mismatch exception
375+
try {
376+
regularPayout = Integer.parseInt(JOptionPane.showInputDialog(message, propmt));
377+
error = false;
378+
379+
}catch(java.lang.NumberFormatException e) {
380+
JOptionPane.showMessageDialog(null, "Please enter a valid integer.");
381+
error = true;
382+
}
383+
}while(error == true);
384+
385+
return regularPayout;
386+
}
387+
}//end of SlotMachine

‎SlotMachineUML.png

67.8 KB
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.