Skip to content

Commit

Permalink
Add GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ijavierja committed Sep 10, 2020
1 parent da66a0e commit 52b52ab
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 65 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ repositories {
}

dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.4.0'

String javaFxVersion = '11'

Expand Down
2 changes: 0 additions & 2 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
[T][✓] borrow book
[T][✗] borrow book
28 changes: 23 additions & 5 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ public Duke(String filePath) {
}
}

public Duke(){
super();
public Duke() {
ui = new Ui();
storage = new Storage(TASKS_PATHNAME);
try {
taskList = new TaskList(storage.load());
} catch (DukeException e) {
ui.showLoadingError();
taskList = new TaskList();
}
}

/**
Expand All @@ -60,14 +67,16 @@ public static void main(String[] args) {
}

public void run() {
ui.showWelcome();
ui.showLine();
System.out.println(ui.welcome());
ui.showLine();
boolean isExit = false;
while (!isExit) {
try {
String fullCommand = ui.readCommand();
ui.showLine(); // show the divider line ("_______")
Command c = Parser.parse(fullCommand);
c.execute(taskList, ui, storage);
ui.showOutput(c.execute(taskList, ui, storage));
isExit = c.isExit();
} catch (DukeException e) {
ui.showError(e.getMessage());
Expand All @@ -77,6 +86,10 @@ public void run() {
}
}

public String welcome(){
return ui.welcome();
}

/* @Override
public void start(Stage stage) {
//Step 1. Setting up required components
Expand Down Expand Up @@ -170,6 +183,11 @@ public void start(Stage stage) {
*/

public String getResponse(String input) {
return "Duke heard: " + input;
try{
Command c = Parser.parse(input);
return c.execute(taskList, ui, storage);
} catch (DukeException e) {
return e.getMessage();
}
}
}
1 change: 1 addition & 0 deletions src/main/java/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* A launcher class to workaround classpath issues.
*/
public class Launcher {

public static void main(String[] args) {
Application.launch(Main.class, args);
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public void initialize() {

public void setDuke(Duke d) {
duke = d;
showWelcome();
}

@FXML
private void showWelcome() {
String response = duke.welcome();
dialogContainer.getChildren().add(
DialogBox.getDukeDialog(response, dukeImage)
);
}

/**
Expand All @@ -48,4 +57,12 @@ private void handleUserInput() {
);
userInput.clear();
}
@FXML
private void greeting() {
String response = duke.getResponse("Greetings, I'm Duke.");
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(response, dukeImage)
);
userInput.clear();
}
}
36 changes: 2 additions & 34 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Command parse(String fullCommand) throws DukeException {
} else if (getWord(fullCommand).equals("find")) {
return find(fullCommand);
} else{
throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-(");
throw new DukeException("\uD83D\uDE2D OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}

Expand Down Expand Up @@ -71,12 +71,10 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
Task task;
String string = "Here are the tasks in your list:";
StringBuilder stringBuilder = new StringBuilder().append(string).append("\n");
ui.showOutput(string);
for (int i = 1; i <= taskList.size(); i++) {
task = taskList.get(i - 1);
string = i + "." + task.getTypeString() + task.getDoneString() + task.getString();
stringBuilder.append(string).append("\n");
ui.showOutput(string);
}
stringBuilder.deleteCharAt(stringBuilder.length()-1);
return stringBuilder.toString();
Expand All @@ -94,7 +92,6 @@ public static Command bye() {
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) {
String string = "Bye. Hope to see you again soon!";
ui.showOutput(string);
return string;
}

Expand All @@ -113,25 +110,21 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
String input = string.substring(4);
String temp;
if (input.isEmpty()) {
temp = " OOPS!!! The description of a todo cannot be empty.";
temp = "\uD83D\uDE00 OOPS!!! The description of a todo cannot be empty.";
outputStringBuilder.append(temp);
ui.showOutput(temp);
return outputStringBuilder.toString();
}

duke.Task task = new duke.Task(duke.TaskType.TODO, false, input);
taskList.add(task);

temp = "Got it. I've added this task:";
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = " " + task.getTypeString() + task.getDoneString() + input;
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp ="Now you have " + taskList.size() + " tasks in the list.";
ui.showOutput(temp);
outputStringBuilder.append(temp);

return outputStringBuilder.toString();
Expand All @@ -155,7 +148,6 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
//No description
if (input.isEmpty()) {
temp = "☹ OOPS!!! The description of a deadline cannot be empty.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}
Expand All @@ -164,7 +156,6 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
int index = input.indexOf("/by");
if (index == -1) {
temp = "☹ OOPS!!! The description of a deadline must have a indicated deadline.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}
Expand Down Expand Up @@ -201,21 +192,17 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
taskList.add(task);

temp = "Got it. I've added this task:";
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = " " + task.getTypeString() + task.getDoneString() + input;
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = "Now you have " + taskList.size() + " tasks in the list.";
ui.showOutput(temp);
outputStringBuilder.append(temp);

return outputStringBuilder.toString();
} catch(DateTimeParseException e) {
temp = "☹ OOPS!!! The description of a deadline must have a valid date and time. (Format: /by dd/mm/yyyy tttt e.g 2/12/2019 1800";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}
Expand All @@ -237,15 +224,13 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
String temp;
if(input.isEmpty()){
temp = "☹ OOPS!!! The description of a event cannot be empty.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}

int index = input.indexOf("/at");
if (index == -1) {
temp ="☹ OOPS!!! The description of an event must have a indicated deadline.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}
Expand All @@ -257,15 +242,12 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
taskList.add(task);

temp = "Got it. I've added this task:";
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = " " + task.getTypeString() + task.getDoneString() + input;
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = "Now you have " + taskList.size() + " tasks in the list.";
ui.showOutput(temp);
outputStringBuilder.append(temp);

return outputStringBuilder.toString();
Expand All @@ -287,7 +269,6 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
String temp;
if(input.isEmpty()){
temp = "☹ OOPS!!! Please indicate which task is done.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}
Expand All @@ -302,15 +283,13 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
}
if(!isInteger) {
temp = "☹ OOPS!!! Incorrect entry for finished task.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}

int tasknumber = Integer.parseInt(input);
if( (tasknumber == 0) ||(tasknumber >taskList.size()) ){
temp = "☹ OOPS!!! Task number is not found in the list.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}
Expand All @@ -321,11 +300,9 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {


temp = "Nice! I've marked this task as done:";
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = " " + newTask.getTypeString() + newTask.getDoneString() + newTask.toString();
ui.showOutput(temp);
outputStringBuilder.append(temp);


Expand All @@ -348,7 +325,6 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
String temp;
if(input.isEmpty()) {
temp = "☹ OOPS!!! Please indicate which task is done.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}
Expand All @@ -363,31 +339,26 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
}
if(!isInteger) {
temp = "☹ OOPS!!! Incorrect entry for finished task.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}

int tasknumber = Integer.parseInt(input);
if( (tasknumber == 0) ||(tasknumber > taskList.size()) ){
temp = "☹ OOPS!!! Task number is not found in the list.";
ui.showOutput(temp);
outputStringBuilder.append(temp);
return outputStringBuilder.toString();
}

duke.Task task = taskList.remove(tasknumber - 1);

temp = "Noted. I've removed this task:";
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = " " + task.getTypeString() + task.getDoneString() + task.toString();
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");

temp = "Now you have " + taskList.size() + " tasks in the list.";
ui.showOutput(temp);
outputStringBuilder.append(temp);

return outputStringBuilder.toString();
Expand All @@ -406,7 +377,6 @@ public static Command save() {
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) {
String temp = storage.save(taskList);
ui.showOutput(temp);
return temp;
}

Expand All @@ -424,13 +394,11 @@ public String execute(TaskList taskList, Ui ui, Storage storage) {
String phrase = fullCommand.substring(5);
StringBuilder outputStringBuilder = new StringBuilder();
String temp = "Here are the matching tasks in your list:";
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");
for (int taskListIndex = 1; taskListIndex <= taskList.size(); taskListIndex++) {
Task task = taskList.get(taskListIndex -1);
if (task.getFullString().contains(phrase)) {
temp = taskListIndex + "." + task.getFullString();
ui.showOutput(temp);
outputStringBuilder.append(temp).append("\n");
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ public String getString() {
}
public String getDoneString() {
String string;
if(isDone){
string = "[✓]";
}
else{
string = "[✗]";
}
return string;

return (isDone ?"[\u2713]" : "[\u2718]" );
}

public duke.Task done() {
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
*/

public class Ui {

public void showWelcome() {
String logo = "____________________________________________________________\n"
/*+ " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n"*/
System.out.println(welcome());
}

public String welcome() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n"
+ " Hello I'm Duke\n"
+ " What can I do for you?\n"
+ "____________________________________________________________\n";
System.out.println(logo);
+ " What can I do for you?";

return logo;
}

public String readCommand() {
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/ParserTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package duke;

import org.junit.Test;
import org.testng.annotations.Test;

import static org.junit.Assert.assertEquals;
import static org.testng.Assert.assertEquals;

import duke.*;

public class ParserTest {
@Test
Expand Down

0 comments on commit 52b52ab

Please sign in to comment.