To find out which command the user currently wants to execute, let's add an Operation
askOperation() method to the Archiver class. This method should display a list of available commands and
ask the user to choose one of them. For convenience, we will ask the user to enter the command's number, which is its
ordinal number in the Operation enum. You can get an enum field's ordinal number using the ordinal() method.
Now everything is ready to rewrite the main method using our latest scientific advancements, specifically
the CommandExecutor class and the askOperation() method.
1. Add a public static Operation askOperation() throws IOException method to the Archiver class.
It must:
1.1. Use the ConsoleHelper class's methods
1.2. Ask the user for the number of the desired operation.
Hint:
To display the number of the "Create archive" operation, use: Operation.CREATE.ordinal()
1.3. Return the selected operation.
Example output of the askOperation() method:
Select an operation:0 - Zip files into an archive
1 - Add a file to an archive
2 - Remove a file from an archive
3 - Extract an archive
4 - View the contents of an archive
5 - Exit
2. Rewrite the main() method:2.1. Declare a local Operation variable
2.2. In a loop, use the askOperation() method to ask for a new value for the variable in step 2.1, and use CommandExecutor.execute() to perform the operation.
2.3. Allow the user to exit the loop by selecting the Operation.EXIT operation
2.4. Wrap the askOperation() and execute(operation) calls in a try-catch block. If a NoSuchZipFileException occurs, use ConsoleHelper to display "You didn't select an archive or you selected an invalid file." If any other exceptions occur, display "An error occurred. Please check the entered data."
2.5. Be sure that the program continues running (continues to a new iteration of the loop) after handling exceptions.
3. Run the program and verify that the "exit" command works.
- You need to create a public static Operation askOperation() method in the Archiver class.
- The askOperation method should display all of the possible operations and their numbers.
- The askOperation method should read the selected operation's number from the keyboard and return it.
- Rewrite the main method according to the task conditions.