Aan de slagGa gratis aan de slag

Tracking application states

Managing different states, such as success or error responses, is essential in real-world applications. You'll practice defining enums and custom methods to organize and display various application states clearly.

Deze oefening maakt deel uit van de cursus

Input/Output and Streams in Java

Cursus bekijken

Oefeninstructies

  • Add a method isActiveUser() that returns true only for the ACTIVE state.
  • Create a user in SUSPENDED state named suspendUser
  • Create a user in ACTIVE state named activeUser
  • Check if suspendUser is an active user

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

public class UserStateManager {
    enum UserState { 
        NEW, ACTIVE, SUSPENDED; 

        public boolean isActiveUser() {
        
        	// return if user is in ACTIVE state
            return this == ____;
        }
    }

    public static void main(String[] args) {
    	// Create a UserState with SUSPENDED status
    	UserState suspendUser = ____.____;
        
        // Create a UserState with ACTIVE status
    	UserState activeUser = ____.____;
        
    	// check if suspendUser is an active user
        System.out.println(suspendUser.____()); 
        System.out.println(activeUser.isActiveUser());
	}
}
Code bewerken en uitvoeren