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 4301640
Show file tree
Hide file tree
Showing 41 changed files with 277 additions and 125 deletions.
24 changes: 24 additions & 0 deletions src/main/java/DialogBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package duke;

import javafx.geometry.Pos;
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);
}
}
166 changes: 163 additions & 3 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,183 @@
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;

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;


/**
* 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 Storage storage;
private TaskList tasks;
private Ui ui;
private static Duke main;

/**
* Constructor of Duke class.
*
* @param filePath Path to text file where save data is stored.
*/
public Duke(String filePath) {
this.ui = new Ui();
this.storage = new Storage(filePath);
try {
this.tasks = this.storage.load();
} catch (DukeException e) {
this.ui.showLoadingError();
this.tasks = new TaskList();
}
}
/**
* Main method for Duke.
*
* @param args Arguments entered when main method is executed.
*/
public static void main(String[] args) {
Duke duke = new Duke("data/duke.txt");
duke.run();
}

/**
* 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;
while (!isExit) {
try {
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());
}
}
}

@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 Duke("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(
new DialogBox(userText, new ImageView(user)),
new DialogBox(dukeText, new ImageView(duke))
);
userInput.clear();
}

Main main = new Main("../../../data/duke.txt");
main.run();
private String getResponse(String input) {
Command c = Parser.parse(input);
c.execute(tasks, ui, storage);
main.storage.save(tasks);
isExit = c.isExit();
return main.ui.getOutput();
}
}
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);
}
}
59 changes: 0 additions & 59 deletions src/main/java/Main.java

This file was deleted.

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 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 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 4301640

Please sign in to comment.