Skip to content

Commit

Permalink
Abstract out lengthier method
Browse files Browse the repository at this point in the history
Some methods are still quite lengthy

They make it harder to follow the logic

Use SLAP to abstract some longer method

This is to make the method readable
  • Loading branch information
cheahTJ committed Feb 23, 2024
1 parent e3a2d33 commit f447992
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 45 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test {
}

application {
mainClass.set("pingmebot.PingMe")
mainClass.set("pingmebot.Launcher")
}

shadowJar {
Expand Down
6 changes: 6 additions & 0 deletions data/PingMeData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
todo | 0 | proj
deadline | 0 | jkjk | May 05 2024 1800
todo | 0 | dasds
todo | 1 | dsadsa
event | 0 | dsads | 1800 | 1900
deadline | 0 | a | Jan 01 2000 1800
12 changes: 12 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ Now you have X tasks in the list.

where X is the current total number of tasks in your task list

## Saving your task: `bye`

This command will save all your tasks internally in a file. Bot will say bye to you.

> Format: `bye`
Example: `bye`

```
Bye. Hope to see you again soon!
```


## Viewing all tasks: `list`

Expand Down
100 changes: 64 additions & 36 deletions src/main/java/pingmebot/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ public AddCommand parseDeadlineCommand() throws PingMeException {
}
}

System.out.println("By:" + by.toString());
System.out.println("Description:" + description.toString());


if (by.toString().isEmpty() || description.toString().isEmpty()) {
throw new PingMeException("You have missing fields! "
+ "You need a task description & a deadline to finish your task, try again!");
Expand Down Expand Up @@ -186,7 +182,6 @@ public AddCommand parseEventsCommand() throws PingMeException {
throw new PingMeException("You having missing fields! "
+ "You need a task description, start and end date/time for your task, try again!");
}

return new AddCommand(new Events(description.toString().strip(),
start.toString().strip(), end.toString().strip()));
}
Expand All @@ -209,13 +204,17 @@ public MarkCommand parseMarkCommand(int currentNumOfTask) throws PingMeException
+ "Please specify the task you wish to mark and try again!");
}

if (Integer.parseInt(words.get(1)) > currentNumOfTask
|| Integer.parseInt(words.get(1)) <= 0) {
throw new PingMeException("You have currently " + currentNumOfTask + " tasks. "
+ "You cannot mark task larger or smaller than this!");

} else {
return new MarkCommand(Integer.parseInt(words.get(1)) - 1);
String taskNumberToMark = words.get(1);
try {
int taskNumber = Integer.parseInt(taskNumberToMark);
if (taskNumber > currentNumOfTask || taskNumber <= 0) {
throw new PingMeException("You have currently " + currentNumOfTask + " tasks. "
+ "You cannot mark a task larger than this or smaller than 1!");
} else {
return new MarkCommand(taskNumber - 1);
}
} catch (NumberFormatException e) {
throw new PingMeException("Invalid task number. '" + taskNumberToMark + "' is not a valid integer.");
}
}

Expand All @@ -237,15 +236,18 @@ public UnmarkCommand parseUnmarkCommand(int currentNumOfTask) throws PingMeExcep
+ "Please specify the task you wish to un-mark and try again!");
}

if (Integer.parseInt(words.get(1)) > currentNumOfTask
|| Integer.parseInt(words.get(1)) <= 0) {
throw new PingMeException("You have currently " + currentNumOfTask + " tasks. "
+ "You cannot un-mark task larger or smaller than this!");

} else {
return new UnmarkCommand(Integer.parseInt(words.get(1)) - 1);
String taskNumberToMark = words.get(1);
try {
int taskNumber = Integer.parseInt(taskNumberToMark);
if (taskNumber > currentNumOfTask || taskNumber <= 0) {
throw new PingMeException("You have currently " + currentNumOfTask + " tasks. "
+ "You cannot un-mark a task larger than this or smaller than 1!");
} else {
return new UnmarkCommand(taskNumber - 1);
}
} catch (NumberFormatException e) {
throw new PingMeException("Invalid task number. '" + taskNumberToMark + "' is not a valid integer.");
}

}

/**
Expand All @@ -266,13 +268,19 @@ public DeleteCommand parseDeleteCommand(int currentNumOfTask) throws PingMeExcep
+ "you want to delete and try again!");
}

if (Integer.parseInt(words.get(1)) > currentNumOfTask
|| Integer.parseInt(words.get(1)) <= 0) {
throw new PingMeException("You have currently " + currentNumOfTask + " tasks. "
+ "You cannot delete task larger or smaller than this!");
} else {
return new DeleteCommand(Integer.parseInt(words.get(1)) - 1);
String taskNumberToMark = words.get(1);
try {
int taskNumber = Integer.parseInt(taskNumberToMark);
if (taskNumber > currentNumOfTask || taskNumber <= 0) {
throw new PingMeException("You have currently " + currentNumOfTask + " tasks. "
+ "You cannot delete task larger than this or smaller than 1!");
} else {
return new DeleteCommand(taskNumber - 1);
}
} catch (NumberFormatException e) {
throw new PingMeException("Invalid task number. '" + taskNumberToMark + "' is not a valid integer.");
}

}

/**
Expand Down Expand Up @@ -319,29 +327,48 @@ public PostponeCommand parsePostponeCommand() throws PingMeException {

if (indexOfFrom != -1) {
if (indexOfTo == -1) {
throw new PingMeException("Make sure to include "
+ "/from ___ /to ___ for events object");
throw new PingMeException("Make sure to include /by ___ for deadline task"
+ " and /from ___ /to ___ for events task");
}
} else if (indexOfBy == -1) {
throw new PingMeException("Make sure to include /by ___ for deadline object"
+ "and /from ___ /to ___ for events object");
throw new PingMeException("Make sure to include /by ___ for deadline task"
+ " and /from ___ /to ___ for events task");
}
PostponeCommand pc = userCommandToPostponeCommand(taskToPostpone, indexOfBy, indexOfFrom, indexOfTo);
return pc;
}


/**
* Returns a postpone command object after retrieving all the necessary timings from user input.
*
* @param taskToPostpone The task number the user wants to postpone.
* @param indexOfBy The index of the word 'by' from user input.
* @param indexOfFrom The index of the word 'from' from user input.
* @param indexOfTo The index of the word 'to' from user input.
* @return A postpone command.
* @throws PingMeException If the user inputs the deadline time format wrongly or has an invalid command.
*/
public PostponeCommand userCommandToPostponeCommand(int taskToPostpone, int indexOfBy,
int indexOfFrom, int indexOfTo) throws PingMeException {
StringBuilder from = new StringBuilder();
StringBuilder to = new StringBuilder();
StringBuilder by = new StringBuilder();

if (words.size() < 6) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy HHmm");
for (int i = 2; i < words.size(); i++) {
if (i > indexOfBy) {
by.append(" ").append(words.get(i));
}
}
return new PostponeCommand(taskToPostpone - 1, " ", " ",
LocalDateTime.parse(by.toString().trim(), formatter));
try {
return new PostponeCommand(taskToPostpone - 1, "", "",
LocalDateTime.parse(by.toString().trim(), formatter));
} catch (DateTimeParseException e) {
throw new PingMeException("Input your deadline by time in the"
+ " d/M/yyyy HHmm format");
}
}

if (words.size() > 5) {
for (int i = 2; i < words.size(); i++) {
if (i > indexOfFrom && i < indexOfTo) {
Expand All @@ -350,9 +377,10 @@ public PostponeCommand parsePostponeCommand() throws PingMeException {
to.append(words.get(i));
}
}
return new PostponeCommand(taskToPostpone - 1, from.toString().trim()
, to.toString().trim(), null);
return new PostponeCommand(taskToPostpone - 1, from.toString().trim(),
to.toString().trim(), null);
}
throw new PingMeException("Try again with your command!");
}

}
2 changes: 1 addition & 1 deletion src/main/java/pingmebot/PingMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* It allows user to interact with it via command line interface.
*/
public class PingMe {
protected static String filePath = "./data/dukeData.txt";
protected static String filePath = "./data/PingMeData.txt";
private Storage storage;
private TaskList tasks;
private UI ui;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/pingmebot/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public void postponeTask(int taskNum, String timingStart,
String timingEnd, LocalDateTime by) throws PingMeException {
Task t = tasks.get(taskNum);

if (t instanceof Deadline && by == null) {
throw new PingMeException("You might have keyed in the details meant for"
+ " events task as the deadline task");
}

if (t instanceof Events && timingStart.isEmpty() && timingEnd.isEmpty()) {
throw new PingMeException("You might have keyed in the details meant for"
+ " deadline task as the events task");
}

if (t instanceof Deadline) {
Deadline d = (Deadline) t;
d.setDeadlineByTiming(by);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/pingmebot/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public AddCommand(Task task) {
@Override
public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException {
tasks.addTask(task);
tasks.updateTaskToStorage(storage);
ui.additionToTasksText(task, tasks);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/pingmebot/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public DeleteCommand(int taskNumber) {
@Override
public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException {
ui.deletionToTasksText(taskNumber, tasks);
tasks.updateTaskToStorage(storage);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/pingmebot/command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ExitCommand extends Command {
*/
@Override
public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException {
tasks.updateTaskToStorage(storage);
ui.sayGoodbye();
}
}
1 change: 0 additions & 1 deletion src/main/java/pingmebot/command/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public MarkCommand(int taskNumber) {
* @throws PingMeException If an error occurs during the execution process.
*/
public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException {
tasks.updateTaskToStorage(storage);
ui.markTaskText(taskNumber, tasks);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/pingmebot/command/PostponeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pingmebot.Storage;
import pingmebot.TaskList;
import pingmebot.UI;
import pingmebot.task.Deadline;



/**
Expand Down Expand Up @@ -47,7 +47,6 @@ public PostponeCommand(int taskNumber, String timingStart, String timingEnd, Loc
@Override
public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException {
tasks.postponeTask(taskNumber, timingStart, timingEnd, by);
tasks.updateTaskToStorage(storage);
ui.postponeTaskText(taskNumber, tasks);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/pingmebot/command/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public UnmarkCommand(int taskNumber) {
* @throws PingMeException If an error occurs during the execution process.
*/
public void execute(TaskList tasks, Storage storage, UI ui) throws PingMeException {
tasks.updateTaskToStorage(storage);
ui.unmarkTaskText(taskNumber, tasks);
}

Expand Down
7 changes: 6 additions & 1 deletion src/test/java/pingmebot/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

import org.junit.jupiter.api.Test;

import pingmebot.command.*;
import pingmebot.command.AddCommand;
import pingmebot.command.DeleteCommand;
import pingmebot.command.FindCommand;
import pingmebot.command.MarkCommand;
import pingmebot.command.PostponeCommand;
import pingmebot.command.UnmarkCommand;
import pingmebot.task.Deadline;
import pingmebot.task.Events;
import pingmebot.task.ToDos;
Expand Down

0 comments on commit f447992

Please sign in to comment.