# A simple Java CLI (part 1) - Solution Solution to the [Brief - A simple Java CLI (part 1)](https://hackmd.io/gazhPGlOSJiQpjJBE6TQsg). ## What where the prerequisites? You should be comfortable with topics below. If it not the case, you should learn more about: - What a computer program is, basically some computation over an input producing an output. Computation to be understood as a sequence of instructions - What a CLI is (Command Line Interface) - What a command line is: a string with some tokens separated by whitespaces. Note that the command line could be empty or contain a single token. A token in this context is a single or sequence of characters which do not have any meaning yet - How to compile and execute a Java program from a terminal - Basic algorithmic and programming knowledge such as variable declaration and assignment, strings, arrays, loops, decisions making (`if-then`, `if-then-else`) based on conditions (boolean expressions) - A search strategy to find relevant resources, ideally in English ## What was expected - Understand the specifications! - Deliver no more or less than expected :) ## The code This is not the unique possible implementation neither an optimized implementation, it is not the objective. It goes straight to the point which is satisfying the specifications. ```java= import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.Scanner; public class Cli { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("> "); while (true) { String commandLine = scanner.nextLine(); String[] tokens = commandLine.split(" ", 2); String commandName = tokens[0]; String output = ""; if (commandName.equals("exit")) { break; } else if (commandName.equals("date")) { output = LocalDate.now().toString(); } else if (commandName.equals("time")) { output = LocalTime.now().toString(); } else if (commandName.equals("datetime")) { output = LocalDateTime.now().toString(); } else if (commandName.equals("useraccount")) { output = System.getProperty("user.name"); } else if (commandName.equals("userhome")) { output = System.getProperty("user.home"); } else if (commandName.equals("os")) { String name = System.getProperty("os.name"); String version = System.getProperty("os.version"); output = name + " (" + version + ")"; } else if (commandName.equals("printenv")) { if (tokens.length == 2) { String name = tokens[1]; String value = System.getenv(name); if (value != null) { output = value; } } } else if (commandName.equals("echo")) { if (tokens.length == 2) { output = tokens[1]; } } else { output = "Command '" + commandName + "' not found."; } System.out.println(output); System.out.print("> "); } scanner.close(); System.out.println("Bye!"); } } ``` ## About Java core concepts in this brief - Syntax and naming conventions, packages - Class and `main` method - `String` class, array (of strings), `split` method, string concatenation with the operator `+`, string comparison with the `equals` method - Retrieve elements from an array, comparison on numbers (`int`) with the `==` operator - `if`/`else if`/`else` - Frequently used time-related classes such as `LocalDate`, `LocalTime` and `LocalDateTime` - How to get some system properties and environment variables thanks to the `System` class - Compilation vs execution errors. Developer MUST take the time to read error messages ## About testing The best, only way to check that your code works and meet the needs is to **TEST IT**! The main difficulty about testing is not writing tests, it's to identify the test cases, what has to be tested in my program. You must test every expected behavior, expected behavior when the program should workd and when the program should not work. In other words you must also test the error cases. A test is a scenario: - Given some inputs (`echo a b c`) - When I run the command line - Then I expect the output to be `a b c` Or for an error use case: - Given some inputs (`echox a b c`) - When I run the command line - Then I expect the output to be `Command 'echox' not found.` ## Resources ### Algorithmic - [Cours d'algorithmique](http://cours.pise.info/algo/codage.htm), Christophe Darmangeat - "[Grokking Algorithms](https://www.amazon.fr/Grokking-Algorithms-Aditya-Y-Bhargava/dp/1617292230/)", Aditya Y Bhargava, Manning Publications ### Java - "[Get Started with Java](https://www.baeldung.com/get-started-with-java-series)", Baeldung - "[Java 11 API docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/module-summary.html)", official Java 11 documentation - "[Head First Java: A Learner's Guide to Real-World Programming](https://www.amazon.fr/Head-First-Java-Brain-friendly-Guide/dp/1491910771)", Kathy Sierra, Bert Bates, Trisha Gee, O'Reilly Media