librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit f843d3345621ba02c4dd901f63d7c420b1ffff65
parent 2ed2aebf0f0b88693c722462ac13e941d1055e88
Author: Michael Neuper <michael@michaelneuper.com>
Date:   Sat, 13 May 2023 23:34:19 +0200

Refactor

Diffstat:
Msrc/controller/Controller.java | 3+++
Msrc/controller/LoginController.java | 38+++++++++++++++++++++++++-------------
Msrc/model/Dealer.java | 4++--
Msrc/model/Deck.java | 2++
Msrc/model/Person.java | 3++-
Msrc/view/MainUI.form | 6+++---
Msrc/view/MainUI.java | 4++--
7 files changed, 39 insertions(+), 21 deletions(-)

diff --git a/src/controller/Controller.java b/src/controller/Controller.java @@ -232,6 +232,7 @@ public class Controller { /** * @return the player's current {@link Hand} */ + @Deprecated public String displayPlayerHand() { return PLAYER.printHand(); } @@ -246,6 +247,7 @@ public class Controller { /** * @return the dealer's current {@link Hand} */ + @Deprecated public String displayDealerHand() { return DEALER.printHand(); } @@ -260,6 +262,7 @@ public class Controller { /** * @return the dealer's first {@link model.Card} */ + @Deprecated public String displayDealerFirstCard() { return DEALER.printFirstHand(); } diff --git a/src/controller/LoginController.java b/src/controller/LoginController.java @@ -49,7 +49,8 @@ public class LoginController { * </ul> * * @param email the email to be validated - * @return {@code true} if the email is a valid email format, {@code false} otherwise + * @return {@code true} if the email is a valid email format, {@code false} + * otherwise */ private boolean isValidEmail(String email) { // check if email is null @@ -64,7 +65,9 @@ public class LoginController { Pattern pattern = Pattern.compile(emailRegex); // check if email matches the pattern - boolean isValid = pattern.matcher(email).matches(); + boolean isValid; + assert email != null; + isValid = pattern.matcher(email).matches(); // if email is not valid, show error message if (!isValid) { @@ -79,12 +82,12 @@ public class LoginController { * number. * * @param mobileNumber the mobile number to check - * @return {@code true} if the mobile number is a valid South African mobile number, - * {@code false} otherwise + * @return {@code true} if the mobile number is a valid South African mobile + * number, {@code false} otherwise */ private boolean isValidSouthAfricanMobileNumber(String mobileNumber) { // remove any non-digit characters from the mobile number - mobileNumber = mobileNumber.replaceAll("[^\\d]", ""); + mobileNumber = mobileNumber.replaceAll("[^\\d]", ""); // TODO: simplify to \D // check if the mobile number is of the correct length if (mobileNumber.length() != 10) { @@ -95,12 +98,14 @@ public class LoginController { // check if the mobile number starts with a valid South African country code String[] validCountryCodes = {"+27", "27", "0"}; boolean validCountryCode = false; + for (String code : validCountryCodes) { if (mobileNumber.startsWith(code)) { validCountryCode = true; break; } } + if (!validCountryCode) { JOptionPane.showMessageDialog(null, "Your country code is not South African"); return false; @@ -110,11 +115,12 @@ public class LoginController { } /** - * Checks if a given date of birth is valid and the person is at least 18 years old. + * Checks if a given date of birth is valid and the person is at least 18 + * years old. * * @param date the date of birth to be checked - * @return {@code true} if the date is valid and the person is at least 18 years - * old, {@code false} otherwise + * @return {@code true} if the date is valid and the person is at least 18 + * years old, {@code false} otherwise */ private static boolean isValidDateAndAdult(Date date) { // check if the date is null @@ -123,7 +129,7 @@ public class LoginController { return false; } - // make sure the date is in the past + // check if the date is in the past if (date.after(new Date())) { JOptionPane.showMessageDialog(null, "You were not born in the future!"); return false; @@ -135,11 +141,13 @@ public class LoginController { Calendar now = Calendar.getInstance(); now.setTime(new Date()); int age = now.get(Calendar.YEAR) - dob.get(Calendar.YEAR); + if (now.get(Calendar.MONTH) < dob.get(Calendar.MONTH) - || (now.get(Calendar.MONTH) == dob.get(Calendar.MONTH) && now.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH))) { + || (now.get(Calendar.MONTH) == dob.get(Calendar.MONTH) + && now.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH))) { age--; - } else { } + if (age < 18) { JOptionPane.showMessageDialog(null, "You need to be at least 18 years old to gamble!"); return false; @@ -158,7 +166,8 @@ public class LoginController { * </ul> * * @param password the password to be validated - * @return {@code true} if password is matches all criteria, {@code false} otherwise + * @return {@code true} if password is matches all criteria, {@code false} + * otherwise */ private boolean isValidPassword(String password) { boolean hasUpperCase = false; @@ -166,11 +175,13 @@ public class LoginController { boolean hasNumber = false; boolean hasSpecialChar = false; + // check if password is more than 10 characters if (password == null || password.length() < 10) { JOptionPane.showMessageDialog(null, "Password must be at least 10 characters long"); return false; } + // check if password has an uppercase letter, lowercase letter, a digit and a special character for (int i = 0; i < password.length(); i++) { char ch = password.charAt(i); @@ -206,7 +217,8 @@ public class LoginController { * following: !@#$%^&*()-+=[]{}|\;:'",<.>/?. * * @param ch the character to check - * @return {@code true} if the character is a special character, {@code false} otherwise + * @return {@code true} if the character is a special character, + * {@code false} otherwise */ private boolean isSpecialCharacter(char ch) { String specialCharacters = "!@#$%^&*()-+=[]{}|\\;:'\",<.>/?"; diff --git a/src/model/Dealer.java b/src/model/Dealer.java @@ -29,7 +29,7 @@ public class Dealer extends Person { * {@code String} */ public String printFirstHand() { - return "" + super.getHand().getCard(0); + return String.valueOf(super.getHand().getCard(0)); } /** @@ -40,7 +40,7 @@ public class Dealer extends Person { * {@link controller.Hand} as a {@code String} */ public String printFirstCardValue() { - return "" + super.getHand().getCard(0).getValue(); + return String.valueOf(super.getHand().getCard(0).getValue()); } } diff --git a/src/model/Deck.java b/src/model/Deck.java @@ -68,6 +68,7 @@ public class Deck { * * @param inCard the {@link Card} the be added to the deck */ + @Deprecated public void addCard(Card inCard) { this.deck.add(inCard); } @@ -150,6 +151,7 @@ public class Deck { */ @Override public String toString() { + // TODO: stop String concatenation in loop String ret = ""; // holds the string to return for (Card card : deck) { ret += card; diff --git a/src/model/Person.java b/src/model/Person.java @@ -43,6 +43,7 @@ public abstract class Person { * * @param hand the {@link Hand} to set */ + @Deprecated public void setHand(Hand hand) { this.hand = hand; } @@ -81,7 +82,7 @@ public abstract class Person { * @return a formatted {@code String} representing the person's {@link Hand} */ public String printHand() { - return this.hand + ""; + return String.valueOf(this.hand); } /** diff --git a/src/view/MainUI.form b/src/view/MainUI.form @@ -28,7 +28,7 @@ <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> - <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,86,0,0,3,-68"/> + <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,88,0,0,3,-68"/> </AuxValues> <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> @@ -148,7 +148,7 @@ <EmptySpace type="separate" max="-2" attributes="0"/> <Component id="jButtonSurrender" min="-2" max="-2" attributes="0"/> <EmptySpace min="-2" pref="18" max="-2" attributes="0"/> - <Component id="jButtonDeal" pref="102" max="32767" attributes="0"/> + <Component id="jButtonDeal" pref="90" max="32767" attributes="0"/> <EmptySpace max="-2" attributes="0"/> </Group> </Group> @@ -280,7 +280,7 @@ <Component id="jLabelCurrentBetAmount" min="-2" max="-2" attributes="0"/> <EmptySpace min="-2" pref="32" max="-2" attributes="0"/> <Component id="jButtonChangeBet" min="-2" max="-2" attributes="0"/> - <EmptySpace pref="169" max="32767" attributes="0"/> + <EmptySpace pref="163" max="32767" attributes="0"/> </Group> </Group> </DimensionLayout> diff --git a/src/view/MainUI.java b/src/view/MainUI.java @@ -190,7 +190,7 @@ public class MainUI extends javax.swing.JFrame { .addGap(18, 18, 18) .addComponent(jButtonSurrender) .addGap(18, 18, 18) - .addComponent(jButtonDeal, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE) + .addComponent(jButtonDeal, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE) .addContainerGap()) ); @@ -250,7 +250,7 @@ public class MainUI extends javax.swing.JFrame { .addComponent(jLabelCurrentBetAmount) .addGap(32, 32, 32) .addComponent(jButtonChangeBet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(169, Short.MAX_VALUE)) + .addContainerGap(163, Short.MAX_VALUE)) ); getContentPane().add(jPanelWest, java.awt.BorderLayout.WEST);