-
Notifications
You must be signed in to change notification settings - Fork 451
[Sherwin Poh Kai Xun] iP #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 21 commits
d839859
784ada6
3099a61
8225235
e4e2572
dd8baab
406d820
7fb4963
9ff00d5
10fca14
d5c5c83
188b6ab
2ed2893
47516e5
f8a5b52
1d97570
c730033
d1ad490
edebd71
4feab0a
a98da27
c0ab76b
151e8d7
a5257cb
1ddb630
54bd1f7
91f63af
36ac23c
6b1fa17
e16ee69
18b8e5b
3e1f1f7
66de513
525fa21
f56aba8
0be78e1
e686719
af7c1a5
6abe1b1
ba5770c
e74aa7c
5dec90c
9a2c9eb
b1ec315
6ae3eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,5 @@ bin/ | |
|
|
||
| /text-ui-test/ACTUAL.txt | ||
| text-ui-test/EXPECTED-UNIX.TXT | ||
|
|
||
| /data/ | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: duke.Duke | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package duke; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeParseException; | ||
|
|
||
| /** | ||
| * Represents a command to add tasks to the task list. | ||
| */ | ||
| public class AddCommand extends Command { | ||
| private String taskType; | ||
| private String description; | ||
| private LocalDate date; | ||
|
|
||
| /** | ||
| * Sole constructor for this class | ||
| * | ||
| * @param taskType Task type to add. | ||
| * @param description Task description. | ||
| * @param date Task date for events and deadlines. | ||
| * @throws DukeException If description is empty. | ||
| */ | ||
| public AddCommand(String taskType, String description, LocalDate date) throws DukeException { | ||
| if (description.equals("")) { | ||
| throw new DukeException("Please provide a description to the task."); | ||
| } | ||
| this.taskType = taskType; | ||
| this.description = description; | ||
| this.date = date; | ||
| } | ||
|
|
||
| /** | ||
| * Creates the task, adds it to the task list, updates the data file and displays the the new task to the UI. | ||
| * | ||
| * @param tasks Task list component. | ||
| * @param storage Storage component. | ||
| * @param ui UI component. | ||
| * @throws DukeException If invalid task type was provided. | ||
| */ | ||
| @Override | ||
| public void execute(TaskList tasks, Storage storage, Ui ui) throws DukeException { | ||
| Task newTask; | ||
| switch (taskType) { | ||
| case "todo": | ||
| newTask = new Todo(description); | ||
| break; | ||
| case "deadline": | ||
| newTask = new Deadline(description, date); | ||
| break; | ||
| case "event": | ||
| newTask = new Event(description, date); | ||
| break; | ||
| default: | ||
| throw new DukeException("Invalid task type provided!"); | ||
| } | ||
| tasks.addTask(newTask); | ||
| storage.updateTasks(tasks); | ||
| ui.showAddedNewTask(newTask, tasks); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package duke; | ||
|
|
||
| /** | ||
| * Represents a generic command. | ||
| */ | ||
| public abstract class Command { | ||
| /** | ||
| * Executes the command. | ||
| * | ||
| * @param tasks Task list component. | ||
| * @param storage Storage component. | ||
| * @param ui UI component. | ||
| */ | ||
| public abstract void execute(TaskList tasks, Storage storage, Ui ui); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package duke; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| /** | ||
| * Represents a deadline task. | ||
| */ | ||
| public class Deadline extends Task { | ||
| protected LocalDate by; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should perhaps be private since there is already a getter method that exposes this variable |
||
|
|
||
| /** | ||
| * Constructor for deadline class. | ||
| * | ||
| * @param description Deadline description. | ||
| * @param by Due date of deadline. | ||
| */ | ||
| public Deadline(String description, LocalDate by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the task's details in a String. | ||
| * | ||
| * @return Task's details in a String. | ||
| */ | ||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + " (by: " + by.format(DateTimeFormatter.ofPattern("MMM dd yyyy")) + ")"; | ||
| } | ||
|
|
||
| /** | ||
| * Returns deadline's due date. | ||
| * | ||
| * @return Deadline's due date. | ||
| */ | ||
| public LocalDate getDueDate() { | ||
| return by; | ||
| } | ||
|
|
||
| /** | ||
| * Returns task's details in a format that will be stored in the data file. | ||
| * | ||
| * @return Task's details in a format that will be stored in the data file. | ||
| */ | ||
| @Override | ||
| public String toStringData() { | ||
| return "D" + super.toStringData() + "|" + by; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean matches(String query) { | ||
| return super.matches(query) | ||
| || by.format(DateTimeFormatter.ofPattern("MMM dd yyyy")).toLowerCase().contains(query.toLowerCase()) | ||
| || query.equalsIgnoreCase("deadline"); | ||
| } | ||
|
Comment on lines
+52
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment to explain this logic would be great |
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||
| package duke; | ||||||
|
|
||||||
| /** | ||||||
| * Represents the delete task command. | ||||||
| */ | ||||||
| public class DeleteCommand extends Command { | ||||||
| private int taskIndex; | ||||||
|
|
||||||
| /** | ||||||
| * Constructor for DeleteCommand | ||||||
| * | ||||||
| * @param taskIndex Index of task to delete. | ||||||
| */ | ||||||
| public DeleteCommand(int taskIndex) { | ||||||
| this.taskIndex = taskIndex; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Removes task from task list, updates tasks in data file and shows that task has been removed on UI. | ||||||
| * | ||||||
| * @param tasks Task list component. | ||||||
| * @param storage Storage component. | ||||||
| * @param ui UI component. | ||||||
| */ | ||||||
| @Override | ||||||
| public void execute(TaskList tasks, Storage storage, Ui ui) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if (tasks.getLength() < taskIndex || 0 >= taskIndex) { | ||||||
| throw new DukeException("Invalid task index provided!"); | ||||||
| } | ||||||
| Task removedTask = tasks.removeTask(taskIndex); | ||||||
| storage.updateTasks(tasks); | ||||||
| ui.showRemovedTask(removedTask, tasks); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package duke; | ||
|
|
||
| /** | ||
| * The Duke Application. | ||
| */ | ||
| public class Duke { | ||
| private Storage storage; | ||
| private TaskList tasks; | ||
| private Ui ui; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be private final |
||
|
|
||
| /** | ||
| * Constructor for the Duke class. | ||
| * Initialises the UI, Storage and loads tasks from data file into Task List. | ||
| * | ||
| * @param dataFolderPath Path to data folder. | ||
| * @param dataFilePath Path to data file. | ||
| */ | ||
| public Duke(String dataFolderPath, String dataFilePath) { | ||
| ui = new Ui(); | ||
| storage = new Storage(dataFolderPath, dataFilePath); | ||
| try { | ||
| tasks = new TaskList(storage.load()); | ||
| } catch (DukeException e) { | ||
| ui.showLoadingError(); | ||
| tasks = new TaskList(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Starts the Duke application. | ||
| */ | ||
| public void run() { | ||
| ui.showWelcome(); | ||
| boolean isExit = false; | ||
| while (!isExit) { | ||
| try { | ||
| String fullCommand = ui.readCommand(); | ||
| ui.showLine(); // show the divider line ("_______") | ||
| Command c = Parser.parse(fullCommand); | ||
| c.execute(tasks, storage, ui); | ||
| isExit = c instanceof ExitCommand; | ||
| } catch (DukeException e) { | ||
| ui.showError(e.getMessage()); | ||
| } finally { | ||
| ui.showLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Main function. | ||
| * | ||
| * @param args Program input arguments. | ||
| */ | ||
| public static void main(String[] args) { | ||
| new Duke("./data", "./data/tasks.txt").run(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package duke; | ||
|
|
||
| /** | ||
| * Represents an expected exception that occurred within the operation of the Duke application. | ||
| */ | ||
| public class DukeException extends RuntimeException { | ||
|
|
||
| /** | ||
| * Constructor for DukeException | ||
| * | ||
| * @param errorMessage Error message. | ||
| */ | ||
| public DukeException(String errorMessage) { | ||
| super(errorMessage); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package duke; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| /** | ||
| * Represents an Event task. | ||
| */ | ||
| public class Event extends Task { | ||
| protected LocalDate at; | ||
|
|
||
| /** | ||
| * Constructor for Event. | ||
| * | ||
| * @param description Event's description. | ||
| * @param at Event's date. | ||
| */ | ||
| public Event(String description, LocalDate at) { | ||
| super(description); | ||
| this.at = at; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the Event's details in a String. | ||
| * | ||
| * @return Event's details in a String. | ||
| */ | ||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + " (at: " + at.format(DateTimeFormatter.ofPattern("MMM dd yyyy")) + ")"; | ||
| } | ||
|
|
||
| /** | ||
| * Returns Event's date. | ||
| * @return Event's date. | ||
| */ | ||
| public LocalDate getEventDate() { | ||
| return at; | ||
| } | ||
|
|
||
| /** | ||
| * Returns task's details in a format that will be stored in the data file. | ||
| * | ||
| * @return Task's details in a format that will be stored in the data file. | ||
| */ | ||
| @Override | ||
| public String toStringData() { | ||
| return "E" + super.toStringData() + "|" + at; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean matches(String query) { | ||
| return super.matches(query) | ||
| || at.format(DateTimeFormatter.ofPattern("MMM dd yyyy")).toLowerCase().contains(query.toLowerCase()) | ||
| || query.equalsIgnoreCase("event"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package duke; | ||
|
|
||
| /** | ||
| * Represents an exit command that will end the application run. | ||
| */ | ||
| public class ExitCommand extends Command { | ||
|
|
||
| /** | ||
| * Displays goodbye message on UI. | ||
| * | ||
| * @param tasks Task list component. | ||
| * @param storage Storage component. | ||
| * @param ui UI component. | ||
| */ | ||
| @Override | ||
| public void execute(TaskList tasks, Storage storage, Ui ui) { | ||
| ui.showGoodbye(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package duke; | ||
|
|
||
| public class FindCommand extends Command{ | ||
| private String query; | ||
|
|
||
| public FindCommand(String query) throws DukeException { | ||
| if (query.equals("")) { | ||
| throw new DukeException("Query should not be empty."); | ||
| } | ||
| this.query = query; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(TaskList tasks, Storage storage, Ui ui) { | ||
| ui.showMatchingTasks(query, tasks); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding standard for CS2103 requires switch statements to not have tab for case, take note to edit the default IDE settings!