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 8, 2019
1 parent 7b593ef commit fd059cf
Show file tree
Hide file tree
Showing 48 changed files with 389 additions and 114 deletions.
61 changes: 61 additions & 0 deletions src/main/java/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;
}
}
69 changes: 67 additions & 2 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,88 @@
package duke;

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

/**
* Application class for Duke.
*/
public class Duke {
private Storage storage;
private TaskList tasks;
private Ui ui;

public Duke() {
this("data/duke.txt");
}

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

public String getResponse(String input) {
return this.step(input);
}

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

Main main = new Main("../../../data/duke.txt");
main.run();
public String step(String input) {
try {
Command c = Parser.parse(input);
c.execute(tasks, ui, storage);
this.storage.save(tasks);
} catch(DukeException e) {
this.ui.showError(e.getMessage());
}
return this.ui.getOutput();
}
}
67 changes: 67 additions & 0 deletions src/main/java/DukeLogic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package duke;

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

public class DukeLogic {
private Storage storage;
private TaskList tasks;
private Ui ui;

/**
* Constructor of Duke class.
*
* @param filePath Path to text file where save data is stored.
*/
public DukeLogic(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();
}
}

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

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();
}
}
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(Main.class, args);
}
}
71 changes: 22 additions & 49 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,32 @@
package duke;

import duke.DukeException;
import duke.Parser;
import duke.Ui;
import duke.Storage;
import duke.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;
import java.io.IOException;

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

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

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

/**
* Run method of Duke.
*/
public void run() {
this.ui.showWelcome();
private Duke duke = new Duke();

boolean isExit = false;
while (!isExit) {
try {
String fullCommand = ui.readCommand();
Command c = Parser.parse(fullCommand);
c.execute(tasks, ui, storage);
isExit = c.isExit();
} catch (DukeException e) {
ui.showError(e.getMessage());
}
@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);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/MainWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package duke;

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("/resources/DaUser.png"));
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/resources/DaDuke.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();
String response = duke.getResponse(input);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage)
);
userInput.clear();
}
}
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
Loading

0 comments on commit fd059cf

Please sign in to comment.