Skip to content

Commit

Permalink
Create a JAR file for the latest Chatty version.
Browse files Browse the repository at this point in the history
JAR file should not actually be included here.

It'll be released instead as v0.2
  • Loading branch information
jianyangg committed Sep 19, 2023
1 parent b558549 commit 1c038e7
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 93 deletions.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ application {
}

shadowJar {
mainClassName = "duke.Launcher"
archiveBaseName = "duke"
archiveClassifier = null
archiveFileName = "duke.jar"
dependsOn("distZip", "distTar")
}

jar {
manifest {
attributes(
'Main-Class': 'duke.Launcher'
)
}
}

run{
standardInput = System.in
}
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ Got it. I've added this task:
Now you have 5 tasks in the list.
```

### Add priority to task: `priority <index> <priority>`

Adds a priority to the task at the specified index. Priority ranges from 0 to 5.


### Exit the program: `bye`

Exits the program.
Expand Down
1 change: 0 additions & 1 deletion src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package duke;

import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Expand Down
204 changes: 128 additions & 76 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,116 +64,168 @@ public void run() {
System.out.println();
input = sc.nextLine().trim();
}

// bye
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}

ui.sendFarewell();
byeCommand();
}

private String handleTextInput(String inputString) {
assert inputString != null : "Input string cannot be null";
assert !inputString.isEmpty() : "Input string cannot be empty";
// handle key logic here.
String command = inputString.split(" ")[0];
int taskIndex;

switch (command) {
case "bye":
return ui.sendFarewell();
return byeCommand();

case "list":
return ui.printTaskList(tasks.toString());
return listCommand();

case "mark":
// taskIndex is the second word in the input string
taskIndex = Integer.parseInt(inputString.split(" ")[1]) - 1;

tasks.markTaskAsDone(taskIndex);

return ui.markTaskAsDoneMessage(tasks.taskToString(taskIndex));
return markCommand(inputString);

case "unmark":
taskIndex = Integer.parseInt(inputString.split(" ")[1]) - 1;
tasks.unmarkTaskAsDone(taskIndex);
return ui.unmarkTaskAsDoneMessage(tasks.taskToString(taskIndex));
return unmarkCommand(inputString);

case "todo":
try {
String taskName = inputString.substring(5);
Task newTask = new ToDo(taskName);
tasks.addToTaskList(newTask);
assert !tasks.isEmpty() : "Task list should not be empty";

return ui.addTaskOutputText(newTask, tasks.size());
} catch (StringIndexOutOfBoundsException e) {
return ui.showErrorMessage("\t☹ OOPS!!! The description of a todo cannot be empty.");
}
return toDoCommand(inputString);

case "deadline":
try {
// stop before /by
String taskName = inputString.substring(9, inputString.indexOf("/by") - 1);
// get day
String deadline = inputString.substring(inputString.indexOf("/by") + 4);
try {
Task newTask = new Deadline(taskName, deadline);

tasks.addToTaskList(newTask);

return ui.addTaskOutputText(newTask, tasks.size());
} catch (java.time.format.DateTimeParseException e) {
return ui.showErrorMessage("\tInvalid date format. Please use yyyy-mm-dd.");
}
} catch (StringIndexOutOfBoundsException e) {
return ui.showErrorMessage("\t☹ OOPS!!! The description of a deadline cannot be empty.");
}
return deadlineCommand(inputString);

case "event":
return eventCommand(inputString);

case "delete":
return deleteCommand(inputString);

case "find":
return findCommand(inputString);

case "priority":
return priorityCommand(inputString);

default:
return wrongCommand();
}

}

private String wrongCommand() {
return ui.showErrorMessage(
"\t☹ OOPS!!! I'm sorry, but I don't know what that means. Try again using either mark <index>,"
+ "unmark <index>, todo <task>, deadline <task /by ..>, event <task /from .. /to ..>, or bye.");
}

private String priorityCommand(String inputString) {
int taskIndex;
// the format will be priority <index> <priority>
// priority is an integer
// index is an integer
// priority is between 1 and 5
// index is between 1 and taskList.size()
String[] inputArr = inputString.split(" ");
int priority = Integer.parseInt(inputArr[2]);
taskIndex = Integer.parseInt(inputArr[1]) - 1;
tasks.setPriority(taskIndex, priority);
return ui.printPriorityMessage(tasks.taskToString(taskIndex), priority);
}

private String findCommand(String inputString) {
String keyword = inputString.substring(5);
String outputString = "";
for (int i = 0; i < tasks.size(); i++) {
if (tasks.taskToString(i).contains(keyword)) {
outputString += tasks.taskToString(i) + "\n";
}
}
return ui.printTaskList(outputString);
}

private String deleteCommand(String inputString) {
int taskIndex;
taskIndex = Integer.parseInt(inputString.split(" ")[1]) - 1;
String msg = ui.printDeleteMessage(tasks.taskToString(taskIndex), taskIndex, tasks.size());
tasks.deleteTask(taskIndex);
return msg;
}

private String eventCommand(String inputString) {
try {
String taskName = inputString.substring(6, inputString.indexOf("/from") - 1);
String from = inputString.substring(inputString.indexOf("/from") + 6, inputString.indexOf("/to") - 1);
String to = inputString.substring(inputString.indexOf("/to") + 4);
Task newTask = new Event(taskName, from, to);

tasks.addToTaskList(newTask);

return ui.addTaskOutputText(newTask, tasks.size());
} catch (StringIndexOutOfBoundsException e) {
return ui.showErrorMessage("\t☹ OOPS!!! The description of an event cannot be empty.");
}
}

private String deadlineCommand(String inputString) {
try {
// stop before /by
String taskName = inputString.substring(9, inputString.indexOf("/by") - 1);
// get day
String deadline = inputString.substring(inputString.indexOf("/by") + 4);
try {
String taskName = inputString.substring(6, inputString.indexOf("/from") - 1);
String from = inputString.substring(inputString.indexOf("/from") + 6, inputString.indexOf("/to") - 1);
String to = inputString.substring(inputString.indexOf("/to") + 4);
Task newTask = new Event(taskName, from, to);
Task newTask = new Deadline(taskName, deadline);

tasks.addToTaskList(newTask);

return ui.addTaskOutputText(newTask, tasks.size());
} catch (StringIndexOutOfBoundsException e) {
return ui.showErrorMessage("\t☹ OOPS!!! The description of an event cannot be empty.");
}
case "delete":
taskIndex = Integer.parseInt(inputString.split(" ")[1]) - 1;
String msg = ui.printDeleteMessage(tasks.taskToString(taskIndex), taskIndex, tasks.size());
tasks.deleteTask(taskIndex);
return msg;

case "find":
String keyword = inputString.substring(5);
String outputString = "";
for (int i = 0; i < tasks.size(); i++) {
if (tasks.taskToString(i).contains(keyword)) {
outputString += tasks.taskToString(i) + "\n";
}
} catch (java.time.format.DateTimeParseException e) {
return ui.showErrorMessage("\tInvalid date format. Please use yyyy-mm-dd.");
}
return ui.printTaskList(outputString);
} catch (StringIndexOutOfBoundsException e) {
return ui.showErrorMessage("\t☹ OOPS!!! The description of a deadline cannot be empty.");
}
}

case "priority":
// the format will be priority <index> <priority>
// priority is an integer
// index is an integer
// priority is between 1 and 5
// index is between 1 and taskList.size()
String[] inputArr = inputString.split(" ");
int priority = Integer.parseInt(inputArr[2]);
taskIndex = Integer.parseInt(inputArr[1]) - 1;
tasks.setPriority(taskIndex, priority);
return ui.printPriorityMessage(tasks.taskToString(taskIndex), priority);
private String toDoCommand(String inputString) {
try {
String taskName = inputString.substring(5);
Task newTask = new ToDo(taskName);
tasks.addToTaskList(newTask);
assert !tasks.isEmpty() : "Task list should not be empty";

default:
return ui.showErrorMessage(
"\t☹ OOPS!!! I'm sorry, but I don't know what that means. Try again using either mark <index>,"
+ "unmark <index>, todo <task>, deadline <task /by ..>, event <task /from .. /to ..>, or bye.");
return ui.addTaskOutputText(newTask, tasks.size());
} catch (StringIndexOutOfBoundsException e) {
return ui.showErrorMessage("\t☹ OOPS!!! The description of a todo cannot be empty.");
}
}

private String unmarkCommand(String inputString) {
int taskIndex;
taskIndex = Integer.parseInt(inputString.split(" ")[1]) - 1;
tasks.unmarkTaskAsDone(taskIndex);
return ui.unmarkTaskAsDoneMessage(tasks.taskToString(taskIndex));
}

private String markCommand(String inputString) {
int taskIndex;
// taskIndex is the second word in the input string
taskIndex = Integer.parseInt(inputString.split(" ")[1]) - 1;

tasks.markTaskAsDone(taskIndex);

return ui.markTaskAsDoneMessage(tasks.taskToString(taskIndex));
}

private String listCommand() {
return ui.printTaskList(tasks.toString());
}

private String byeCommand() {
return ui.sendFarewell();
}

// Obtains the response from the input
Expand Down
1 change: 0 additions & 1 deletion src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public class Task {
private String taskName;
private boolean isDone;
private boolean prioritySet;
private int priority;

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public TaskList() {
* @throws DukeException If there is an error loading the file.
*/
public TaskList(ArrayList<Task> savedTaskList) throws DukeException {
// this.taskList.addAll(savedTaskList);
this.taskList = savedTaskList;
}

Expand Down Expand Up @@ -94,6 +93,10 @@ public String getTaskItemSaveString(int i) {
return taskList.get(i).saveString();
}

/**
* Returns boolean representing whether the task list is empty.
* @return Boolean True if the task list is empty, false otherwise.
*/
public Boolean isEmpty() {
return taskList.isEmpty();
}
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,20 @@ public class Ui {
+ "╚█████╔╝██║░░██║██║░░██║░░░██║░░░░░░██║░░░░░░██║░░░\n"
+ "░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░░░░╚═╝░░░░░░╚═╝░░░\n";
private final String GREETING = "Hello! I'm Chatty.\nWhat can I do for you?";
private final String FAREWELL = "Bye. Have \"fun\" in school!";
private final String FAREWELL = "Bye. Have \"fun\" in school!\nExiting in 2 seconds...";
private final String ERROR = "Error: ";

/**
* Prints the introduction message to console.
*/
public String showIntroduction() {
// System.out.println("\nWelcome to Chatty.\n" + LOGO);
return GREETING;
}

// private String sendGreeting() {
// // System.out.println(DIVIDER);
// // System.out.println(GREETING);
// // System.out.println(DIVIDER);
// return GREETING;

// }

/**
* Prints the concluding message to console.
*/
public String sendFarewell() {
// System.out.println(DIVIDER);
// System.out.println(FAREWELL);
// System.out.println(DIVIDER);
return FAREWELL;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void start(Stage stage) {
AnchorPane ap = fxmlLoader.load();
Scene scene = new Scene(ap);
stage.setScene(scene);
stage.setTitle("Chatty");
fxmlLoader.<MainWindow>getController().setDuke(duke);
stage.show();
} catch (IOException e) {
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/duke/gui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package duke.gui;

import duke.Duke;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

import java.util.Objects;

/**
* Controller for MainWindow. Provides the layout for the other controls.
*/
Expand Down Expand Up @@ -50,14 +57,21 @@ public void setDuke(Duke d) {
@FXML
private void handleUserInput() {
String input = userInput.getText();

String response = duke.getResponse(input);

// Aligned with SLAP principle
printToTerminal(input, response);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage)
);
if (Objects.equals(input, "bye")) {
// sleep
// dialogContainer.getChildren().add(DialogBox.getDukeDialog("Exiting in 2 seconds", dukeImage));
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(2000), event -> {Platform.exit();}));
timeline.play();
}
userInput.clear();
}

Expand Down
Loading

0 comments on commit 1c038e7

Please sign in to comment.