Ewan10 / Task-Manager

0 stars 0 forks source link

Refactoring approach to user input #7

Closed thompsy closed 2 years ago

thompsy commented 2 years ago

The TaskManager class is doing a lot of different things. In order to start separating out its responsibilities and make testing easier we're going to refactor out the user input handling that exists there.

All of the user input should be handled in the Main class for now. This includes reading input data from the command line and validating it. The validated input can then be passed into the public methods of the TaskManager which will do the actual work.

Let's consider adding a new task as a small worked example. A new task needs the following data:

In order to pass these on the command line we would run the program something like this: taskManager add --title "my title" --date "12/12/2021" --time "10:10am"

The Main class should then be able to gather this information and pass it into the public add method of the TaskManager. The add method will need to be updated to accept these parameters.

Ewan10 commented 2 years ago

Andrew I realized that I do not understand how the apache cli accepts input. I added options and parse the command line and I read them again I watched some videos about the cli which didn't help once again irrelevant. I don' t know how to refactor the add method wherein I call the userinput method validateTime and ValidateDate methods. Also Whenever I run command there are errors I cannot run it with any command. I realized how I am familiar with with the interactive reading of input and printing on a console window. I need a session and it would be helpful to write some sample code to see how it is run the command line.

private LocalDate validateDate() { String date; int[] userDate = { 0, 0, 0 }; String[] dateAsString = {};

do {
  do {
    date = userInput("Enter the date as: YYYY-MM-dd");
  } while (!Pattern.compile("\\d{4}?\\-\\d{2}?\\-\\d{2}?").matcher(date).matches());

  dateAsString = date.split("-");
  for (int i = 0; i < userDate.length; i++)
    userDate[i] = Integer.parseInt(dateAsString[i]);
} while (!(userDate[0] >= 2021) || (!(userDate[1] >= 1 && userDate[1] <= 12)) || (!(userDate[2] >= 1
    && userDate[2] <= 31)));

// Date validation according to numeric values of 31, 12 and a positive number for the day, the month and the year respectively.
return LocalDate.parse(date);

} private String userInput(String expression) { // the user to enter input and it returns that input string. String s; Scanner sx = new Scanner(System.in); System.out.println(expression); s = sx.nextLine(); sx.close(); return s; }

thompsy commented 2 years ago

Ok Ioannis, I'll email you to setup a session where we can walk through the basics of how to use Apache CLI.

One other important thing that I need to mention here is your approach to communication. Working on your communication skills is very important if you ever want to get a job in the field. Here are a few pointers that could help you improve:

thompsy commented 2 years ago

Here's a small demo project that shows how to use the Commons CLI library: https://github.com/thompsy/commons-cli-example

You should be able to clone this and run the Maven command listed in the Readme to run it. If you have a look over this before our call that would be helpful.

Ewan10 commented 2 years ago

I know derrogatory language is inappropriate in a professional context. I 'd say it is in any society unacceptable. The executive will fire you instantly. It won't happen again boss.

Now your example helps to detect the obstacles I have with the cli. I cloned the example repo. If I type 'mvn compile' it builds successfully. First of all I opened the folder of the cloned repo in VSCode and on the terminal I move in the commons-cli-example folder. If I type on the terminal the full command:

mvn compile exec:java -Dexec.mainClass="uk.co.downthewire.example.Main" \ -Dexec.args="-a 'Example task' --date '12/12/21' -t '10am'" Then build errors appear. Now if I run it on Git bash the errors are such:

ERROR] Unknown lifecycle phase " -Dexec.args=-a 'Example task' --date '12/12/21' -t '10am'". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging.

If I do it on powershell the errors are such:

[ERROR] Unknown lifecycle phase ".mainClass=uk.co.downthewire.example.Main". You must specify a valid lifecycle phase at : or :[:]:. Available lifecycle phaitialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generates-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-pactegration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-si site-deploy. -> [Help 1] [ERROR] Re-run Maven using the -X switch to enable full debug logging.

I do not know the syntax of the command line as exec: java or -Dexec.args and so on and so forth. Also I noticed I don't use the getOptionValue method.

thompsy commented 2 years ago

Thanks Ioannis, that's helpful detail!

I had a small mistake in the command listed in the readme of the example project. I've fixed that now so the following command should work: mvn compile exec:java -Dexec.mainClass="uk.co.downthewire.example.Main" -Dexec.args="-a 'Example task' --date '12/12/21' -t '10am'"

Try running that in the example project and let me know what you get.

Also, it's good practice if you put use code formatting when adding code, errors or commands into comments on Github and other places. It makes things a bit easier to read for others. You can use the "Insert Code" button on the editor. You can see some more details about Markdown, the format that's used, here: https://docs.github.com/en/github/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax

thompsy commented 2 years ago

This issue will be replaced by #8