Skip to main content

Practice Code Structure

Section 1.37 Code Refactoring

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;
    }
}

Activity 1.37.2.

    Compare your refactored code with the solution below. Did you refactor all three method correctly?
  • 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() {
    		return isValidUsername() && isValidPassword() && age >= 18;
    	}
    
    	// Check if the username is valid
    	private boolean isValidUsername() {
    		 return username != null && username.length() >= 3 && username.length() < 10;
    	}			
    			
    
    	// Check if the password is valid
    	private boolean isValidPassword() {
    		return password != null && isValidUsername() && !password.contains(username);
    	}
    }
    
  • You refactored correctly!
  • 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() {
    		return isValidUsername() && isValidPassword() && age >= 18;
    	}
    
    	// Check if the username is valid
    	private boolean isValidUsername() {
    	     if (username == null) {
    	     	return false;
    	     }
    		 return username.length() >= 3 && username.length() < 10;
    	}			
    			
    
    	// Check if the password is valid
    	private boolean isValidPassword() {
    	    if (password == null) {
    	    	return false;
    	    }
    		return isValidUsername() && !password.contains(username);
    	}
    }
    
  • You refactored correctly!
You have attempted 1 of 3 activities on this page.