Skip to content

Commit

Permalink
Completed C-Sort, C-DetectDuplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiHawkEye committed Sep 12, 2019
1 parent ead332a commit 0ca5087
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 38 deletions.
4 changes: 4 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
<classpathentry kind="lib" path="/Users/kaizhe/Desktop/Modules/CS2103/javafx-sdk-11.0.2/lib/javafx.base.jar"/>
<classpathentry kind="lib" path="/Users/kaizhe/Desktop/Modules/CS2103/javafx-sdk-11.0.2/lib/javafx.graphics.jar"/>
<classpathentry kind="lib" path="/Users/kaizhe/Desktop/Modules/CS2103/javafx-sdk-11.0.2/lib/javafx.controls.jar"/>
<classpathentry kind="lib" path="/Users/kaizhe/Desktop/Modules/CS2103/javafx-sdk-11.0.2/lib/javafx.fxml.jar"/>
</classpath>
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'application'
id 'checkstyle'
id 'com.github.johnrengelman.shadow' version '5.1.0'
id 'org.openjfx.javafxplugin' version '0.0.7'
}

shadowJar {
Expand Down Expand Up @@ -31,6 +32,11 @@ repositories {
mavenCentral()
}

javafx {
version = "11.0.2"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}

application {
// Change this to your main class.
mainClassName = "duke.Duke"
Expand Down
31 changes: 10 additions & 21 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public Duke(String filePath) {
* @param args Arguments entered when main method is executed.
*/
public static void main(String[] args) {
Duke duke = new Duke(SAVE_PATH);
duke.run();
new Duke(SAVE_PATH).run();
}

/**
Expand All @@ -56,7 +55,14 @@ public static void main(String[] args) {
* @param input Input entered by user.
*/
public String getResponse(String input) {
return this.step(input);
try {
Command c = Parser.parse(input);
c.execute(tasks, ui, storage);
this.storage.save(tasks);
} catch (DukeException e) {
this.ui.showError(e.getMessage());
}
return this.ui.getOutput();
}

/**
Expand All @@ -68,8 +74,7 @@ public void run() {
boolean isExit = false;
while (!isExit) {
try {
String fullCommand = ui.readCommand();
Command c = Parser.parse(fullCommand);
Command c = Parser.parse(ui.readCommand());
c.execute(tasks, ui, storage);
storage.save(tasks);
isExit = c.isExit();
Expand All @@ -78,20 +83,4 @@ public void run() {
}
}
}

/**
* Step-wise execution of Duke.
*
* @param input User input.
*/
public String step(String input) {
try {
Command c = Parser.parse(input);
c.execute(tasks, ui, storage);
this.storage.save(tasks);
} catch (DukeException e) {
this.ui.showError(e.getMessage());
}
return this.ui.getOutput();
}
}
7 changes: 7 additions & 0 deletions src/main/java/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke;

import duke.util.Ui;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
Expand Down Expand Up @@ -29,6 +30,12 @@ public class MainWindow extends AnchorPane {
@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
Ui ui = new Ui();
ui.showWelcome();
String welcome = ui.getOutput();
dialogContainer.getChildren().add(
DialogBox.getDukeDialog(welcome, dukeImage)
);
}

public void setDuke(Duke d) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AddCommand(Task task) {
* @param storage Storage that stores the modified TaskList.
*/
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
tasks.add(this.task);
tasks.addTask(this.task);
ui.printResponse("Got it. I've added this task:\n "
+ this.task.toString() + "\n"
+ "Now you have " + tasks.size() + " tasks in the list.");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
TaskList tempList = new TaskList();
for (Task task : tasks) {
if (task.toString().contains(this.query)) {
tempList.add(task);
tempList.addTask(task);
}
}
ui.printResponse("Here are the matching tasks in your list:\n "
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/command/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package duke.command;

import duke.exception.DukeException;
import duke.util.Parser;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;
import duke.task.Task;

/**
* Command containing method for finding Tasks in TaskList.
*/
public class SortCommand extends Command {
private String field;

/**
* Constructor for SortCommand.
*
* @param field Field to sort tasks by.
*/
public SortCommand(String field) {
this.field = field;
}

/**
* Sorts Tasks in TaskList according to the specified field.
*
* @param tasks TaskList to sort Tasks.
* @param ui Ui for printing responses to the console.
* @param storage Storage that stores the modified TaskList.
*/
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
tasks.sort(field);
ui.printResponse("Here is the sorted list:\n"
+ tasks.toString() + "\n");
}

/**
* Returns boolean to initiate exit of program.
*
* @return False so program does not exit.
*/
public boolean isExit() {
return false;
}
}
Binary file modified src/main/java/data/duke.txt
Binary file not shown.
Binary file modified src/main/java/duke/Duke.class
Binary file not shown.
Binary file modified src/main/java/duke/MainWindow.class
Binary file not shown.
Binary file modified src/main/java/duke/command/AddCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/FindCommand.class
Binary file not shown.
Binary file added src/main/java/duke/command/SortCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/task/Task.class
Binary file not shown.
Binary file modified src/main/java/duke/task/TaskList.class
Binary file not shown.
Binary file modified src/main/java/duke/util/Parser.class
Binary file not shown.
Binary file modified src/main/java/duke/util/Ui.class
Binary file not shown.
42 changes: 42 additions & 0 deletions src/main/java/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,37 @@ public Task(String type, String description, Date date) {
this.isDone = false;
}

/**
* Returns String containing type of task.
*
* @return String of type.
*/
public String getType() {
return this.type;
}

/**
* Returns String containing description of task.
*
* @return String containing description.
*/
public String getDescription() {
return this.description;
}

/**
* Returns Date containing date of task.
*
* @return Date containing date.
*/
public Date getDate() {
return this.date;
}

/**
* Returns String containing status of task.
*
* @return String containing status.
*/
public String getStatusIcon() {
return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols
Expand All @@ -47,11 +76,24 @@ public void markAsUnDone() {
this.isDone = false;
}

/**
* Returns true if Tasks have the same values in all fields, else false.
*
* @param task Task to be compared to this Task.
* @return True if Tasks are the same.
*/
public boolean equals(Task task) {
return this.type.equals(task.type)
&& this.description.equals(task.description)
&& this.date.equals(task.date);
}

/**
* Returns String containing information about the task.
*
* @return String containing status, description and date of task.
*/
@Override
public String toString() {
return String.format("[%s][%s] %s %s", this.type,
this.getStatusIcon(),
Expand Down
51 changes: 50 additions & 1 deletion src/main/java/task/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package duke.task;

import duke.exception.DukeException;
import duke.task.Task;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;

/**
* TaskList class contains Tasks to be done.
Expand All @@ -14,7 +16,10 @@ public class TaskList extends ArrayList<Task> implements Serializable {
* @param task Task to be added to TaskList.
* @return Boolean if Task is successfully added.
*/
public boolean addTask(Task task) {
public boolean addTask(Task task) throws DukeException {
if (this.hasDuplicates(task)) {
throw new DukeException("☹ OOPS!!! Task already exists in the list.");
}
return this.add(task);
}

Expand All @@ -34,6 +39,7 @@ public Task get(int itemId) {
* @param itemId Id of the Task to be removed.
* @return Task which has been removed from TaskList.
*/
@Override
public Task remove(int itemId) {
return super.remove(itemId - 1);
}
Expand All @@ -47,6 +53,49 @@ public void markAsDone(int itemId) {
super.get(itemId - 1).markAsDone();
}

/**
* Sorts Tasks in TaskList by field specified.
*
* @param field Field to sort Tasks by.
*/
public void sort(String field) throws DukeException {
switch (field) {
case "date":
Collections.sort(this,
(task1, task2) -> task1.getDate().compareTo(task2.getDate()));
break;
case "description":
Collections.sort(this,
(task1, task2) -> task1.getDescription().compareTo(task2.getDescription()));
break;
case "type":
Collections.sort(this,
(task1, task2) -> task1.getType().compareTo(task2.getType()));
break;
case "done":
Collections.sort(this,
(task1, task2) -> task1.getStatusIcon().compareTo(task2.getStatusIcon()));
break;
default:
throw new DukeException("☹ OOPS!!! Field not found to sort.");
}
}

/**
* Returns true if there are duplicate Tasks in TaskList, else false.
*
* @param task1 Task to be checked against.
* @return True if there are duplicate Tasks, else false.
*/
public boolean hasDuplicates(Task task1) {
for (Task task2 : this) {
if (task1.equals(task2)) {
return true;
}
}
return false;
}

/**
* Returns String of Tasks contained in TaskList.
*
Expand Down
Loading

0 comments on commit 0ca5087

Please sign in to comment.