Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
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);
}
}
5 changes: 5 additions & 0 deletions java/src/main/java/com/codurance/training/tasks/Command.java
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 java/src/main/java/com/codurance/training/tasks/Task.java
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 java/src/main/java/com/codurance/training/tasks/TaskImpl.java
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;
}
}
90 changes: 78 additions & 12 deletions java/src/main/java/com/codurance/training/tasks/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -93,19 +155,20 @@ private void add(String commandLine) {
}

private void addProject(String name) {

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.

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) {

Choose a reason for hiding this comment

The 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);
}
Expand All @@ -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) {

Choose a reason for hiding this comment

The 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()) {
Expand All @@ -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();
}

Expand All @@ -146,4 +212,4 @@ private void error(String command) {
private long nextId() {
return ++lastId;
}
}
}
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);
}
}