Switching across Enums

Report a typo

You are given a sealed interface Command and enum SimpleCommand with commands: START, STOP, PAUSE. You are also given a CustomCommand class to handle commands that aren't define as enums. Your task is to extend the processCommand method using switch to print messages based on the enum constant.

  • START – "System starting"

  • STOP – "System stopping"

  • PAUSE – "System paused"

If a CustomCommand is provided to the switch statement instead of the three enums, then the program should print "Custom command.".

You only need to modify the processCommand method. Do not modify the remaining code.

Sample Input 1:

START

Sample Output 1:

System starting
Write a program in Java 17
import java.util.Scanner;

sealed interface Command permits SimpleCommand, CustomCommand {}

enum SimpleCommand implements Command {
START, STOP, PAUSE
}

final class CustomCommand implements Command {
String description;

CustomCommand(String description) {
this.description = description;
}

public String getDescription() {
return description;
}
}

public class Main {
public static void processCommand(Command cmd) {
// write your code here
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine().trim().toUpperCase();

Command command;
try {
command = SimpleCommand.valueOf(input);
} catch (IllegalArgumentException e) {
command = new CustomCommand(input);
}

Create a free account to access the full topic