Skip to content

Commit

Permalink
Sarthak | Refactors CheckCommand and UncheckCommand to use Arguments …
Browse files Browse the repository at this point in the history
…to get the int value of an argument, #5
  • Loading branch information
SarthakMakhija committed Jan 9, 2025
1 parent a9ca0a6 commit b0bd02c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/codurance/training/commands/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ static Arguments skipOneAndCreate(String[] parts) {
Arguments skipOne() {
return new Arguments(this.subList(1, this.size()));
}

int argumentAtIndexAsInt(int index) {
return Integer.parseInt(this.get(index));
}
}
10 changes: 2 additions & 8 deletions src/main/java/com/codurance/training/commands/CheckCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ class CheckCommand implements Command {

public void execute(Arguments arguments) throws Exception {
assert (arguments.size() == 1);
int taskId = taskId(arguments);
if (projects.markTaskWithIdDone(taskId)) return;
writer.write(String.format("Could not find a task with an ID of %d\n", taskId));
}

//highlights that the method does not belong here
private static int taskId(List<String> arguments) {
return Integer.parseInt(arguments.get(0));
if (projects.markTaskWithIdDone(arguments.argumentAtIndexAsInt(0))) return;
writer.write(String.format("Could not find a task with an ID of %d\n", arguments.argumentAtIndexAsInt(0)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.codurance.training.tasks.Projects;

import java.io.Writer;
import java.util.List;

class UncheckCommand implements Command {
private final Writer writer;
Expand All @@ -16,13 +15,7 @@ class UncheckCommand implements Command {

public void execute(Arguments arguments) throws Exception {
assert (arguments.size() == 1);
int taskId = taskId(arguments);
if (projects.markTaskWithIdNotDone(taskId)) return;
writer.write(String.format("Could not find a task with an ID of %d\n", taskId));
}

//highlights that the method does not belong here
private static int taskId(List<String> arguments) {
return Integer.parseInt(arguments.get(0));
if (projects.markTaskWithIdNotDone(arguments.argumentAtIndexAsInt(0))) return;
writer.write(String.format("Could not find a task with an ID of %d\n", arguments.argumentAtIndexAsInt(0)));
}
}
12 changes: 12 additions & 0 deletions src/test/java/com/codurance/training/commands/ArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ public void skipOneFromArguments() {
Arguments arguments = new Arguments(List.of("an", "argument", "1"));
assertEquals(List.of("argument", "1"), arguments.skipOne());
}

@Test
public void argumentAtIndex0AsInt() {
Arguments arguments = new Arguments(List.of("1", "argument", "1"));
assertEquals(1, arguments.argumentAtIndexAsInt(0));
}

@Test
public void argumentAtIndex1AsInt() {
Arguments arguments = new Arguments(List.of("0", "200", "1"));
assertEquals(200, arguments.argumentAtIndexAsInt(1));
}
}

0 comments on commit b0bd02c

Please sign in to comment.