Skip to content

Commit

Permalink
Merge branch 'branch-Level-10' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene3231 committed Sep 1, 2020
2 parents 2bb7df5 + 345d570 commit 27a9e3d
Show file tree
Hide file tree
Showing 20 changed files with 315 additions and 34 deletions.
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ 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'

String javaFxVersion = '11'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-controls', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-fxml', version: javaFxVersion, classifier: 'linux'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux'
}

test {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: duke.Duke

61 changes: 61 additions & 0 deletions src/main/java/duke/DialogBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package duke;

import java.io.IOException;
import java.util.Collections;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

/**
* An example of a custom control using FXML.
* This control represents a dialog box consisting of an ImageView to represent the speaker's face and a label
* containing text from the speaker.
*/
public class DialogBox extends HBox {
@FXML
private Label dialog;
@FXML
private ImageView displayPicture;

private DialogBox(String text, Image img) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml"));
fxmlLoader.setController(this);
fxmlLoader.setRoot(this);
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}

dialog.setText(text);
displayPicture.setImage(img);
}

/**
* Flips the dialog box such that the ImageView is on the left and text on the right.
*/
private void flip() {
ObservableList<Node> tmp = FXCollections.observableArrayList(this.getChildren());
Collections.reverse(tmp);
getChildren().setAll(tmp);
setAlignment(Pos.TOP_LEFT);
}

public static DialogBox getUserDialog(String text, Image img) {
return new DialogBox(text, img);
}

public static DialogBox getDukeDialog(String text, Image img) {
var db = new DialogBox(text, img);
db.flip();
return db;
}
}
52 changes: 52 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package duke;

import duke.command.Command;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Duke {
private ByteArrayOutputStream GUIOutput;
private ByteArrayOutputStream GUIError;
private Storage storageVar;
private TaskListHandler handlerVar;
private static final PrintStream originalOutput = System.out;
private static final PrintStream originalError = System.err;

public Duke() {
storageVar = new Storage("./data");
handlerVar = new TaskListHandler(storageVar.getListFromFile());
}

public static void main(String[] args) {
initialize();
}
Expand All @@ -18,4 +37,37 @@ public static void initialize() {
Ui userInterface = new Ui(handler, storage);
userInterface.run();
}

/**
* You should have your own function to generate a response to user input.
* Replace this stub with your completed method.
*/
String getResponse(String input) {
setGUIStreams();
try {
Command c = Parser.parse(input, handlerVar);
c.execute(handlerVar, storageVar);
System.out.println();
} catch (NoSuchElementException e1) {
System.out.println(e1.getMessage());
} catch (DukeException e) {
System.out.println(e.getMessage());
DukeException.tryAgain();
}
resetGUIStreams();
return GUIOutput.toString();
}

public void setGUIStreams() {
GUIOutput = new ByteArrayOutputStream();
GUIError = new ByteArrayOutputStream();
System.setOut(new PrintStream(GUIOutput));
System.setErr(new PrintStream(GUIError));
}

public void resetGUIStreams() {
System.setOut(originalOutput);
System.setErr(originalError);
}

}
12 changes: 12 additions & 0 deletions src/main/java/duke/Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package duke;

import javafx.application.Application;

/**
* A launcher class to workaround classpath issues.
*/
public class Launcher {
public static void main(String[] args) {
Application.launch(Main.class, args);
}
}
32 changes: 32 additions & 0 deletions src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package duke;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
* A GUI for Duke using FXML.
*/
public class Main extends Application {

private final Duke duke = new Duke();

@Override
public void start(Stage stage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml"));
AnchorPane ap = fxmlLoader.load();
Scene scene = new Scene(ap);
stage.setScene(scene);
fxmlLoader.<MainWindow>getController().setDuke(duke);
fxmlLoader.<MainWindow>getController().showGreeting();
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
67 changes: 67 additions & 0 deletions src/main/java/duke/MainWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package duke;

import javafx.animation.PauseTransition;
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.stage.Stage;
import javafx.util.Duration;

/**
* Controller for MainWindow. Provides the layout for the other controls.
*/
public class MainWindow extends AnchorPane {
@FXML
private ScrollPane scrollPane;
@FXML
private VBox dialogContainer;
@FXML
private TextField userInput;
@FXML
private Button sendButton;

private Duke duke;

private Image userImage = new Image(this.getClass().getResourceAsStream("/images/User.png"));
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/Duke.png"));

@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
}

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

/**
* Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to
* the dialog container. Clears the user input after processing.
*/
@FXML
private void handleUserInput() {
String input = userInput.getText();
if (input.equals("bye")) {
Stage stage = (Stage) scrollPane.getScene().getWindow();
PauseTransition delay = new PauseTransition(Duration.seconds(2));
delay.setOnFinished( event -> stage.close() );
delay.play();
}
String response = duke.getResponse(input);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage)
);
userInput.clear();
}

public void showGreeting() {
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(Ui.getGreeting(), dukeImage)
);
}
}
4 changes: 2 additions & 2 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void run() {
checkIfSaveFileExists();
this.listFromFile = loadListFromFile();
} catch (DukeException e) {
e.printStackTrace(System.out);
System.out.println(e.getMessage());
this.listFromFile = resetSaveFile();
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ public ArrayList<Task> resetSaveFile() {
this.saveToFile(newList);
Ui.greet();
} catch (DukeException e1) {
e1.printStackTrace(System.out);
System.out.println(e1);
}
return newList;
} else {
Expand Down
56 changes: 30 additions & 26 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.NoSuchElementException;
import java.util.Scanner;

Expand All @@ -14,6 +16,7 @@ public class Ui {
protected static boolean isRunning = true;
protected final TaskListHandler handler;
protected final Storage storage;
protected static String logo;

/**
* Initializes the Ui with the task list and storage.
Expand Down Expand Up @@ -50,7 +53,7 @@ public void run() {
// Encounter end of file, terminate
isRunning = false;
} catch (DukeException e) {
e.printStackTrace(System.out);
System.out.println(e.getMessage());
DukeException.tryAgain();
}
}
Expand All @@ -60,16 +63,14 @@ public void run() {
* Draws the top border of the chatbot's response.
*/
public static void drawTopBorder() {
System.out.println(" ______________________________________________________________ ");
System.out.println("* *");
System.out.println("___________________________________");
}

/**
* Draws the bottom border of the chatbot's response.
*/
public static void drawBottomBorder() {
System.out.println();
System.out.println("*______________________________________________________________*");
System.out.println("___________________________________");

}

Expand Down Expand Up @@ -118,28 +119,31 @@ public static void printSuccess(String operation, Task currentTask, int listSize
*/
public static void greet() {
drawTopBorder();
String logo = "\n"
+ " ___ ___ \n"
+ " ( ) ( ) \n"
+ " .-.| | ___ ___ | | ___ .--. \n"
+ " / \\ | ( )( ) | | ( ) / \\ \n"
+ "| .-. | | | | | | | ' / | .-. ; \n"
+ "| | | | | | | | | |,' / | | | | \n"
+ "| | | | | | | | | . '. | |/ | \n"
+ "| | | | | | | | | | `. \\ | ' _.' \n"
+ "| ' | | | | ; ' | | \\ \\ | .'.-. \n"
+ "' `-' / ' `-' / | | \\ . ' `-' / \n"
+ " `.__,' '.__.' (___ ) (___) `.__.' \n"
+ " \n"
+ " \n";
System.out.println(logo);
String greeting = "Hey! I'm Duke the chatbot!";
String doForYou = "What can I do for you?";
indent(1);
System.out.println(greeting);
indent(1);
System.out.println(doForYou);
System.out.println(getGreeting());
drawBottomBorder();
}

public static String getGreeting() {
setLogo();
String greeting = " Hey! I'm Duke the chatbot!";
String doForYou = " What can I do for you?";
return logo + "\n" + greeting + "\n" + doForYou;
}

public static void setLogo() {
logo = "\n" +
" ___ ___ \n" +
" ( ) ( ) \n" +
" .-.| | ___ ___ | | ___ .--. \n" +
" / \\ | ( )( ) | | ( ) / \\ \n" +
"| .-. | | | | | | | ' / | .-. ; \n" +
"| | | | | | | | | |,' / | | | | \n" +
"| | | | | | | | | . '. | |/ | \n" +
"| | | | | | | | | | `. \\ | ' _.' \n" +
"| ' | | | | ; ' | | \\ \\ | .'.-. \n" +
"' `-' / ' `-' / | | \\ . ' `-' / \n" +
" `.__,' '.__.' (___ ) (___) `.__.' \n" +
" \n" +
" \n";
}
}
2 changes: 1 addition & 1 deletion src/main/java/duke/command/AddAbstractTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void execute(TaskListHandler handler, Storage storage) {
Ui.printSuccess("add", newTask, taskList.size());
storage.saveToFile(taskList);
} catch (DukeException e) {
e.printStackTrace(System.out);
System.out.println(e.getMessage());
DukeException.tryAgain();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/command/ByeCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke.command;

import duke.Duke;
import duke.Storage;
import duke.TaskListHandler;
import duke.Ui;
Expand Down
Loading

0 comments on commit 27a9e3d

Please sign in to comment.