Skip to content

Commit

Permalink
Merge branch 'branch-Level-10'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kangxun committed Sep 4, 2021
2 parents 0897ad0 + a3f6b3b commit 6b8b494
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 84 deletions.
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ 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 All @@ -29,7 +44,7 @@ test {
}

application {
mainClassName = "duke.Duke"
mainClassName = "duke.gui.Launcher"
}

shadowJar {
Expand Down
64 changes: 30 additions & 34 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

// import java packages
import java.util.ArrayList;
import java.util.Scanner;

// import duke packages
import duke.command.Deadline;
Expand All @@ -28,7 +27,7 @@ public class Duke {
* @param filePath Filepath to save tasks and load tasks.
*/
public Duke(String filePath) {
ui = new Ui();
ui = new Ui("", "");
storage = new Storage(filePath);
try {
commandList = new TaskList(storage.loadTasks());
Expand All @@ -39,21 +38,29 @@ public Duke(String filePath) {
}

/**
* Runs the Duke chatbot.
* Converts Duke to string format.
*
* @return Duke as a string.
*/
public void run() {
ui.sayHello();
int count = 0 + commandList.getLength();
@Override
public String toString() {
return "im a chatbot ☺ - calico";
}

// Create a scanner to read from standard input.
Scanner sc = new Scanner(System.in);
String cmd = sc.nextLine();
/**
* Generates a response to user input.
*
* @param cmd Command from user
* @return Chatbot response
*/
public String getResponse(String cmd) {
int count = 0 + commandList.getLength();

while (Parser.isNotBye(cmd)) {
if (Parser.isNotBye(cmd)) {
try {
// when command given is "list"
if (Parser.isList(cmd)) {
ui.printList(commandList);
return ui.showList(commandList);
} else if (Parser.isDone(cmd)) {
// when command given is "done"

Expand All @@ -67,7 +74,7 @@ public void run() {
// throw exception when itemNo not within limit
throw new DukeException("i cant seem to find the task you're looking for");
}
ui.printTaskDone(commandList, itemNo - 1);
return ui.showTaskDone(commandList, itemNo - 1);
} else if (Parser.isValidTask(cmd)) {
String task = Parser.getTaskName(cmd);
String desc = Parser.getDesc(cmd);
Expand Down Expand Up @@ -96,19 +103,20 @@ public void run() {
throw new DukeException("the name of task is not valid");
}
storage.saveTasks(commandList.getTasks());
ui.printTaskAdded(commandList, count);
count++;
return ui.showTaskAdded(commandList, count - 1);
}
} else if (Parser.isDelete(cmd)) {
int itemNo = Parser.parseDeleteCmd(cmd);

// Make sure itemNo is within limit
if ((itemNo <= count) && (itemNo > 0)) {
// Remove item from lists
ui.printTaskDeleted(commandList, itemNo - 1);
String deletionMessage = ui.showTaskDeleted(commandList, itemNo - 1);
commandList.remove(itemNo - 1);
storage.saveTasks(commandList.getTasks());
count--;
return deletionMessage;
} else {
// throw exception when itemNo not within limit
throw new DukeException("i cant seem to find the task you're looking for");
Expand All @@ -122,9 +130,9 @@ public void run() {
}
ArrayList<Task> matchingTasks = commandList.find(desc);
if (matchingTasks.isEmpty()) {
ui.showNoMatch();
return ui.showNoMatch();
} else {
ui.showMatchingTasks(matchingTasks);
return ui.showMatchingTasks(matchingTasks);
}
} else {
// throw exception when command not found
Expand All @@ -135,33 +143,21 @@ public void run() {
throw new DukeException("im sorry, but i dont know what that means :(");
}
}
cmd = sc.nextLine();
} catch (DukeException e) {
ui.showDukeError(e);
cmd = sc.nextLine();
return ui.showDukeError(e);
}
}

// when command given is "bye"
ui.sayGoodbye();
return ui.sayGoodbye();
}

/**
* Starts the Duke chatbot.
* Say hello to user.
*
* @param args Arguments to be passed to main function.
* @return Hello message.
*/
public static void main(String[] args) {
new Duke("data/tasks.txt").run();
}

/**
* Converts Duke to string format.
*
* @return Duke as a string.
*/
@Override
public String toString() {
return "im a chatbot ☺ - calico";
public String intro() {
return ui.sayHello();
}
}
2 changes: 1 addition & 1 deletion src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public DukeException(String message) {
*/
@Override
public String toString() {
return " oops.." + getMessage();
return " oops.." + getMessage();
}
}
69 changes: 69 additions & 0 deletions src/main/java/duke/gui/DialogBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package duke.gui;

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;
import javafx.scene.shape.Rectangle;

/**
* 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();
}
double width = displayPicture.getFitWidth();
double height = displayPicture.getFitHeight();
Rectangle circleMask = new Rectangle(width, height);
circleMask.setArcHeight(height);
circleMask.setArcWidth(width);


dialog.setText(text);
displayPicture.setClip(circleMask);
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;
}
}
12 changes: 12 additions & 0 deletions src/main/java/duke/gui/Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package duke.gui;

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);
}
}
36 changes: 36 additions & 0 deletions src/main/java/duke/gui/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package duke.gui;

import java.io.IOException;

import duke.Duke;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

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

private Duke duke = new Duke("data/tasks.txt");

@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);
stage.setTitle("Calico");
scene.setFill(Color.TRANSPARENT);
fxmlLoader.<MainWindow>getController().setDuke(duke);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
66 changes: 66 additions & 0 deletions src/main/java/duke/gui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package duke.gui;

import duke.Duke;
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;



/**
* 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/DaUser.png"));
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));

/**
* Initialise Main Window. Introduces Duke.
*/
@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
}

public void setDuke(Duke d) {
duke = d;
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(duke.intro(), dukeImage)
);
}

/**
* 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();
String response = duke.getResponse(input);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage)
);
if (userInput.getText().equals("bye")) {
userInput.clear();
Platform.exit();
}
userInput.clear();
}
}
Loading

0 comments on commit 6b8b494

Please sign in to comment.