Skip to content

Commit

Permalink
Merge pull request #3 from LimZiJia/branch-BCD-Extension
Browse files Browse the repository at this point in the history
Add mass ops
  • Loading branch information
LimZiJia authored Feb 13, 2024
2 parents ee095c7 + db01704 commit 46e1569
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
16 changes: 10 additions & 6 deletions src/main/duke/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public String parse(String input) {
reply = Duke.storage.save(Duke.tasks);
break;
case MARK:
reply = Duke.tasks.mark(inputParsed[1]); // mark(task number)
reply = Duke.tasks.mark(inputParsed); // mark(task number)
break;
case UNMARK:
reply = Duke.tasks.unmark(inputParsed[1]); // unmark(task number)
reply = Duke.tasks.unmark(inputParsed); // unmark(task number)
break;
case DELETE:
reply = Duke.tasks.delete(inputParsed[1]); // delete(task number)
reply = Duke.tasks.delete(inputParsed); // delete(task number)
break;
case FIND:
reply = Duke.tasks.find(inputParsed[1]); // find(task name)
Expand Down Expand Up @@ -153,9 +153,13 @@ public String addTask(String[] inputs, Command command) {

private void checkValidFormat(String[] inputs, Command command) {
switch (command) {
case MARK: // Command: mark taskNumber
case UNMARK: // Command: unmark taskNumber
case DELETE:// Command: delete taskNumber
case MARK: // Command: mark listOfTaskNumbers
case UNMARK: // Command: unmark listOfTaskNumbers
case DELETE:// Command: delete listOfTaskNumbers
if (inputs.length < 2) {
throw new DukeEmptyArgumentException();
}
break;
case FIND: // Command: find taskName
case TODO: // Command: todo taskName
if (inputs.length < 2) {
Expand Down
76 changes: 44 additions & 32 deletions src/main/duke/tasks/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,67 +66,79 @@ public List<Task> getList() {
*
* @param input The index of the task that should be marked as done.
*/
public String mark(String input) {
String s;
public String mark(String[] input) {
StringBuilder s = new StringBuilder();
String forPrintingError = "";
try {
int i = Integer.parseInt(input);
Task t = Duke.tasks.get(i - 1);
t.markAsDone();
Duke.tasks.set(i - 1, t);
s = "Nice! I've marked this task as done:\n " + t;

// Start at 1 beacuse indexes start at 1. Index 0 is "mark"
for (int i = 1; i < input.length; i++) {
forPrintingError = input[i];
int idx = Integer.parseInt(input[i]);
Task t = Duke.tasks.get(idx - 1);
t.markAsDone();
Duke.tasks.set(idx - 1, t);
s.append("Nice! I've marked this task as done:\n ").append(t).append('\n');
}
} catch (NumberFormatException e) {
s = "Not a valid number!\n";
s = new StringBuilder(forPrintingError + " is not a valid number!\n");
} catch (IndexOutOfBoundsException e) {
s = "Sorry, index out of range!\n";
s = new StringBuilder(forPrintingError + "--> index out of range!\n");
}

return s;
return s.toString();
}

/**
* Marks a task as not done.
*
* @param input The index of the task that should be marked as not done.
*/
public String unmark(String input) {
String s;
public String unmark(String[] input) {
StringBuilder s = new StringBuilder();
String forPrintingError = "";
try {
int i = Integer.parseInt(input);
Task t = Duke.tasks.get(i - 1);
t.markAsNotDone();
Duke.tasks.set(i - 1, t);
s = "OK, I've marked this task as not done yet:\n " + t;

// Start at 1 beacuse indexes start at 1. Index 0 is "unmark"
for (int i = 1; i < input.length; i++) {
forPrintingError = input[i];
int idx = Integer.parseInt(input[i]);
Task t = Duke.tasks.get(idx - 1);
t.markAsNotDone();
Duke.tasks.set(idx - 1, t);
s.append("OK, I've marked this task as not done yet:\n ").append(t).append('\n');
}
} catch (NumberFormatException e) {
s = "Not a valid number!\n";
s = new StringBuilder(forPrintingError + " is not a valid number!\n");
} catch (IndexOutOfBoundsException e) {
s = "Sorry, index out of range!\n";
s = new StringBuilder(forPrintingError + "--> index out of range!\n");
}

return s;
return s.toString();
}

/**
* Deletes a task from the list.
*
* @param input The index of the task that should be deleted.
*/
public String delete(String input) {
String s;
public String delete(String[] input) {
StringBuilder s = new StringBuilder();
String forPrintingError = "";
try {
int i = Integer.parseInt(input);
Task t = Duke.tasks.remove(i - 1);
s = "Noted. I've removed this task:\n " + t;
s += String.format("\nNow you have %d tasks in the list.\n", Duke.tasks.size());

// Start at 1 beacuse indexes start at 1. Index 0 is "delete"
for (int i = 1; i < input.length; i++) {
forPrintingError = input[i];
int idx = Integer.parseInt(input[i]);
Task t = Duke.tasks.remove(idx - 1);
s.append("Noted. I've removed this task:\n ").append(t);
s.append(String.format("Now you have %d tasks in the list.\n\n", Duke.tasks.size()));
}
} catch (NumberFormatException e) {
s = "Not a valid number! Or perhaps add a ' '\n";
s = new StringBuilder(forPrintingError + " is not a valid number! Or perhaps add a ' '\n");
} catch (IndexOutOfBoundsException e) {
s = "Sorry, index out of range!\n";
s = new StringBuilder(forPrintingError + "--> index out of range!\n");
}

return s;
return s.toString();
}

/**
Expand Down

0 comments on commit 46e1569

Please sign in to comment.