Skip to content

Commit

Permalink
Merge pull request #2 from yadobler/branch-A-Assertions
Browse files Browse the repository at this point in the history
Merge branch-A-Assertions into master
  • Loading branch information
yadobler authored Sep 11, 2024
2 parents bdc4bae + 387fb87 commit c4f8ad2
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/main/java/yappingbot/commands/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CommandDispatcher {
* @param ui UI interface to output text
*/
public CommandDispatcher(Ui ui) {
assert ui != null;
this.ui = ui;
}

Expand All @@ -35,6 +36,7 @@ public CommandDispatcher(Ui ui) {
* @return TaskList of the whole task list, not filtered
*/
public TaskList resetView(TaskList currentUserList, boolean silent) {
assert currentUserList != null;
// reset the view to main parent
TaskList userList = currentUserList;
while (userList instanceof TaskListFilterView) {
Expand All @@ -53,6 +55,7 @@ public TaskList resetView(TaskList currentUserList, boolean silent) {
* @param userList TaskList to be printed
*/
public void printUserList(TaskList userList) {
assert userList != null;
if (userList.isEmpty()) {
ui.println("List is empty!");
}
Expand Down Expand Up @@ -91,6 +94,9 @@ public void printUserList(TaskList userList) {
* @param userList TaskList to be searched.
*/
public void changeTaskListStatus(int i, boolean isTaskDone, TaskList userList) {
assert userList != null;
assert userList.size() > i;

Task t = userList.get(i);
t.setTaskDone(isTaskDone);
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -118,6 +124,8 @@ public void changeTaskListStatus(int i, boolean isTaskDone, TaskList userList) {
* @param userList TaskList to be searched.
*/
public void deleteTask(int i, TaskList userList) {
assert userList != null;
assert userList.size() > i;
Task t = userList.deleteTask(i);
ui.print(ReplyTextMessages.DELETED_TEXT
+ "\n"
Expand All @@ -144,6 +152,9 @@ public void createNewTask(
TaskTypes taskTypes,
TaskList userList)
throws YappingBotIncorrectCommandException {
assert userList != null;
assert userInputSpliced != null;

Task newTask;
String taskName = null;
String command = null;
Expand Down Expand Up @@ -270,6 +281,9 @@ public void createNewTask(
* Tasks.
*/
public TaskList findStringInTasks(String searchString, TaskList userList) {
assert searchString != null;
assert userList != null;

StringBuilder sb = new StringBuilder();
String searchStringSanitized = searchString.replaceAll("\n", "");
sb.append(String.format(ReplyTextMessages.FIND_STRING_INIT_1s, searchStringSanitized));
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/yappingbot/commands/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public Parser() {
*/
public CommandTypes parseCommand(String commandString)
throws YappingBotUnknownCommandException {
assert commandString != null;
assert commandsHashMap != null;
if (commandString.toLowerCase().trim().isEmpty()) {
throw new YappingBotUnknownCommandException();
} else {
Expand All @@ -67,13 +69,14 @@ public CommandTypes parseCommand(String commandString)
* @throws YappingBotInvalidTaskNumberException Exception if given String is not a valid
* integer.
*/
public static int parseTaskNumberSelected(String userInputSlice)
public static int parseTaskNumberSelected(String userInputSlices)
throws YappingBotInvalidTaskNumberException {
assert userInputSlices != null;
int i;
try {
i = Integer.parseInt(userInputSlice) - 1;
i = Integer.parseInt(userInputSlices) - 1;
} catch (NumberFormatException ex) {
throw new YappingBotInvalidTaskNumberException(userInputSlice);
throw new YappingBotInvalidTaskNumberException(userInputSlices);
}
return i;
}
Expand All @@ -89,6 +92,7 @@ public static int parseTaskNumberSelected(String userInputSlice)
*/
public static void checkMinimumArgsAvailable(String[] userInputSlices, int i)
throws YappingBotOobException {
assert userInputSlices != null;
if ((userInputSlices.length - 1) < i) {
throw new YappingBotOobException(ReplyTextMessages.NOT_ENOUGH_ARGUMENTS, i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public YappingBotIncorrectCommandException(String usageMessage, String userInput
*/
public static YappingBotIncorrectCommandException withUserInputArray(String usageMessage,
String[] userInput) {
assert usageMessage != null;
assert userInput != null;
StringBuilder sb = new StringBuilder();
for (String s : userInput) {
sb.append(s);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/yappingbot/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Storage {
* disk with.
*/
public Storage(String savefilePath) {
assert savefilePath != null;
this.savefilePath = savefilePath;
}

Expand Down Expand Up @@ -58,6 +59,7 @@ public ArrayList<String> loadListFromFile() throws YappingBotSaveFileNotFoundExc
* @throws YappingBotSaveFileIoException Exception if file is not accessible.
*/
public void saveListToFile(ArrayList<String> userListRaw) throws YappingBotSaveFileIoException {
assert userListRaw != null;
try (BufferedWriter bw = new BufferedWriter(new FileWriter(savefilePath))) {
for (String t : userListRaw) {
bw.write(t);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/yappingbot/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public Deadline(String taskName, boolean taskDone, String deadline)
throws YappingBotIncorrectCommandException {
super(taskName, taskDone);
super.setTaskType(TaskTypes.DEADLINE);

assert deadline != null;
this.setDeadline(deadline);
}

Expand All @@ -59,6 +61,7 @@ public Deadline(String taskName, boolean taskDone, String deadline)
* @return String deadline in "YYYY-MM-DD" format
*/
public String getDeadline() {
assert deadline != null;
return deadline.toString();
}

Expand All @@ -70,6 +73,7 @@ public String getDeadline() {
* @throws YappingBotIncorrectCommandException If the provided date String is not valid format.
*/
public void setDeadline(String deadline) throws YappingBotIncorrectCommandException {
assert deadline != null;
try {
this.deadline = LocalDate.parse(deadline);
} catch (DateTimeParseException e) {
Expand All @@ -88,6 +92,7 @@ public String getTaskTypeSymbol() {

@Override
public String toString() {
assert deadline != null;
return String.format(
"%s (by: %s)",
super.getTaskName(),
Expand All @@ -96,13 +101,15 @@ public String toString() {

@Override
public String serialize() {
assert deadline != null;
return String.format("%s:%s",
super.serialize(),
this.getDeadline().replaceAll(":", "/colon"));
}

@Override
public void deserialize(String[] stringDataSlices) throws YappingBotInvalidSaveFileException {
assert stringDataSlices != null;
if (stringDataSlices.length < 4) {
throw new YappingBotInvalidSaveFileException(
INVALID_SAVE_FILE_EXCEPTION_MISSING_VALUES);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/yappingbot/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public Event(String taskName, boolean taskDone, String startTime, String endTime
throws YappingBotIncorrectCommandException {
super(taskName, taskDone);
super.setTaskType(TaskTypes.EVENT);

assert startTime != null;
assert endTime != null;
this.setStartTime(startTime);
this.setEndTime(endTime);
}
Expand All @@ -61,6 +64,7 @@ public Event(String taskName, boolean taskDone, String startTime, String endTime
* @return starting date as String formatted "YYYY-MM-DD".
*/
public String getStartTime() {
assert startTime != null;
return startTime.toString();
}

Expand All @@ -72,6 +76,7 @@ public String getStartTime() {
* @throws YappingBotIncorrectCommandException If the provided date String is not valid format.
*/
public void setStartTime(String startTime) throws YappingBotIncorrectCommandException {
assert startTime != null;
try {
this.startTime = LocalDate.parse(startTime);
} catch (DateTimeParseException e) {
Expand All @@ -87,6 +92,7 @@ public void setStartTime(String startTime) throws YappingBotIncorrectCommandExce
* @return ending date as String formatted "YYYY-MM-DD".
*/
public String getEndTime() {
assert endTime != null;
return endTime.toString();
}

Expand All @@ -98,6 +104,7 @@ public String getEndTime() {
* @throws YappingBotIncorrectCommandException If the provided date String is not valid format.
*/
public void setEndTime(String endTime) throws YappingBotIncorrectCommandException {
assert endTime != null;
try {
this.endTime = LocalDate.parse(endTime);
} catch (DateTimeParseException e) {
Expand All @@ -114,6 +121,9 @@ public String getTaskTypeSymbol() {

@Override
public String toString() {
assert getTaskName() != null;
assert startTime != null;
assert endTime != null;
return String.format("%s (from: %s to: %s)",
super.getTaskName(),
this.startTime.format(DateTimeFormatter.ofPattern("MMM d yyyy")),
Expand All @@ -123,6 +133,8 @@ public String toString() {

@Override
public String serialize() {
assert startTime != null;
assert endTime != null;
return String.format("%s:%s:%s",
super.serialize(),
this.getStartTime().replaceAll(":", "/colon"),
Expand All @@ -132,6 +144,7 @@ public String serialize() {

@Override
public void deserialize(String[] stringDataSlices) throws YappingBotInvalidSaveFileException {
assert stringDataSlices != null;
if (stringDataSlices.length < 5) {
throw new YappingBotInvalidSaveFileException(
ReplyTextMessages.INVALID_SAVE_FILE_EXCEPTION_MISSING_VALUES
Expand All @@ -148,6 +161,7 @@ public void deserialize(String[] stringDataSlices) throws YappingBotInvalidSaveF

@Override
public boolean isStringFoundInTask(String searchString) {
assert searchString != null;
// abuse the shortcircuiting
return (super.isStringFoundInTask(searchString)
|| getStartTime().contains(searchString)
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/yappingbot/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Task() {
* @param isTaskDone Boolean of whether the task is marked or unmarked as done.
*/
public Task(String taskName, boolean isTaskDone) {
assert taskName != null;
this.taskName = taskName;
this.isTaskDone = isTaskDone;
}
Expand All @@ -37,6 +38,7 @@ public Task(String taskName, boolean isTaskDone) {
* @return TaskTypes type of task.
*/
public TaskTypes getTaskType() {
assert taskType != null;
return taskType;
}

Expand Down Expand Up @@ -89,6 +91,7 @@ public String getTaskDoneCheckmark() {

@Override
public String toString() {
assert taskName != null;
return String.format("Name: %s, Completed: %s", this.taskName, this.isTaskDone);
}

Expand All @@ -100,6 +103,8 @@ public String toString() {
* @return String of serialized format of the task.
*/
public String serialize() {
assert taskName != null;
assert taskType != null;
return String.format("%s:%s:%s",
taskType,
taskName.replaceAll(":", "/colon"),
Expand All @@ -118,6 +123,7 @@ public String serialize() {
* String arrays.
*/
public void deserialize(String[] stringDataSlices) throws YappingBotInvalidSaveFileException {
assert stringDataSlices != null;
if (stringDataSlices.length < 3) {
throw new YappingBotInvalidSaveFileException(
INVALID_SAVE_FILE_EXCEPTION_MISSING_VALUES
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/yappingbot/tasks/tasklist/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public TaskList() {
* @param tasksRaw ArrayList of Strings, each a line that denotes a serialized task.
*/
public YappingBotInvalidSaveFileException generateFromRaw(ArrayList<String> tasksRaw) {
assert tasksRaw != null;

ArrayList<Exception> errorLists = new ArrayList<>();
for (String taskIndividualRaw : tasksRaw) {
String[] s = taskIndividualRaw.split(":");
Expand All @@ -43,6 +45,7 @@ public YappingBotInvalidSaveFileException generateFromRaw(ArrayList<String> task
errorLists.add(e);
}
}

if (!errorLists.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (Exception e : errorLists) {
Expand All @@ -61,6 +64,7 @@ public YappingBotInvalidSaveFileException generateFromRaw(ArrayList<String> task
* @return ArrayList of Strings of each serialized task.
*/
public ArrayList<String> toRawFormat() {
assert tasks != null;
ArrayList<String> taskListRaw = new ArrayList<>();
for (Task t : tasks) {
taskListRaw.add(t.serialize());
Expand All @@ -74,6 +78,8 @@ public ArrayList<String> toRawFormat() {
* @param task Task to be added.
*/
public void addTask(Task task) {
assert tasks != null;
assert task != null;
tasks.add(task);
size += 1;
}
Expand All @@ -86,6 +92,10 @@ public void addTask(Task task) {
* @throws YappingBotOobException Exception if index provided is out of bounds.
*/
public Task deleteTask(int index) throws YappingBotOobException {
assert tasks != null;
assert index < size;
assert tasks.size() == size;

Task t = get(index);
tasks.remove(index);
size -= 1;
Expand All @@ -98,6 +108,7 @@ public Task deleteTask(int index) throws YappingBotOobException {
* @return boolean of whether list is empty.
*/
public boolean isEmpty() {
assert tasks.size() == size;
return size == 0;
}

Expand All @@ -108,6 +119,7 @@ public boolean isEmpty() {
*/

public int size() {
assert tasks.size() == size;
return size;
}

Expand All @@ -119,6 +131,8 @@ public int size() {
* @throws YappingBotOobException Exception if index provided is out of bounds.
*/
public Task get(int index) throws YappingBotOobException {
assert tasks != null;
assert tasks.size() == size;
if (index < 0 || index >= size) {
throw new YappingBotOobException(ReplyTextMessages.SELECT_TASK_MISSING_TEXT_1d, index);
}
Expand All @@ -132,11 +146,13 @@ public Iterator<Task> iterator() {

@Override
public void forEach(Consumer<? super Task> action) {
assert tasks != null;
tasks.forEach(action);
}

@Override
public Spliterator<Task> spliterator() {
assert tasks != null;
return tasks.spliterator();
}
}
3 changes: 3 additions & 0 deletions src/main/java/yappingbot/ui/gui/DialogBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ private DialogBox(String text, Image img) throws IOException {
fxmlLoader.setRoot(this);
fxmlLoader.load();

assert text != null;
dialog.setText(text);

assert img != null;
displayPicture.setImage(img);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/yappingbot/ui/gui/MainGuiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void start(Stage stage) {


stage.show();

YappingBot yp = new YappingBot(ui, new Storage(Launcher.getSavefilePath()));
Thread ypLogic = new Thread(() -> {
yp.start();
Expand Down
Loading

0 comments on commit c4f8ad2

Please sign in to comment.