0
\$\begingroup\$

This is a follow-up question for Android app class serialization. The implementation of User class has been updated and the functionality of null string checking, password strength checking and the correctness of date formatting are considered in this post. The part of password strength checking has been implemented in PasswordStrength class.

The experimental implementation

  • Project name: UserClassTest

  • PasswordStrength.java implementation:

    package com.example.userclasstest;
    
    import java.util.ArrayList;
    
    public class PasswordStrength {
        private ArrayList<String> commonPasswords = new ArrayList<>();
        private int minimumLengthRequired = 8;
        public enum Level {
            Weak,
            Medium,         //  TODO: Implement the determination of medium level password
            Strong          //  TODO: Implement the determination of strong level password
        }
        private Level passwordStrengthLevel;
    
    
        public PasswordStrength(String input)
        {
            CreateCommonPasswordsList();
            if (LengthCheck(input) == false)
            {
                this.passwordStrengthLevel = Level.Weak;
                return;
            }
            if (this.commonPasswords.contains(input))
            {
                this.passwordStrengthLevel = Level.Weak;
                return;
            }
            this.passwordStrengthLevel = Level.Medium;
            return;
        }
    
        private void CreateCommonPasswordsList()
        {
            this.commonPasswords.add("111111");
            this.commonPasswords.add("123123");
            this.commonPasswords.add("12345");
            this.commonPasswords.add("123456");
            this.commonPasswords.add("12345678");
            this.commonPasswords.add("123456789");
            this.commonPasswords.add("1234567890");
            this.commonPasswords.add("picture1");
            this.commonPasswords.add("password");
            this.commonPasswords.add("password123");
            this.commonPasswords.add("Password");
            this.commonPasswords.add("Password123");
            this.commonPasswords.add("Password123");
        }
    
        private boolean LengthCheck(String input)
        {
            if (input.length() > minimumLengthRequired)
            {
                return true;
            }
            return false;
        }
    
        public boolean IsPasswordWeak()
        {
            return this.passwordStrengthLevel.equals(Level.Weak);
        }
    }
    
  • User.java implementation:

    package com.example.userclasstest;
    
    import android.content.Context;
    import android.util.Log;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    
    
    public class User  implements java.io.Serializable{
        private String fullName;
        private String personalID;
        private String dateOfBirth;
        private String cellPhoneNumber;
        private String emailInfo;
        private String password;
    
    
        public User(String fullNameInput,
                    String personalIDInput,
                    String dateOfBirthInput,
                    String cellPhoneNumberInput,
                    String emailInfoInput,
                    String passwordInput) throws NoSuchAlgorithmException, NullPointerException, IllegalArgumentException
        //  User object constructor
        {
            //  Reference: https://stackoverflow.com/a/6358/6667035
            if (fullNameInput == null) {
                throw new NullPointerException("fullNameInput must not be null");
            }
            this.fullName = fullNameInput;
    
            if (personalIDInput == null) {
                throw new NullPointerException("personalIDInput must not be null");
            }
            this.personalID = personalIDInput;
    
            if (dateOfBirthInput == null) {
                throw new NullPointerException("dateOfBirthInput must not be null");
            }
            if (IsDateValid(dateOfBirthInput) == false)
            {
                throw new IllegalArgumentException("dateOfBirthInput is invalid");
            }
            this.dateOfBirth = dateOfBirthInput;
    
            if (cellPhoneNumberInput == null) {
                throw new NullPointerException("cellPhoneNumberInput must not be null");
            }
            this.cellPhoneNumber = cellPhoneNumberInput;
    
            if (emailInfoInput == null) {
                throw new NullPointerException("emailInfoInput must not be null");
            }
            this.emailInfo = emailInfoInput;
    
            if (passwordInput == null) {
                throw new NullPointerException("passwordInput must not be null");
            }
            if (new PasswordStrength(passwordInput).IsPasswordWeak())
            {
                throw new IllegalArgumentException("Password is weak!");
            }
            this.password = HashingMethod(passwordInput);
        }
    
        //  Reference: https://stackoverflow.com/a/20231617/6667035
        private boolean IsDateValid(String dateStr) {
            if (dateStr.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})"))
                return true;
            else
                return false;
        }
    
        public String GetFullName()
        {
            return this.fullName;
        }
        public String GetPersonalID()
        {
            return this.personalID;
        }
    
        public String GetDateOfBirth()
        {
            return this.dateOfBirth;
        }
    
        public String GetCellPhoneNumber()
        {
            return this.cellPhoneNumber;
        }
    
        public String GetEmailInfo()
        {
            return this.emailInfo;
        }
    
        public String GetHash() throws NoSuchAlgorithmException
        {
            return HashingMethod(this.fullName + this.personalID);
        }
    
        public String GetHashedPassword() throws NoSuchAlgorithmException
        {
            return this.password;
        }
    
        public boolean CheckPassword(String password)
        {
            boolean result = false;
            try {
                result = this.password.equals(HashingMethod(password));
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return result;
        }
    
        //**********************************************************************************************
    
        //  Reference: https://stackoverflow.com/a/2624385/6667035
        private String HashingMethod(String InputString) throws NoSuchAlgorithmException
        {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            String stringToHash = InputString;
            messageDigest.update(stringToHash.getBytes());
            String stringHash = new String(messageDigest.digest());
            return stringHash;
        }
    }
    
  • MainActivity.java implementation:

    package com.example.userclasstest;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            User currentUser = null;
            try {
                currentUser = new User(
                        "Mike",
                        "M12345678",
                        "10/13/1990",
                        "(555) 555-1234",
                        "[email protected]",
                        "p?@nY4q~?A-)T#+^");
            }
            catch (Exception e)
            {
                ShowToast(e.getMessage(), Toast.LENGTH_LONG);
                e.printStackTrace();
                Log.d("onCreate", e.getMessage());
                return;
            }
            ShowToast(currentUser.GetFullName(), Toast.LENGTH_SHORT);
        }
    
        private void ShowToast(String Text, int Duration)
        {
            Context context = getApplicationContext();
            CharSequence text = Text;
            int duration = Duration;
    
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    }
    

All suggestions are welcome.

The summary information:

  • Which question it is a follow-up to?

    Android app class serialization

  • What changes has been made in the code since last question?

    The implementation of User class has been updated and the functionality of null string checking, password strength checking and the correctness of date formatting are considered in this post.

  • Why a new review is being asked for?

    If there is any possible improvement, please let me know.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Dates

//  Reference: https://stackoverflow.com/a/20231617/6667035
private boolean IsDateValid(String dateStr) {
    if (dateStr.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})"))
        return true;
    else
        return false;
}

Did you read the comment the OP posted after that linked answer?

Almost 5 years after posting this answer, I realize that this is a stupid way to validate a date format. But i'll just leave this here to tell people that using regex to validate a date is unacceptable

As an example... "99/99/2021" is a valid date...

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Don't delete your answer, because your main concern about CurrentUser is IMO not correct. CurrentUser is created inside the Try block. See first version inside the edit history of the question. Nevertheless your answer shows some valid points. \$\endgroup\$
    – Heslacher
    Commented May 21, 2021 at 6:13
  • \$\begingroup\$ @heslacher Thanks, you're right, I missed the return in the exception handler so the line I was worried about wouldn't have run. I've updated my answer so that it's now in line with the current version of the question (since I caused the edits) and I'll revisit when I have a keyboard. \$\endgroup\$
    – forsvarir
    Commented May 21, 2021 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.