librejack

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

commit 7857cfbe824d80195a9e6327d798fd5fc8400901
parent 87bc044bb666da17a632707ef8c86a1cf99f89f1
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date:   Sun, 12 Feb 2023 22:41:20 +0200

Change formatting

Diffstat:
Msrc/controller/LoginController.java | 95+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/view/LoginGUI.java | 8+++-----
2 files changed, 52 insertions(+), 51 deletions(-)

diff --git a/src/controller/LoginController.java b/src/controller/LoginController.java @@ -13,9 +13,10 @@ public class LoginController { public LoginController() { } + /** * Checks whether all the entered fields are present and valid - * + * * @param email * @param mobileNumber * @param date @@ -23,22 +24,23 @@ public class LoginController { * @return true if all fields are valid, false otherwise */ public boolean areAllFieldsValid(String email, String mobileNumber, Date date, String password) { - return isValidEmail(email) && - isValidSouthAfricanMobileNumber(mobileNumber) && - isValidDateAndAdult(date) && - isValidPassword(password); + return isValidEmail(email) + && isValidSouthAfricanMobileNumber(mobileNumber) + && isValidDateAndAdult(date) + && isValidPassword(password); } /** - * Determines whether a given email is a valid email format according to the following criteria: - * - The email must contain a username and a domain separated by an @ symbol. - * - The username may contain alphanumeric characters, underscores, plus signs, asterisks, and hyphens. - * - The domain must consist of one or more subdomains separated by periods, and end in a top-level domain (TLD) - * of two to seven letters. - * - * @param email the email to be validated - * @return true if the email is a valid email format, false otherwise - */ + * Determines whether a given email is a valid email format according to the + * following criteria: - The email must contain a username and a domain + * separated by an @ symbol. - The username may contain alphanumeric + * characters, underscores, plus signs, asterisks, and hyphens. - The domain + * must consist of one or more subdomains separated by periods, and end in a + * top-level domain (TLD) of two to seven letters. + * + * @param email the email to be validated + * @return true if the email is a valid email format, false otherwise + */ private boolean isValidEmail(String email) { // Check if email is null if (email == null) { @@ -46,9 +48,9 @@ public class LoginController { } // Define regular expression pattern for valid email addresses - String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." + - "[a-zA-Z0-9_+&*-]+)*@" + - "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; + String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." + + "[a-zA-Z0-9_+&*-]+)*@" + + "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; Pattern pattern = Pattern.compile(emailRegex); // Check if email matches the pattern @@ -64,11 +66,13 @@ public class LoginController { } /** - * Checks whether a given mobile number is a valid South African mobile number. - * - * @param mobileNumber the mobile number to check - * @return true if the mobile number is a valid South African mobile number, false otherwise - */ + * Checks whether a given mobile number is a valid South African mobile + * number. + * + * @param mobileNumber the mobile number to check + * @return true if the mobile number is a valid South African mobile number, + * false otherwise + */ private boolean isValidSouthAfricanMobileNumber(String mobileNumber) { // Remove any non-digit characters from the mobile number mobileNumber = mobileNumber.replaceAll("[^\\d]", ""); @@ -96,34 +100,35 @@ public class LoginController { // If all checks pass, the mobile number is considered valid return true; } - + /** - * Checks if a given date is valid and the person is at least 18 years old. - * - * @param date the date to be checked - * @return true if the date is valid and the person is at least 18 years old, false otherwise - */ + * Checks if a given date is valid and the person is at least 18 years old. + * + * @param date the date to be checked + * @return true if the date is valid and the person is at least 18 years + * old, false otherwise + */ private static boolean isValidDateAndAdult(Date date) { // Check if the date is null if (date == null) { JOptionPane.showMessageDialog(null, "Please enter a date of birth"); return false; } - + // 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; } - + // Check if the person is at least 18 years old Calendar dob = Calendar.getInstance(); dob.setTime(date); 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))) { + 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))) { age--; } else { } @@ -131,18 +136,16 @@ public class LoginController { JOptionPane.showMessageDialog(null, "You need to be at least 18 years old to gamble!"); return false; } - + // If all checks pass, the date is valid and the person is at least 18 return true; } - + /** * Checks the following conditions to determine whether a password is valid: - * - is at least 10 characters in length - * - has at least 1 special character - * - has at least 1 number - * - has at least 1 uppercase and lowercase letter - * + * - is at least 10 characters in length - has at least 1 special character + * - has at least 1 number - has at least 1 uppercase and lowercase letter + * * @param password to be validated * @return true if password is valid, false otherwise */ @@ -188,15 +191,15 @@ public class LoginController { } /** - * Checks whether a character is a special character, i.e., one of the following: - * !@#$%^&*()-+=[]{}|\;:'",<.>/?. - * - * @param ch the character to check - * @return true if the character is a special character, false otherwise - */ + * Checks whether a character is a special character, i.e., one of the + * following: !@#$%^&*()-+=[]{}|\;:'",<.>/?. + * + * @param ch the character to check + * @return true if the character is a special character, false otherwise + */ private boolean isSpecialCharacter(char ch) { String specialCharacters = "!@#$%^&*()-+=[]{}|\\;:'\",<.>/?"; return specialCharacters.contains(String.valueOf(ch)); } - + } diff --git a/src/view/LoginGUI.java b/src/view/LoginGUI.java @@ -2,8 +2,6 @@ package view; import controller.LoginController; import java.util.Date; -import javax.swing.JOptionPane; - /** * @@ -17,7 +15,7 @@ public class LoginGUI extends javax.swing.JFrame { public LoginGUI() { initComponents(); } - + LoginController controller = new LoginController(); /** @@ -243,8 +241,8 @@ public class LoginGUI extends javax.swing.JFrame { String password = new String(passwordCh); // convert password to string Date selectedDate = jDateChooser1.getDate(); if (controller.areAllFieldsValid( - jTextFieldEmail.getText(), - jTextFieldMobileNumber.getText(), + jTextFieldEmail.getText(), + jTextFieldMobileNumber.getText(), selectedDate, password)) { new MainGUI().setVisible(true); // open the game window