---
# System prepended metadata

title: Brief - A simple Java CLI (part 3)

---

# Brief - A simple Java CLI (part 3)

## Prerequisite
- Fulfill "[A simple Java CLI (part 2)](https://hackmd.io/YZIFZiAEQF2DEikHgsskBw)" brief

## Learning objectives
- Basic concepts of object-oriented design and programming
- Implement more complex logic
- Discover new classes from the Java library
- Learn about files and exceptions
- Learn about maintaining existing code
- Develop your curiosity and creativity

## How to?
- Same as parts 1 and 2

## Towards object-oriented design and programming
Separation of concerns and responsibilities are some of object-oriented design and programming main motivations.

## A class to represent the command line (input)
- Create a new class named `CommandLine`
- The class must declare two fields of type `String`, `private` to encapsulate the command name and the optional argument of the command
- Declare a constructor with a `String` argument, the argument of the constructor is expecting a command line string to parse in order to extract the command name and optional argument, assign values to the fields
- Declare two methods returning: one the command name, the other the argument or `null` if no argument
- Declare a "query method" named `hasArgument` indicating whether or not the command line object has an argument, choose the right return type for the method. The method will help the logic implemented in the commands (see below)

## A class for the commands
- Create a new class named `Commands`
- Create a method for each command, the method must return a `String`, be `static`, named with the name of the command and have a single argument of type `CommandLine`
- Thanks to the `CommandLine` object you can have access to the argument if needed

## Refactor the `main` method
- Refactor the existing code in order to use the two new classes
- Instantiate a `CommandLine` object each time a command is received from the terminal
- Call the corresponding `static` method in the `Commands` class in order to execute the logic of the command
- Test all your commands in order to verify that you do not have any side-effect

## New commands to implement

### Print the content of a text file
- Add a `cat` command that prints the content of a text file, adding dynamically the line number in the output
- If no argument is passed to the command, prints `Please specify a path to a text file to read`
- If the argument is present but does not denote a path to a file or an exception is raised (any type of exception), the command prints `Error reading file`, does not print the message of the exception
- The command should be able to read any text file, but you have to create a new file in the root directory of your project so that you can test your command. A file named `cat-me.txt` with content here after:
```shell
Content of the cat-me text file
Second line
Third line
Fourth line
Last line of the file
```
- Arguments: a relative or absolute path to a text file
- Example output (command `cat ./cat-me.txt`):
```shell
1. Content of the cat-me text file
2. Second line
3. Third line
4. Fourth line
5. Last line of the file
```

### Creative command
- Choose and implement a new command of your choice, be curious and creative, do not try to implement a too complex command
- Arguments: command may have arguments or not
- Have fun!