Tracking application states
Managing different states clearly, like success or error responses, is essential in real applications. You'll practice defining enums and custom methods to clearly manage and display various application states.
This exercise is part of the course
Input/Output and Streams in Java
Exercise instructions
- Add a method
isActiveUser()that returnstrueonly for theACTIVEstate. - Create a user in
SUSPENDEDstate namedsuspendUser - Create a user in
ACTIVEstate namedactiveUser - Check if
suspendUseris an active user
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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());
}
}