-
Notifications
You must be signed in to change notification settings - Fork 190
feat: Modified code according to SOLID principles Task List. #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SachinGoud9
wants to merge
1
commit into
codurance:master
Choose a base branch
from
SachinGoud9:SachinGoud9/task-list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
java/src/main/java/com/codurance/training/tasks/TaskExceptionsImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.codurance.training.tasks; | ||
|
|
||
| import com.codurance.training.tasks.interfaces.TaskExceptions; | ||
|
|
||
| import java.io.PrintWriter; | ||
|
|
||
| public class TaskExceptionsImpl implements TaskExceptions { | ||
|
|
||
| private final PrintWriter out; | ||
|
|
||
| public TaskExceptionsImpl(PrintWriter out) { | ||
| this.out = out; | ||
| } | ||
|
|
||
| public void help() { | ||
| out.println("Commands:"); | ||
| out.println(" show"); | ||
| out.println(" add project <project name>"); | ||
| out.println(" add task <project name> <task description>"); | ||
| out.println(" check <task ID>"); | ||
| out.println(" uncheck <task ID>"); | ||
| out.println(); | ||
| } | ||
|
|
||
| public void error(String command) { | ||
| out.printf("I don't know what the command \"%s\" is.", command); | ||
| out.println(); | ||
| } | ||
| } |
108 changes: 108 additions & 0 deletions
108
java/src/main/java/com/codurance/training/tasks/TaskExecution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package com.codurance.training.tasks; | ||
|
|
||
|
|
||
| import com.codurance.training.tasks.classes.Task; | ||
| import com.codurance.training.tasks.interfaces.TaskActions; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class TaskExecution implements TaskActions { | ||
| private long lastId = 0; | ||
| private final PrintWriter out; | ||
| private TaskExceptionsImpl taskExceptions; | ||
| private final Map<String, List<Task>> tasks = new LinkedHashMap<>(); | ||
| public TaskExecution(PrintWriter out) { | ||
| this.out = out; | ||
| } | ||
|
|
||
| public void execute(String commandLine) { | ||
| String[] commandRest = commandLine.split(" ", 2); | ||
| String command = commandRest[0]; | ||
| taskExceptions = new TaskExceptionsImpl(out); | ||
| switch (command) { | ||
| case "show": | ||
| show(); | ||
| break; | ||
| case "add": | ||
| add(commandRest[1]); | ||
| break; | ||
| case "check": | ||
| check(commandRest[1]); | ||
| break; | ||
| case "uncheck": | ||
| uncheck(commandRest[1]); | ||
| break; | ||
| case "help": | ||
| taskExceptions.help(); | ||
| break; | ||
| default: | ||
| taskExceptions.error(command); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public void show() { | ||
| for (Map.Entry<String, List<Task>> project : tasks.entrySet()) { | ||
| out.println(project.getKey()); | ||
| for (Task task : project.getValue()) { | ||
| out.printf(" [%c] %d: %s%n", (task.isDone() ? 'x' : ' '), task.getId(), task.getDescription()); | ||
| } | ||
| out.println(); | ||
| } | ||
| } | ||
|
|
||
| public void add(String commandLine) { | ||
| String[] subcommandRest = commandLine.split(" ", 2); | ||
| String subcommand = subcommandRest[0]; | ||
| if (subcommand.equals("project")) { | ||
| addProject(subcommandRest[1]); | ||
| } else if (subcommand.equals("task")) { | ||
| String[] projectTask = subcommandRest[1].split(" ", 2); | ||
| addTask(projectTask[0], projectTask[1]); | ||
| } | ||
| } | ||
|
|
||
| public void addProject(String name) { | ||
| tasks.put(name, new ArrayList<Task>()); | ||
| } | ||
|
|
||
| public void addTask(String project, String description) { | ||
| List<Task> projectTasks = tasks.get(project); | ||
| if (projectTasks == null) { | ||
| out.printf("Could not find a project with the name \"%s\".", project); | ||
| out.println(); | ||
| return; | ||
| } | ||
| projectTasks.add(new Task(nextId(), description, false)); | ||
| } | ||
|
|
||
| public void check(String idString) { | ||
| setDone(idString, true); | ||
| } | ||
|
|
||
| public void uncheck(String idString) { | ||
| setDone(idString, false); | ||
| } | ||
|
|
||
| public void setDone(String idString, boolean done) { | ||
| int id = Integer.parseInt(idString); | ||
| for (Map.Entry<String, List<Task>> project : tasks.entrySet()) { | ||
| for (Task task : project.getValue()) { | ||
| if (task.getId() == id) { | ||
| task.setDone(done); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| out.printf("Could not find a task with an ID of %d.", id); | ||
| out.println(); | ||
| } | ||
|
|
||
| public long nextId() { | ||
| return ++lastId; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...va/com/codurance/training/tasks/Task.java → ...odurance/training/tasks/classes/Task.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
java/src/main/java/com/codurance/training/tasks/interfaces/TaskActions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.codurance.training.tasks.interfaces; | ||
| public interface TaskActions { | ||
| void show(); | ||
| void add(String commandLine); | ||
| void addProject(String name); | ||
| void addTask(String project, String description); | ||
| void check(String idString); | ||
| void uncheck(String idString); | ||
| void setDone(String idString, boolean done); | ||
| long nextId(); | ||
| } |
6 changes: 6 additions & 0 deletions
6
java/src/main/java/com/codurance/training/tasks/interfaces/TaskExceptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.codurance.training.tasks.interfaces; | ||
|
|
||
| public interface TaskExceptions { | ||
| public void help(); | ||
| public void error(String command); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create a POJO and use it here, eliminate primitive obsession