Skip to content

Commit

Permalink
Completed Level-10
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiHawkEye committed Sep 6, 2019
1 parent 7b593ef commit a14e041
Show file tree
Hide file tree
Showing 42 changed files with 276 additions and 94 deletions.
51 changes: 51 additions & 0 deletions src/main/java/DialogBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package duke;

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

public class DialogBox extends HBox {

private Label text;
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);
}

/**
* 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);
}

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

public static DialogBox getDukeDialog(Label l, ImageView iv) {
var db = new DialogBox(l, iv);
db.flip();
return db;
}
}
120 changes: 110 additions & 10 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,123 @@
package duke;

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


/**
* Application class for Duke.
*/
public class Duke {
public class Duke extends Application {
private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;
private Image user = new Image(this.getClass().getResourceAsStream("/resources/DaUser.png"));
private Image duke = new Image(this.getClass().getResourceAsStream("/resources/DaDuke.png"));

private Main main;

/**
* Main method for Duke.
*
* @param args Arguments entered when main method is executed.
*/
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);

Main main = new Main("../../../data/duke.txt");
main.run();
Main duke = new Main("data/duke.txt");
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);

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);

main = new Main("data/duke.txt");

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

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

/**
* 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();
}

private String getResponse(String input) {
return main.step(input);
}
}
12 changes: 12 additions & 0 deletions src/main/java/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(Duke.class, args);
}
}
50 changes: 29 additions & 21 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
package duke;

import duke.DukeException;
import duke.Parser;
import duke.Ui;
import duke.Storage;
import duke.TaskList;
import duke.exception.DukeException;
import duke.util.Parser;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;
import duke.command.Command;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
* Main class of Duke.
* Contains run() method.
*/
public class Main {
private Storage storage;
private TaskList tasks;
private Ui ui;

/**
* Constructor of Main class.
* Constructor of Duke class.
*
* @param filePath Path to text file where save data is stored.
*/
public Main(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
this.ui = new Ui();
this.storage = new Storage(filePath);
try {
tasks = storage.load();
this.tasks = this.storage.load();
} catch (DukeException e) {
ui.showLoadingError();
tasks = new TaskList();
this.ui.showLoadingError();
this.tasks = new TaskList();
}
}

/**
* Run method of Duke.
*/
public void run() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
this.ui.showWelcome();

boolean isExit = false;
Expand All @@ -50,10 +46,22 @@ public void run() {
String fullCommand = ui.readCommand();
Command c = Parser.parse(fullCommand);
c.execute(tasks, ui, storage);
storage.save(tasks);
isExit = c.isExit();
} catch (DukeException e) {
ui.showError(e.getMessage());
}
}
}

public String step(String input) {
try {
Command c = Parser.parse(input);
c.execute(tasks, ui, storage);
this.storage.save(tasks);
} catch(DukeException e) {
ui.showError(e.getMessage());
}
return this.ui.getOutput();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package duke.command;

import duke.DukeException;
import duke.Parser;
import duke.Ui;
import duke.Storage;
import duke.TaskList;
import duke.exception.DukeException;
import duke.util.Parser;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;
import duke.task.Task;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package duke.command;

import duke.TaskList;
import duke.Ui;
import duke.Storage;
import duke.DukeException;
import duke.task.TaskList;
import duke.util.Ui;
import duke.util.Storage;
import duke.exception.DukeException;

/**
* Abstract Command class that contains execute() for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package duke.command;

import duke.DukeException;
import duke.Parser;
import duke.Ui;
import duke.Storage;
import duke.TaskList;
import duke.exception.DukeException;
import duke.util.Parser;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;
import duke.task.Task;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package duke.command;

import duke.DukeException;
import duke.Parser;
import duke.Ui;
import duke.Storage;
import duke.TaskList;
import duke.exception.DukeException;
import duke.util.Parser;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;
import duke.task.Task;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package duke.command;

import duke.Ui;
import duke.Storage;
import duke.TaskList;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;

/**
* Command containing method for exiting program.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package duke.command;

import duke.DukeException;
import duke.Parser;
import duke.Ui;
import duke.Storage;
import duke.TaskList;
import duke.exception.DukeException;
import duke.util.Parser;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;
import duke.task.Task;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package duke.command;

import duke.Ui;
import duke.Storage;
import duke.TaskList;
import duke.util.Ui;
import duke.util.Storage;
import duke.task.TaskList;

/**
* Command containing method for listing Tasks in TaskList.
Expand Down
Binary file added src/main/java/data/duke.txt
Binary file not shown.
Binary file added src/main/java/duke/DialogBox.class
Binary file not shown.
Binary file modified src/main/java/duke/Duke.class
Binary file not shown.
Binary file removed src/main/java/duke/DukeException.class
Binary file not shown.
Binary file added src/main/java/duke/Launcher.class
Binary file not shown.
Binary file modified src/main/java/duke/Main.class
Binary file not shown.
Binary file removed src/main/java/duke/Parser.class
Binary file not shown.
Binary file removed src/main/java/duke/Ui.class
Binary file not shown.
Binary file modified src/main/java/duke/command/AddCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/Command.class
Binary file not shown.
Binary file modified src/main/java/duke/command/DeleteCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/DoneCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/ExitCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/FindCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/ListCommand.class
Binary file not shown.
Binary file added src/main/java/duke/exception/DukeException.class
Binary file not shown.
Binary file not shown.
Binary file added src/main/java/duke/util/Parser.class
Binary file not shown.
Binary file not shown.
Binary file added src/main/java/duke/util/Ui.class
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package duke;
package duke.exception;

/**
* Exception class for Duke.
Expand Down
Binary file added src/main/java/resources/DaDuke.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/java/resources/DaUser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a14e041

Please sign in to comment.