-
Notifications
You must be signed in to change notification settings - Fork 192
fix: update feature deadline #48
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
Paritosh-Pranjal
wants to merge
1
commit into
codurance:master
Choose a base branch
from
Paritosh-Pranjal:Paritosh_SOLID_KATAS
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
18 changes: 18 additions & 0 deletions
18
java/src/main/java/com/codurance/training/tasks/AddTaskCommand.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,18 @@ | ||
package com.codurance.training.tasks; | ||
|
||
class AddTaskCommand implements Command { | ||
private final TaskList taskList; | ||
private final String project; | ||
private final String description; | ||
|
||
public AddTaskCommand(TaskList taskList, String project, String description) { | ||
this.taskList = taskList; | ||
this.project = project; | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
taskList.addTask(project, description); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
java/src/main/java/com/codurance/training/tasks/CheckTaskCommand.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,16 @@ | ||
package com.codurance.training.tasks; | ||
|
||
class CheckTaskCommand implements Command { | ||
private final TaskList taskList; | ||
private final String idString; | ||
|
||
public CheckTaskCommand(TaskList taskList, String idString) { | ||
this.taskList = taskList; | ||
this.idString = idString; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
taskList.setDone(idString, true); | ||
} | ||
} |
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,5 @@ | ||
package com.codurance.training.tasks; | ||
|
||
interface Command { | ||
void execute(); | ||
} |
32 changes: 10 additions & 22 deletions
32
java/src/main/java/com/codurance/training/tasks/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,17 @@ | ||
package com.codurance.training.tasks; | ||
|
||
public final class Task { | ||
private final long id; | ||
private final String description; | ||
private boolean done; | ||
import java.util.Date; | ||
|
||
public Task(long id, String description, boolean done) { | ||
this.id = id; | ||
this.description = description; | ||
this.done = done; | ||
} | ||
interface Task { | ||
long getId(); | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
String getDescription(); | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
boolean isDone(); | ||
|
||
public boolean isDone() { | ||
return done; | ||
} | ||
void setDone(boolean done); | ||
|
||
public void setDone(boolean done) { | ||
this.done = done; | ||
} | ||
} | ||
Date getDeadline(); | ||
|
||
void setDeadline(Date deadline); | ||
} |
46 changes: 46 additions & 0 deletions
46
java/src/main/java/com/codurance/training/tasks/TaskImpl.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,46 @@ | ||
package com.codurance.training.tasks; | ||
|
||
import java.util.Date; | ||
|
||
class TaskImpl implements Task { | ||
private final long id; | ||
private final String description; | ||
private boolean done; | ||
private Date deadline; | ||
|
||
public TaskImpl(long id, String description, boolean done) { | ||
this.id = id; | ||
this.description = description; | ||
this.done = done; | ||
} | ||
|
||
@Override | ||
public long getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public boolean isDone() { | ||
return done; | ||
} | ||
|
||
@Override | ||
public void setDone(boolean done) { | ||
this.done = done; | ||
} | ||
|
||
@Override | ||
public Date getDeadline() { | ||
return deadline; | ||
} | ||
|
||
@Override | ||
public void setDeadline(Date deadline) { | ||
this.deadline = deadline; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,18 +4,17 @@ | |
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.*; | ||
|
||
public final class TaskList implements Runnable { | ||
public class TaskList { | ||
private static final String QUIT = "quit"; | ||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
private final Map<String, List<Task>> tasks = new LinkedHashMap<>(); | ||
private final BufferedReader in; | ||
private final PrintWriter out; | ||
|
||
private long lastId = 0; | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
@@ -62,6 +61,12 @@ private void execute(String commandLine) { | |
case "uncheck": | ||
uncheck(commandRest[1]); | ||
break; | ||
case "today": | ||
showTasksDueToday(); | ||
break; | ||
case "deadline": | ||
setDeadline(commandRest[1]); | ||
break; | ||
case "help": | ||
help(); | ||
break; | ||
|
@@ -71,16 +76,73 @@ private void execute(String commandLine) { | |
} | ||
} | ||
|
||
private void showTasksDueToday() { | ||
Date today = new Date(); | ||
for (Map.Entry<String, List<Task>> project : tasks.entrySet()) { | ||
out.println(project.getKey()); | ||
for (Task task : project.getValue()) { | ||
if (task.getDeadline() != null && isSameDay(today, task.getDeadline())) { | ||
out.printf(" [%c] %d: %s (Due: %s)%n", (task.isDone() ? 'x' : ' '), task.getId(), task.getDescription(), DATE_FORMAT.format(task.getDeadline())); | ||
} | ||
} | ||
out.println(); | ||
} | ||
} | ||
|
||
private boolean isSameDay(Date date1, Date date2) { | ||
Calendar cal1 = Calendar.getInstance(); | ||
cal1.setTime(date1); | ||
Calendar cal2 = Calendar.getInstance(); | ||
cal2.setTime(date2); | ||
return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && | ||
cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && | ||
cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH); | ||
} | ||
|
||
private void setDeadline(String deadlineCommand) { | ||
String[] parts = deadlineCommand.split(" ", 2); | ||
String idString = parts[0]; | ||
String date = parts[1]; | ||
int id = Integer.parseInt(idString); | ||
try { | ||
Date deadline = DATE_FORMAT.parse(date); | ||
setTaskDeadline(id, deadline); | ||
} catch (ParseException e) { | ||
out.println("Invalid date format. Use yyyy-MM-dd."); | ||
} | ||
} | ||
|
||
private void setTaskDeadline(int id, Date deadline) { | ||
for (Map.Entry<String, List<Task>> project : tasks.entrySet()) { | ||
for (Task task : project.getValue()) { | ||
if (task.getId() == id) { | ||
task.setDeadline(deadline); | ||
return; | ||
} | ||
} | ||
} | ||
out.printf("Could not find a task with an ID of %d.", id); | ||
out.println(); | ||
} | ||
|
||
private 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()); | ||
printTask(task); | ||
} | ||
out.println(); | ||
} | ||
} | ||
|
||
private void printTask(Task task) { | ||
out.printf(" [%c] %d: %s", (task.isDone() ? 'x' : ' '), task.getId(), task.getDescription()); | ||
if (task.getDeadline() != null) { | ||
out.printf(" (Due: %s)", DATE_FORMAT.format(task.getDeadline())); | ||
} | ||
out.println(); | ||
} | ||
|
||
private void add(String commandLine) { | ||
String[] subcommandRest = commandLine.split(" ", 2); | ||
String subcommand = subcommandRest[0]; | ||
|
@@ -93,19 +155,20 @@ private void add(String commandLine) { | |
} | ||
|
||
private void addProject(String name) { | ||
tasks.put(name, new ArrayList<Task>()); | ||
tasks.put(name, new ArrayList<>()); | ||
} | ||
|
||
private void addTask(String project, String description) { | ||
public void addTask(String project, String description) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't use primitives in method parameters,make use of POJO. |
||
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)); | ||
projectTasks.add(new TaskImpl(nextId(), description, false)); | ||
} | ||
|
||
|
||
private void check(String idString) { | ||
setDone(idString, true); | ||
} | ||
|
@@ -114,7 +177,7 @@ private void uncheck(String idString) { | |
setDone(idString, false); | ||
} | ||
|
||
private void setDone(String idString, boolean done) { | ||
public void setDone(String idString, boolean done) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use POJO, eliminate primitive obsession. |
||
int id = Integer.parseInt(idString); | ||
for (Map.Entry<String, List<Task>> project : tasks.entrySet()) { | ||
for (Task task : project.getValue()) { | ||
|
@@ -128,13 +191,16 @@ private void setDone(String idString, boolean done) { | |
out.println(); | ||
} | ||
|
||
|
||
private 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(" today"); | ||
out.println(" deadline <task ID> <yyyy-MM-dd>"); | ||
out.println(); | ||
} | ||
|
||
|
@@ -146,4 +212,4 @@ private void error(String command) { | |
private long nextId() { | ||
return ++lastId; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
java/src/main/java/com/codurance/training/tasks/UncheckTaskCommand.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,16 @@ | ||
package com.codurance.training.tasks; | ||
|
||
class UncheckTaskCommand implements Command { | ||
private final TaskList taskList; | ||
private final String idString; | ||
|
||
public UncheckTaskCommand(TaskList taskList, String idString) { | ||
this.taskList = taskList; | ||
this.idString = idString; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
taskList.setDone(idString, false); | ||
} | ||
} |
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 for project and use it here.