Skip to content

Commit

Permalink
Merge branch 'branch-Level-10'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dban1 committed Feb 4, 2020
2 parents ba437ae + e285aa2 commit 8e3071c
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 33 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'application'
id 'checkstyle'
id 'com.github.johnrengelman.shadow' version '5.1.0'
id 'org.openjfx.javafxplugin' version '0.0.7'
}

checkstyle {
Expand All @@ -23,6 +24,11 @@ repositories {
mavenCentral()
}

javafx {
version = "11.0.2"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.0'
}
Expand Down
4 changes: 2 additions & 2 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
T|0|hack the pentagon
D|0|Defeat 0/20 Monsters |3 July 2025
T|0|venuor
D|0|kill people |5pm
29 changes: 16 additions & 13 deletions src/main/java/duke/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,24 @@ public boolean isExit() {
return this.isExit;
}

public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeException {
public String execute(TaskList taskList, Storage storage, Ui ui) throws DukeException {
String result = "";
switch (this.commandWord) {
case "bye":
ui.showMessage("Aww okay, see you next time!");
result += ui.showMessage("Aww okay, see you next time!");
this.isExit = true;
break;
case "list":
if (taskList.getTaskList().size() < 1) {
ui.showMessage("no tasks");
result += ui.showMessage("no tasks");
break;
} else {
System.out.println("Here are the tasks in your list:");
result += ("Here are the tasks in your list:\n");
int index = 1;
String output = "";
for (Task t : taskList.getTaskList()) {
output = index + ". " + t.toString();
System.out.println(output);
result += output + "\n";
index++;
}
}
Expand All @@ -71,7 +72,7 @@ public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeExcept

int taskNum = Integer.parseInt(this.elements[1]);
taskList.getTaskList().get(taskNum - 1).setDone();
ui.showMessage(
result += ui.showMessage(
"Nice! I've marked this task as done:\n" + taskList.getTaskList().get(taskNum - 1).toString());
break;
}
Expand All @@ -90,8 +91,9 @@ public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeExcept
}

int taskNum = Integer.parseInt(elements[1]);
System.out.println(
"Boi. I've went and deleted that task \n" + taskList.getTaskList().get(taskNum - 1).toString());
result += (
"Boi. I've went and deleted that task \n" + taskList.getTaskList().get(taskNum - 1).toString())
+ "\n";
taskList.getTaskList().remove(taskNum - 1);
break;
}
Expand All @@ -102,7 +104,7 @@ public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeExcept
}
Task task = new Todo(this.fullInput.substring(5)); // stores input to storedText List
TaskList.addTask(task, taskList);
ui.showMessage("I've added the todo to the list!");
result += ui.showMessage("I've added the todo to the list!");
break;

case "deadline": {
Expand All @@ -114,7 +116,7 @@ public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeExcept
String by = deadlineElements[1].substring(1);
Task deadline = new Deadline(deadlineDescription, by);
TaskList.addTask(deadline, taskList);
ui.showMessage("I've added the deadline to the list!");
result += ui.showMessage("I've added the deadline to the list!");
break;
}

Expand All @@ -128,7 +130,7 @@ public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeExcept
String at = eventElements[1].substring(1);
Task event = new Event(eventDescription, at);
TaskList.addTask(event, taskList);
ui.showMessage("I've added the event to the list!");
result += ui.showMessage("I've added the event to the list!");
break;
}

Expand All @@ -142,12 +144,12 @@ public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeExcept
if (t.toString().contains(findElement)) {
hasAtLeastOne = true;
output = index + ". " + t.toString();
ui.showMessage(output);
result += ui.showMessage(output) + "\n";
}
index++;
}
if (!hasAtLeastOne) {
ui.showMessage("Couldn't find any related tasks! Sorry.");
result += ui.showMessage("Couldn't find any related tasks! Sorry.");
}
} catch (Exception e) {
throw new DukeException("Need more arguments boi");
Expand All @@ -158,5 +160,6 @@ public void execute(TaskList taskList, Storage storage, Ui ui) throws DukeExcept
default:
throw new DukeException("unknown command!!!");
}
return result;
}
}
56 changes: 56 additions & 0 deletions src/main/java/duke/main/DialogBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package duke.main;

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 java.io.IOException;
import java.util.Collections;

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;
}
}
133 changes: 123 additions & 10 deletions src/main/java/duke/main/Duke.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
package duke.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import duke.commands.Command;
import duke.exception.DukeException;
import duke.storage.Storage;
import duke.tasks.Deadline;
import duke.tasks.Event;
import duke.tasks.Task;
import duke.tasks.TaskList;
import duke.tasks.Todo;
import duke.ui.Parser;
import duke.ui.Ui;
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.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
* Represents a Duke program that stores user-inputted tasks.
*/
public class Duke {
public class Duke extends Application {
private Storage storage;
private TaskList tasks;
private Ui ui;

private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;

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

/**
* Constructs a Duke class.
* @param filePath Filepath of the storage file tasks.txt
Expand All @@ -40,6 +51,108 @@ public Duke(String filePath) {
}
}

public Duke() {
ui = new Ui();
storage = new Storage("./data/tasks.txt");
try {
tasks = storage.buildTaskList();
} catch (DukeException e) {
ui.showError(e);
tasks = new TaskList();
storage.updateStorage(tasks);
}
}

@Override
public void start(Stage stage) {
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();

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

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

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

private Label getDialogLabel(String text) {
Label textToAdd = new Label(text);
textToAdd.setWrapText(true);

return textToAdd;
}

// private void handleUserInput() {
// String userTextInput = userInput.getText();
// Label userText = new Label(userTextInput);
// Label dukeText = new Label(getResponse(userTextInput));
// dialogContainer.getChildren().addAll(
// DialogBox.getUserDialog(userTextInput, new ImageView(user)),
// DialogBox.getDukeDialog(getResponse(userTextInput), new ImageView(duke))
// );
// userInput.clear();
// }

public String getResponse(String fullCommand) {
String response = ui.divider("") + "\n";
try {
response += ui.divider("");
Command c = Parser.parseCommand(fullCommand);
response += c.execute(tasks, storage, ui);
storage.updateStorage(tasks);
} catch (DukeException e) {
response += ui.showError(e);
} finally {
response += "\n" + ui.divider("");
}
return response;
}

/**
* Executes the main function of Duke.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/duke/main/Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package duke.main;

import javafx.application.Application;

public class Launcher {
public static void main(String[] args) {
Application.launch(Main.class, args);
}
}
28 changes: 28 additions & 0 deletions src/main/java/duke/main/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package duke.main;

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

import java.io.IOException;

public class Main extends Application {
private Duke duke = new Duke();

@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) {
System.out.println("ncrwjke");
e.printStackTrace();
}
}
}
Loading

0 comments on commit 8e3071c

Please sign in to comment.