Skip to content

Commit

Permalink
Merge pull request #3 from Francis-Tan/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
Improve code quality
  • Loading branch information
Francis-Tan authored Sep 19, 2022
2 parents b7c61d0 + 8c14007 commit 7bf59ec
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 135 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ checkstyle {

group 'org.example'
version '0.1'
mainClassName = 'duke.services.Duke'
mainClassName = 'duke.gui.Launcher'

// Output to build/libs/Duke.jar
shadowJar {
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/duke/gui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duke.gui;

import duke.services.Duke;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
Expand All @@ -10,6 +11,8 @@
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
* Controller for MainWindow. Provides the layout for the other controls.
Expand All @@ -32,8 +35,8 @@ public void initialize() throws IOException {
//Scroll down to the end every time dialogContainer's height changes
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());

Duke.start();
dialogContainer.getChildren().add(DialogBox.getDukeDialog(Duke.response, dukeImage));
Duke.activate();
dialogContainer.getChildren().add(DialogBox.getDukeDialog(Duke.getReply(), dukeImage));
}

/**
Expand All @@ -49,5 +52,16 @@ private void handleUserInput() {
DialogBox.getDukeDialog(response, dukeImage)
);
userInput.clear();

if (!Duke.isActive()) {
userInput.setDisable(true);
sendButton.setDisable(true);
new Timer().schedule(new TimerTask() {
public void run() {
Platform.exit();
System.exit(0);
}
}, 2000);
}
}
}
84 changes: 62 additions & 22 deletions src/main/java/duke/services/Duke.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
package duke.services;

import javafx.application.Platform;

import java.io.IOException;
import java.util.Arrays;

/**
* Personal Assistant that helps you keep track of your tasks
* Provides Duke's high-level functionality
*/
public class Duke {

public static String response;
/** What Duke will tell the user after a command */
private static String reply;

/** Is Duke interacting with the user? */
private static boolean isActive;

/**
* You should have your own function to generate a response to user input.
* Replace this stub with your completed method.
* Duke performs its activation behaviour. Save data is loaded.
*
* @throws IOException From IO errors when loading save data
*/
public static String getResponse(String input) {
response = "";
String[] words = Arrays.stream(input.strip().split(" ")).toArray(String[]::new);
public static void activate() throws IOException {
isActive = true;
Storage.loadData();
setReply(new String[] {
"Hello! I'm Duke",
"What can I do for you?",
});
}

/**
* Responds to the command and gives a reply
*
* @param command User's inputted command
* @return Duke's reply
*/
public static String getResponse(String command) {
String[] words = Parser.convertToWords(command);
if (words.length > 0) {
try {
//could try grouping words.length == 1 cases
if (words.length == 1 && words[0].equals("bye")) {
stop();
Duke.deactivate();
} else if (words.length == 1 && words[0].equals("list")) {
TaskList.listTasks(); //could put words.length == 1 cases all here
TaskList.listTasks();
} else if (words.length == 1 && words[0].equals("SAVE")) {
Storage.wipeDataOnExit(false);
} else if (words.length == 1 && words[0].equals("WIPE")) {
Expand All @@ -44,24 +61,47 @@ public static String getResponse(String input) {
} else if (words[0].equals("find")) {
TaskList.findTasksContainingKeyword(words);
} else {
Ui.sayLines(new String[]{"I'm sorry, I don't know what that means"});
setReply(new String[]{"I'm sorry, I don't know that command"});
}
} catch (IllegalArgumentException | IOException e) {
Ui.sayLines(new String[]{e.getMessage()});
setReply(new String[]{e.getMessage()});
}
}
return response;
return getReply();
}

public static void start() throws IOException {
Storage.loadData();
Ui.introduceSelf();
/**
* Updates duke's reply to the given lines
*/
public static void setReply(String[] lines) {
StringBuilder replyBuilder = new StringBuilder();
System.out.println("____________________________________________________________");
for (String line : lines) {
System.out.println(line);
replyBuilder.append(line).append("\n");
}
System.out.println("____________________________________________________________\n");
reply = replyBuilder.toString();
}

public static String getReply() {
return reply;
}

public static void stop() throws IOException {
/**
* Duke performs its deactivation behaviour. Data is saved.
*
* @throws IOException From IO errors when saving data
*/
public static void deactivate() throws IOException {
Storage.saveData();
Ui.sayGoodbye();
Platform.exit();
System.exit(0);
setReply(new String[] {
"Bye. Hope to see you again soon!",
});
isActive = false;
}

public static boolean isActive() {
return isActive;
}
}
46 changes: 7 additions & 39 deletions src/main/java/duke/services/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,21 @@
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;

/** Handles reading of user commands */
/** Handles reading of user commands and string manipulation */
public class Parser {

/** Points to the current word being read in the current command */
private static int currWordIndex = 0;

/**
* Receives user's inputs and responds to them until "bye" is entered
* Converts the command to an array of words split by " ".
*
* @param command User's inputted command
* @return An array of the command's words, split by " ".
*/
public static void handleUserInputs() {
Scanner inputScanner = new Scanner(System.in);
String[] words = Arrays.stream(inputScanner.nextLine().strip().split(" ")).toArray(String[]::new);
while (!(words.length == 1 && words[0].equals("bye"))) {
if (words.length > 0) {
try {
if (words.length == 1 && words[0].equals("list")) {
TaskList.listTasks(); //could put words.length == 1 cases all here
} else if (words.length == 1 && words[0].equals("SAVE")) {
Storage.wipeDataOnExit(false);
} else if (words.length == 1 && words[0].equals("WIPE")) {
Storage.wipeDataOnExit(true);
} else if (words[0].equals("todo")) {
TaskList.addTodo(words);
} else if (words[0].equals("deadline")) {
TaskList.addDeadline(words);
} else if (words[0].equals("event")) {
TaskList.addEvent(words);
} else if (words[0].equals("mark")) {
TaskList.markTaskAsDone(words);
} else if (words[0].equals("unmark")) {
TaskList.markTaskAsNotDone(words);
} else if (words[0].equals("delete")) {
TaskList.deleteTask(words);
} else if (words[0].equals("find")) {
TaskList.findTasksContainingKeyword(words);
} else {
Ui.sayLines(new String[]{"I'm sorry, I don't know what that means"});
}
} catch (IllegalArgumentException e) {
Ui.sayLines(new String[]{e.getMessage()});
}
}
words = Arrays.stream(inputScanner.nextLine().strip().split(" ")).toArray(String[]::new);
}
inputScanner.close();
public static String[] convertToWords(String command) {
return Arrays.stream(command.strip().split(" ")).toArray(String[]::new);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/duke/services/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Storage {
/**
* Loads saved data on stored tasks from Duke_Tasks.txt if it exists, otherwise creates it
*
* @throws IOException If an I/O error occurs or the parent directory doesn't exist
* @throws IOException If an IO error occurs or the parent directory doesn't exist
*/
public static void loadData() throws IOException {
Path path = Paths.get("Duke_Tasks");
Expand All @@ -40,7 +40,7 @@ public static void loadData() throws IOException {
String[] words;
Task task;
while (line != null) {
//[typeSymbol][1 or 0] [desc] [flag] [timing]
//format of savedata lines: [typeSymbol][1 or 0] [desc] [flag] [timing]
words = Arrays.stream(line.split(" ")).toArray(String[]::new);
if (line.charAt(0) == 'T') {
task = new Todo(Parser.getDescription(words, null));
Expand Down Expand Up @@ -69,7 +69,7 @@ public static void loadData() throws IOException {
*/
public static void wipeDataOnExit(boolean willWipe) {
willWipeData = willWipe;
Ui.sayLines(new String[] {
Duke.setReply(new String[] {
"Data will be " + (willWipe ? "wiped" : "saved") + " on exit"}
);
}
Expand All @@ -85,7 +85,7 @@ public static void saveData() throws IOException {
BufferedWriter bf = new BufferedWriter(new FileWriter(dataSaved));
StringBuilder lineBuilder = new StringBuilder();
for (Task task : TaskList.getTasks()) {
//[typeSymbol][1 or 0] [desc] [flag] [timing]
//format of savedata lines: [typeSymbol][1 or 0] [desc] [flag] [timing]
lineBuilder.append(task.getTypeSymbol())
.append(task.getStatusIcon().equals("X") ? '1' : '0')
.append(" ")
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/duke/services/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ public class TaskList {
/** The tasks stored */
private static List<Task> tasks = new ArrayList<>();

/**
* Gets the tasks stored
*/
public static List<Task> getTasks() {
return tasks;
}

/**
* @param i The index of the task in the tasklist
* @return A string describing the task w.r.t to the tasklist
*/
private static String getTaskInfo(int i) {
return " " + (i + 1) + ". " + tasks.get(i);
}
/**
* Stores the task and displays outcome
*/
public static void addTask(Task task) {
tasks.add(task);
Ui.sayLines(new String[] {
Duke.setReply(new String[] {
"Got it. I've added this task:",
" " + task,
"Now you have " + tasks.size() + " task" + (tasks.size() == 1 ? "" : "s") + " in the list."
Expand Down Expand Up @@ -66,7 +70,7 @@ public static void addEvent(String[] words) {
*/
public static void markTaskAsDone(String[] words) {
Task task = tasks.get(Parser.getTaskNumber(words) - 1).markAsDone();
Ui.sayLines(new String[]{
Duke.setReply(new String[]{
"Nice! I've marked this task as done:",
" " + task
});
Expand All @@ -79,7 +83,7 @@ public static void markTaskAsDone(String[] words) {
*/
public static void markTaskAsNotDone(String[] words) {
Task task = tasks.get(Parser.getTaskNumber(words) - 1).markAsNotDone();
Ui.sayLines(new String[]{
Duke.setReply(new String[]{
"OK, I've marked this task as not done yet:",
" " + task
});
Expand All @@ -92,7 +96,7 @@ public static void markTaskAsNotDone(String[] words) {
*/
public static void deleteTask(String[] words) {
Task removedTask = tasks.remove(Parser.getTaskNumber(words) - 1);
Ui.sayLines(new String[]{
Duke.setReply(new String[]{
"Noted. I've removed this task:",
" " + removedTask,
"Now you have " + tasks.size() + " task" + (tasks.size() == 1 ? "" : "s") + " in the list."
Expand All @@ -106,9 +110,9 @@ public static void listTasks() {
String[] taskDescriptions = new String[getTasks().size() + 1];
taskDescriptions[0] = "Here are the tasks in your list:";
for (int i = 1; i < taskDescriptions.length; ++i) {
taskDescriptions[i] = " " + i + "." + tasks.get(i - 1);
taskDescriptions[i] = " " + i + ". " + tasks.get(i - 1);
}
Ui.sayLines(taskDescriptions);
Duke.setReply(taskDescriptions);
}

/**
Expand All @@ -122,14 +126,14 @@ public static void findTasksContainingKeyword(String[] words) {
}
String keyword = words.length == 1 ? "" : keywordBuilder.deleteCharAt(keywordBuilder.length() - 1).toString();

ArrayList<String> matchingTasks = new ArrayList<String>();
ArrayList<String> matchingTasks = new ArrayList<>();
matchingTasks.add("Here are the tasks containing the keyword \"" + keyword + "\" :");
for (int i = 0; i < tasks.size(); ++i) {
Task currTask = tasks.get(i);
if (currTask.getDescription().contains(keyword)) {
matchingTasks.add(" " + (i + 1) + "." + currTask);
matchingTasks.add(" " + (i + 1) + ". " + currTask);
}
}
Ui.sayLines(matchingTasks.toArray(String[]::new));
Duke.setReply(matchingTasks.toArray(String[]::new));
}
}
37 changes: 0 additions & 37 deletions src/main/java/duke/services/Ui.java

This file was deleted.

Loading

0 comments on commit 7bf59ec

Please sign in to comment.