Activity 1.37.1.
There is a copy of the following code block in an interactive mode that you can modify and compile. Your task is to improve the code’s style as an expert would view it, while ensuring its behavior remains unchanged. You may run your refactored code to verify that it still passes all test cases.
public class CredentialCheck {
private String username;
private String password;
private int age;
// Constructor to initialize the fields
public CredentialCheck(String username, String password, int age) {
this.username = username;
this.password = password;
this.age = age;
}
// Check if the credentials are valid
public boolean hasValidCredentials() {
if (isValidUsername() && isValidPassword() && age >= 18) {
return true;
}
return false;
}
// Check if the username is valid
private boolean isValidUsername() {
if (username != null) {
if (username.length() >= 3 && username.length() < 10) {
return true;
}
}
return false;
}
// Check if the password is valid
private boolean isValidPassword() {
if (password == null) {
return false;
} else if (!isValidUsername()) {
return false;
} else if (password.contains(username)) {
return false;
}
return true;
}
}