Skip to content

Commit

Permalink
Merge branch 'master' into branch-A-BetterGui
Browse files Browse the repository at this point in the history
  • Loading branch information
HarishB99 committed Feb 19, 2025
2 parents 24a17dd + 0e7314d commit 4f6d54f
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 16 deletions.
13 changes: 11 additions & 2 deletions src/main/java/bhaymax/command/DeadlineCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bhaymax.command;

import bhaymax.controller.MainWindow;
import bhaymax.exception.TaskAlreadyExistsException;
import bhaymax.exception.command.AttemptToCreateDuplicateTaskException;
import bhaymax.exception.command.InvalidCommandFormatException;
import bhaymax.exception.file.FileWriteException;
import bhaymax.storage.Storage;
import bhaymax.task.TaskList;
Expand Down Expand Up @@ -31,9 +34,15 @@ public DeadlineCommand(String taskDescription, String deadline) {
}

@Override
public void execute(TaskList taskList, MainWindow mainWindowController, Storage storage) throws FileWriteException {
public void execute(TaskList taskList, MainWindow mainWindowController, Storage storage)
throws FileWriteException, InvalidCommandFormatException {
Deadline newDeadline = new Deadline(this.taskDescription, this.deadline);
int taskListCount = taskList.addTask(newDeadline);
int taskListCount;
try {
taskListCount = taskList.addTask(newDeadline);
} catch (TaskAlreadyExistsException e) {
throw new AttemptToCreateDuplicateTaskException();
}
storage.saveTasks(taskList);
String response = String.format(
DeadlineCommand.RESPONSE_FORMAT,
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/bhaymax/command/EventCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bhaymax.command;

import bhaymax.controller.MainWindow;
import bhaymax.exception.TaskAlreadyExistsException;
import bhaymax.exception.command.AttemptToCreateDuplicateTaskException;
import bhaymax.exception.command.InvalidCommandFormatException;
import bhaymax.exception.file.FileWriteException;
import bhaymax.storage.Storage;
import bhaymax.task.TaskList;
Expand Down Expand Up @@ -34,9 +37,15 @@ public EventCommand(String taskDescription, String start, String end) {
}

@Override
public void execute(TaskList taskList, MainWindow mainWindowController, Storage storage) throws FileWriteException {
public void execute(TaskList taskList, MainWindow mainWindowController, Storage storage)
throws FileWriteException, InvalidCommandFormatException {
Event newEvent = new Event(this.taskDescription, this.start, this.end);
int taskListCount = taskList.addTask(newEvent);
int taskListCount;
try {
taskListCount = taskList.addTask(newEvent);
} catch (TaskAlreadyExistsException e) {
throw new AttemptToCreateDuplicateTaskException();
}
storage.saveTasks(taskList);
String response = String.format(
RESPONSE_FORMAT,
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/bhaymax/command/TodoCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bhaymax.command;

import bhaymax.controller.MainWindow;
import bhaymax.exception.TaskAlreadyExistsException;
import bhaymax.exception.command.AttemptToCreateDuplicateTaskException;
import bhaymax.exception.command.InvalidCommandFormatException;
import bhaymax.exception.file.FileWriteException;
import bhaymax.storage.Storage;
import bhaymax.task.TaskList;
Expand All @@ -27,9 +30,15 @@ public TodoCommand(String taskDescription) {
}

@Override
public void execute(TaskList taskList, MainWindow mainWindowController, Storage storage) throws FileWriteException {
public void execute(TaskList taskList, MainWindow mainWindowController, Storage storage)
throws FileWriteException, InvalidCommandFormatException {
Todo newTodoTask = new Todo(this.taskDescription);
int taskListCount = taskList.addTask(newTodoTask);
int taskListCount;
try {
taskListCount = taskList.addTask(newTodoTask);
} catch (TaskAlreadyExistsException e) {
throw new AttemptToCreateDuplicateTaskException();
}
storage.saveTasks(taskList);
String response = String.format(
RESPONSE_FORMAT,
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/bhaymax/exception/TaskAlreadyExistsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package bhaymax.exception;

/**
* Thrown when an attempt to add a duplicate task to the tasks list occurs
*/
public class TaskAlreadyExistsException extends RuntimeException {
public static final String ERROR_MESSAGE = "You have already added this task to your list.";

public TaskAlreadyExistsException() {
super(TaskAlreadyExistsException.ERROR_MESSAGE);
}

public TaskAlreadyExistsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bhaymax.exception.command;

import bhaymax.exception.TaskAlreadyExistsException;

/**
* Thrown when a user attempts to add a task that already exists
*/
public class AttemptToCreateDuplicateTaskException extends InvalidCommandFormatException {
public AttemptToCreateDuplicateTaskException() {
super(TaskAlreadyExistsException.ERROR_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package bhaymax.exception.command;

/**
* Thrown when the end date for an event precedes its start date
* (which is obviously invalid as you can't have an event end
* before it even starts)
*/
public class InvalidTimeRangeForEventException extends InvalidCommandFormatException {
public static final String ERROR_MESSAGE = "The date range provided for event is incorrect. "
+ "End date should be after or equal to the start date";

public InvalidTimeRangeForEventException() {
super(InvalidTimeRangeForEventException.ERROR_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bhaymax.exception.file;

/**
* Thrown when there are duplicates in the tasks file
*/
public class DuplicateTaskInFileException extends InvalidFileFormatException {
public static final String ERROR_MESSAGE = "This line has been duplicated at least once.";

public DuplicateTaskInFileException(int lineNumber) {
super(lineNumber, DuplicateTaskInFileException.ERROR_MESSAGE);
}
}
4 changes: 4 additions & 0 deletions src/main/java/bhaymax/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import java.util.Optional;
import java.util.Scanner;

import bhaymax.exception.TaskAlreadyExistsException;
import bhaymax.exception.command.InvalidDateFormatInFileException;
import bhaymax.exception.file.DuplicateTaskInFileException;
import bhaymax.exception.file.FileWriteException;
import bhaymax.exception.file.InvalidFileFormatException;
import bhaymax.exception.file.UnrecognisedTaskTypeException;
Expand Down Expand Up @@ -94,6 +96,8 @@ public TaskList loadTasks()
return taskList;
} catch (DateTimeParseException e) {
throw new InvalidDateFormatInFileException(lineNumber);
} catch (TaskAlreadyExistsException e) {
throw new DuplicateTaskInFileException(lineNumber);
} catch (FileNotFoundException e) {
return taskList;
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/bhaymax/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Represents a generic task
*/
public class Task {
public abstract class Task implements Comparable<Task> {
public static final String DELIMITER = "|";
public static final String SERIAL_FORMAT = "%s " + Task.DELIMITER + " %d " + Task.DELIMITER + " %s";

Expand Down Expand Up @@ -52,4 +52,12 @@ public boolean hasSearchTerm(String searchTerm) {
public String toString() {
return "[" + this.getStatusIcon() + "] " + this.description;
}

@Override
public int compareTo(Task task) {
int differenceInType = this.type.compareTo(task.type);
return (differenceInType != 0)
? differenceInType
: this.description.toLowerCase().compareTo(task.description.toLowerCase());
}
}
14 changes: 13 additions & 1 deletion src/main/java/bhaymax/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import bhaymax.command.FilterOption;
import bhaymax.controller.MainWindow;
import bhaymax.exception.TaskAlreadyExistsException;
import bhaymax.task.timesensitive.TimeSensitiveTask;
import bhaymax.util.Pair;

Expand Down Expand Up @@ -40,7 +41,10 @@ public boolean isValidIndex(int index) {
* @param task an object of {@link Task} type to be added
* @return the number of tasks in the list after adding the new task
*/
public int addTask(Task task) {
public int addTask(Task task) throws TaskAlreadyExistsException {
if (this.tasks.contains(task)) {
throw new TaskAlreadyExistsException();
}
this.tasks.add(task);
return this.tasks.size();
}
Expand Down Expand Up @@ -92,7 +96,9 @@ public Pair<Task, Integer> removeTask(int index) {
* display dialog boxes with the tasks in this list
*/
public void showTasks(MainWindow mainWindowController) {
this.tasks.sort(Task::compareTo);
this.tasks.stream()
//.sorted()
.map(task -> (tasks.indexOf(task) + 1) + TaskList.TASK_LIST_BULLET_POINT_SEPARATOR + task)
.reduce((previousTask, nextTask)
-> previousTask + System.lineSeparator() + nextTask)
Expand All @@ -114,7 +120,9 @@ public void showTasks(MainWindow mainWindowController) {
*/
public void showTasksFilteredByDate(
String dateTime, FilterOption filterOption, MainWindow mainWindowController) {
this.tasks.sort(Task::compareTo);
String response = this.tasks.stream()
//.sorted()
.filter(task -> task instanceof TimeSensitiveTask)
.filter(timeSensitiveTask -> (
(TimeSensitiveTask) timeSensitiveTask).hasDateMatchingFilter(dateTime, filterOption))
Expand All @@ -133,7 +141,9 @@ public void showTasksFilteredByDate(
* display dialog boxes with the matched tasks in this list
*/
public void showTasksContainingSearchTerm(String searchTerm, MainWindow mainWindowController) {
this.tasks.sort(Task::compareTo);
String response = this.tasks.stream()
//.sorted()
.filter(task -> task.hasSearchTerm(searchTerm))
.map(task -> (this.tasks.indexOf(task) + 1) + TaskList.TASK_LIST_BULLET_POINT_SEPARATOR + task)
.reduce((previousTask, nextTask)
Expand All @@ -148,7 +158,9 @@ public void showTasksContainingSearchTerm(String searchTerm, MainWindow mainWind
* @return the {@code String} representation of the {@code TaskList} object that is suitable for saving to a file
*/
public String serialise() {
this.tasks.sort(Task::compareTo);
return this.tasks.stream()
//.sorted()
.map(Task::serialise)
.reduce((task1, task2)
-> task1 + System.lineSeparator() + task2)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/bhaymax/task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ private static MatchResult getMatchResult(int lineNumber, String serialisedTodo)
return matchResult;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Todo todoTask) {
return this.compareTo(todoTask) == 0;
} else {
return false;
}
}

@Override
public String toString() {
return "[T]" + super.toString();
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/bhaymax/task/timesensitive/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class Deadline extends TimeSensitiveTask {
*/
public Deadline(String description, String dueDateTime)
throws DateTimeParseException {
super(Deadline.TYPE, description);
super(Deadline.TYPE, description,
LocalDateTime.parse(dueDateTime, DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT)));
this.dueDateTime = LocalDateTime.parse(
dueDateTime, DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT));
}
Expand Down Expand Up @@ -165,6 +166,18 @@ boolean isOnDateTime(LocalDateTime dateTime) {
return this.dueDateTime.isEqual(dateTime);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Deadline deadline) {
return this.compareTo(deadline) == 0;
} else {
return false;
}
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + this.getDeadlineInOutputFormat() + ")";
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/bhaymax/task/timesensitive/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Scanner;
import java.util.regex.MatchResult;

import bhaymax.exception.command.InvalidTimeRangeForEventException;
import bhaymax.exception.file.InvalidTaskStatusException;
import bhaymax.exception.file.TaskDeSerialisationException;
import bhaymax.exception.file.WrongTaskFormatException;
Expand All @@ -25,7 +26,8 @@ public class Event extends TimeSensitiveTask {

// For construction of exception message
public static final String START_DATE_INPUT_FORMAT = "{start date: " + Parser.DATETIME_INPUT_FORMAT + "}";
public static final String END_DATE_INPUT_FORMAT = "{end date: " + Parser.DATETIME_INPUT_FORMAT + "}";
public static final String END_DATE_INPUT_FORMAT = "{end date (should be after or equal to start date): "
+ Parser.DATETIME_INPUT_FORMAT + "}";

private static final String SERIALISATION_FORMAT = "%s " + Task.DELIMITER + " %s " + Task.DELIMITER + " %s";
private static final String DE_SERIALISATION_FORMAT = "^E \\| ([0-1]) \\| (.+)"
Expand Down Expand Up @@ -55,10 +57,14 @@ public class Event extends TimeSensitiveTask {
* @see Parser#DATETIME_INPUT_FORMAT
*/
public Event(String description, String start, String end)
throws DateTimeParseException {
super(Event.TYPE, description);
throws DateTimeParseException, InvalidTimeRangeForEventException {
super(Event.TYPE, description,
LocalDateTime.parse(start, DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT)));
this.start = LocalDateTime.parse(start, DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT));
this.end = LocalDateTime.parse(end, DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT));
if (this.end.isBefore(this.start)) {
throw new InvalidTimeRangeForEventException();
}
}

@Override
Expand Down Expand Up @@ -103,7 +109,7 @@ private static Event getEvent(int lineNumber, MatchResult matchResult) throws Wr
String eventStart = matchResult.group(Event.REGEX_GROUP_START_DATE);
String eventEnd = matchResult.group(Event.REGEX_GROUP_END_DATE);
event = new Event(eventDescription, eventStart, eventEnd);
} catch (DateTimeParseException e) {
} catch (DateTimeParseException | InvalidTimeRangeForEventException e) {
throw new WrongTaskFormatException(
lineNumber, Event.NAME, Event.TYPE,
Event.START_DATE_INPUT_FORMAT, Event.END_DATE_INPUT_FORMAT);
Expand Down Expand Up @@ -134,15 +140,15 @@ private String getStartDateInInputFormat() {
}

private String getStartDateInOutputFormat() {
return this.start.format(DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT));
return this.start.format(DateTimeFormatter.ofPattern(Parser.DATETIME_OUTPUT_FORMAT));
}

private String getEndDateInInputFormat() {
return this.end.format(DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT));
}

private String getEndDateInOutputFormat() {
return this.end.format(DateTimeFormatter.ofPattern(Parser.DATETIME_INPUT_FORMAT));
return this.end.format(DateTimeFormatter.ofPattern(Parser.DATETIME_OUTPUT_FORMAT));
}

@Override
Expand Down Expand Up @@ -197,6 +203,18 @@ boolean isOnDateTime(LocalDateTime dateTime) {
|| this.end.isEqual(dateTime);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Event event) {
return this.compareTo(event) == 0;
} else {
return false;
}
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: "
Expand Down
Loading

0 comments on commit 4f6d54f

Please sign in to comment.