Skip to content

Commit

Permalink
Merge pull request #9 from Michaeliaaa/branch-level-10
Browse files Browse the repository at this point in the history
Branch level 10
  • Loading branch information
Michaeliaaa committed Sep 1, 2020
2 parents 35f3a02 + adbcfe8 commit 2065b37
Show file tree
Hide file tree
Showing 17 changed files with 400 additions and 292 deletions.
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Tue Sep 01 18:02:33 SGT 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
46 changes: 23 additions & 23 deletions src/main/java/duke/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ public CommandHandler(String input, DukeCommandType commandType) {
* @param tasks
* @throws DukeException
*/
public static void handleCommands(String input, DukeCommandType commandType, TaskList tasks) throws DukeException {
public static String handleCommands(String input, DukeCommandType commandType, TaskList tasks) throws DukeException {
String task;
String output = "";
switch (commandType) {
case TODO:
try {
task = input.split("todo ")[1];
Task newTask = new ToDos(task);
tasks.addTask(newTask);
output += tasks.addTask(newTask);
} catch (ArrayIndexOutOfBoundsException exception) {
try {
throw new DukeException("", DukeExceptionType.MISSING_DESCRIPTION, TODO);
} catch (DukeException e) {
System.err.println(e);
output += e;
}
}
break;
Expand Down Expand Up @@ -75,17 +76,17 @@ public static void handleCommands(String input, DukeCommandType commandType, Tas
} else {
try {
Task newTask = new Deadlines(task, due);
tasks.addTask(newTask);
output += tasks.addTask(newTask);
} catch (DateTimeParseException e) {
DukeException.wrongTimeFormat();
}
}
} catch (DukeException e) {
System.err.println(e);
output += e;
}
}
} catch (DukeException e) {
System.err.println(e);
output += e;
}
break;
case EVENT:
Expand Down Expand Up @@ -114,65 +115,64 @@ public static void handleCommands(String input, DukeCommandType commandType, Tas
throw new DukeException("", DukeExceptionType.MISSING_TIMING, DEADLINE);
} else {
Task newTask = new Events(task, due);
tasks.addTask(newTask);
output += tasks.addTask(newTask);
}
} catch (DukeException e) {
System.err.println(e);
output += e;
} catch (DateTimeParseException e) {
DukeException.wrongTimeFormat();
}
}
} catch (DukeException e) {
System.err.println(e);
output += e;
}
break;
case LIST:
tasks.getListOfTasks();
output += tasks.getListOfTasks();
break;
case FIND:
String keyword = input.split(" ")[1];
System.out.println(keyword);
tasks.findTasks(keyword);
output += tasks.findTasks(keyword);
break;
case DONE:
try {
int index = Integer.parseInt(input.split(" ")[1]);
tasks.done(index);
output += tasks.done(index);
} catch (IndexOutOfBoundsException exception) {
try {
throw new DukeException("", DukeExceptionType.INVALID_INDEX, DONE);
} catch (DukeException e) {
System.err.println(e);
output += e;
}
}
break;
case DELETE:
try {
int index = Integer.parseInt(input.split(" ")[1]);
tasks.delete(index);
output += tasks.delete(index);
} catch (IndexOutOfBoundsException exception) {
try {
throw new DukeException("", DukeExceptionType.INVALID_INDEX, DELETE);
} catch (DukeException e) {
System.err.println(e);
output += e;
}
}
break;
case HELP:
Ui.getListOfCommands();
output += Ui.getListOfCommands();
break;
case UNKNOWN:
case EXIT:
output += Ui.exit();
break;
default:
try {
throw new DukeException("", DukeExceptionType.UNKNOWN);
} catch (DukeException e) {
System.err.println(e);
output += e;
}
break;
case EXIT:
Ui.exit();
break;
default:
throw new IllegalStateException("Unexpected value: " + commandType);
}
return output;
}
}
52 changes: 33 additions & 19 deletions src/main/java/duke/DialogBox.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
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 {

private Label text;
@FXML
private Label dialog;
@FXML
private ImageView displayPicture;

public DialogBox(Label l, ImageView iv) {
text = l;
displayPicture = iv;

text.setWrapText(true);
displayPicture.setFitWidth(100.0);
displayPicture.setFitHeight(100.0);

this.setAlignment(Pos.TOP_RIGHT);
this.getChildren().addAll(text, 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() {
this.setAlignment(Pos.TOP_LEFT);
ObservableList<Node> tmp = FXCollections.observableArrayList(this.getChildren());
FXCollections.reverse(tmp);
this.getChildren().setAll(tmp);
Collections.reverse(tmp);
getChildren().setAll(tmp);
setAlignment(Pos.TOP_LEFT);
}

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

public static DialogBox getDukeDialog(Label l, ImageView iv) {
var db = new DialogBox(l, iv);
public static DialogBox getDukeDialog(String text, Image img) {
var db = new DialogBox(text, img);
db.flip();
return db;
}
Expand Down
136 changes: 12 additions & 124 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,28 @@
package duke;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.layout.Region;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
* Duke class is the main class to use in order to run the bot.
*/
public class Duke extends Application {

private Image user = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image duke = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));

private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;
public class Duke {

/** User's storage */
private Storage storage;
/** User's task list */
private TaskList tasks;
/** Ui to interact with user */
private Ui ui;
/** Parser to parse user's inputs */
private Parser parser;

/**
* Initialises Duke and objects required.
*/
@SuppressWarnings("checkstyle:WhitespaceAround")
public Duke() {
ui = new Ui();
ui = new Ui(parser);
storage = new Storage();
tasks = new TaskList();
parser = new Parser();
try {
storage.load(tasks);
} catch (Exception e) {
Expand All @@ -56,7 +37,11 @@ public Duke() {
*/
public void run() throws DukeException {
Ui.greeting();
Parser.parseInputs(tasks);
boolean isExit = ui.isExit();
while (!isExit) {
ui.readInput();
isExit = ui.isExit();
}
storage.save(TaskList.tasks);
}

Expand All @@ -70,104 +55,7 @@ public static void main(String[] args) throws DukeException {
new Duke().run();
}

@Override
public void start(Stage stage) {
//Step 1. Setting up required components

//The container for the content of the chat to scroll.
scrollPane = new ScrollPane();
dialogContainer = new VBox();
scrollPane.setContent(dialogContainer);

userInput = new TextField();
sendButton = new Button("Send");

AnchorPane mainLayout = new AnchorPane();
mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);

//Scroll down to the end every time dialogContainer's height changes.
dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));

scene = new Scene(mainLayout);

stage.setScene(scene);
stage.show();

//Step 2. Formatting the window to look as expected
stage.setTitle("Duke");
stage.setResizable(false);
stage.setMinHeight(600.0);
stage.setMinWidth(400.0);

mainLayout.setPrefSize(400.0, 600.0);

scrollPane.setPrefSize(385, 535);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

scrollPane.setVvalue(1.0);
scrollPane.setFitToWidth(true);

// You will need to import `javafx.scene.layout.Region` for this.
dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);

userInput.setPrefWidth(325.0);

sendButton.setPrefWidth(55.0);

AnchorPane.setTopAnchor(scrollPane, 1.0);

AnchorPane.setBottomAnchor(sendButton, 1.0);
AnchorPane.setRightAnchor(sendButton, 1.0);

AnchorPane.setLeftAnchor(userInput , 1.0);
AnchorPane.setBottomAnchor(userInput, 1.0);

//Part 3. Add functionality to handle user input.
sendButton.setOnMouseClicked((event) -> {
handleUserInput();
});

userInput.setOnAction((event) -> {
handleUserInput();
});

}

/**
* Iteration 1:
* Creates a label with the specified text and adds it to the dialog container.
* @param text String containing text to add
* @return a label with the specified text that has word wrap enabled.
*/
private Label getDialogLabel(String text) {
// You will need to import `javafx.scene.control.Label`.
Label textToAdd = new Label(text);
textToAdd.setWrapText(true);

return textToAdd;
}

/**
* Iteration 2:
* 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.
*/
private void handleUserInput() {
Label userText = new Label(userInput.getText());
Label dukeText = new Label(getResponse(userInput.getText()));
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText, new ImageView(user)),
DialogBox.getDukeDialog(dukeText, new ImageView(duke))
);
userInput.clear();
}

/**
* You should have your own function to generate a response to user input.
* Replace this stub with your completed method.
*/
private String getResponse(String input) {
return "Duke heard: " + input;
protected String getResponse(String input) throws DukeException {
return parser.parseInputs(input);
}
}

0 comments on commit 2065b37

Please sign in to comment.