From ca8245b7c9224030e49a5a9fba3d25dbd2991dd7 Mon Sep 17 00:00:00 2001 From: gx-huang Date: Fri, 28 Feb 2020 15:17:30 +0800 Subject: [PATCH] A-CodeQuality --- build.gradle | 2 +- src/main/java/Deadlines.java | 15 ++++- src/main/java/DialogBox.java | 9 ++- src/main/java/Duke.java | 13 +++- src/main/java/DukeParser.java | 110 ++++++++++++++++----------------- src/main/java/DukeStorage.java | 4 +- src/main/java/DukeUI.java | 11 +++- src/main/java/Events.java | 10 ++- src/main/java/MainWindow.java | 12 +++- src/main/java/Task.java | 10 +-- src/main/java/TaskList.java | 30 ++++++--- src/main/java/ToDos.java | 2 +- 12 files changed, 141 insertions(+), 87 deletions(-) diff --git a/build.gradle b/build.gradle index a4820a76a9..8583807dc7 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ repositories { } final def application = application { - mainClassName = "Main" + mainClassName = "Launcher" } application diff --git a/src/main/java/Deadlines.java b/src/main/java/Deadlines.java index 14e244a8cb..2dd93930c2 100644 --- a/src/main/java/Deadlines.java +++ b/src/main/java/Deadlines.java @@ -3,23 +3,34 @@ import java.time.LocalDate; /** - * Class for Deadlines obj + * Class for Deadlines. */ -public class Deadlines extends Task{ +public class Deadlines extends Task { protected String by; private LocalDate date; + /** + * Deadline constructor. + * @param description description of deadlines + * @param isDone if task is done + * @param by deadline date in format yyyy-mm-dd + */ public Deadlines(String description, Boolean isDone, String by) { super(description, isDone); this.by = by; date = LocalDate.parse(by); } + /** + * returns date of deadline. + * @return date task is due + */ public String getDeadline() { return this.by; } /** + * changes format of date. * @return String value after modifying the date from yyyy-mm-dd to MMM DD YYYY */ @Override diff --git a/src/main/java/DialogBox.java b/src/main/java/DialogBox.java index cd7adba0c5..f5eab5ca2b 100644 --- a/src/main/java/DialogBox.java +++ b/src/main/java/DialogBox.java @@ -11,8 +11,7 @@ import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; -import javafx.scene.layout.*; -import javafx.scene.paint.Color; +import javafx.scene.layout.HBox; import javafx.scene.shape.Circle; /** @@ -61,6 +60,12 @@ public static DialogBox getUserDialog(String text, Image img) { return new DialogBox(text, img); } + /** + * returns flipped dialogbox. + * @param text text in dialogbox + * @param img image of duke + * @return returns a flipped dialogbox + */ public static DialogBox getDukeDialog(String text, Image img) { var db = new DialogBox(text, img); db.flip(); diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e117318a9e..79f238a077 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,14 +2,23 @@ public class Duke { private DukeParser parser; + private DukeStorage storage; + private TaskList tasks; + /** + * Duke constructor. + */ public Duke() { String filePath = "tasks.txt"; - DukeStorage storage = new DukeStorage(filePath); - TaskList tasks = storage.readText(); + storage = new DukeStorage(filePath); + tasks = storage.readText(); parser = new DukeParser(tasks); } + public void saveData() { + storage.saveTasks(tasks); + } + public String getResponse(String input) { return parser.parseCommand(input); } diff --git a/src/main/java/DukeParser.java b/src/main/java/DukeParser.java index 71959634aa..d13c49945f 100644 --- a/src/main/java/DukeParser.java +++ b/src/main/java/DukeParser.java @@ -11,88 +11,88 @@ public DukeParser(TaskList tasks) { } /** - * - * @param command + * parses user input as commands and generates a response. + * @param command user input * @return Bot response after processing user's command */ public String parseCommand(String command) { - String[] command_broken = command.split(" ",2); + String[] commandBroken = command.split(" ",2); - String action = command_broken[0]; + String action = commandBroken[0]; - try { + try { - if (action.equalsIgnoreCase("bye")) { - return DukeUI.showByeMsg(); + if (action.equalsIgnoreCase("bye")) { + return DukeUI.showByeMsg(); - } else if (action.equalsIgnoreCase("list")) { - return tasks.printTasks(); + } else if (action.equalsIgnoreCase("list")) { + return tasks.printTasks(); - } else if (action.equalsIgnoreCase("done")) { + } else if (action.equalsIgnoreCase("done")) { - String context = command_broken[1]; - int taskNo = Integer.parseInt(context); + String context = commandBroken[1]; + int taskNo = Integer.parseInt(context); - return DukeUI.showDoneMsg(tasks.markDone(taskNo)); + return DukeUI.showDoneMsg(tasks.markDone(taskNo)); - } else if (action.equalsIgnoreCase("delete")) { + } else if (action.equalsIgnoreCase("delete")) { - String context = command_broken[1]; - int taskNo = Integer.parseInt(context); - return tasks.removeTask(taskNo); + String context = commandBroken[1]; + int taskNo = Integer.parseInt(context); + return tasks.removeTask(taskNo); - } else if (action.equalsIgnoreCase(("deadline"))) { + } else if (action.equalsIgnoreCase(("deadline"))) { - String context = command_broken[1]; - String[] context_broken = context.split(" /by ", 2); - Task tempTask = tasks.addTask(new Deadlines(context_broken[0], false ,context_broken[1])); - return DukeUI.showCreationMsg(tempTask); + String context = commandBroken[1]; + String[] contextBroken = context.split(" /by ", 2); + Task tempTask = tasks.addTask(new Deadlines(contextBroken[0], false, contextBroken[1])); + return DukeUI.showCreationMsg(tempTask); - } else if (action.equalsIgnoreCase(("todo"))) { + } else if (action.equalsIgnoreCase(("todo"))) { - if (command_broken.length == 1) { - throw new DukeException("THE DESCRIPTION OF TODO CANNOT BE EMPTY"); - } - Task tempTask = tasks.addTask(new ToDos(command_broken[1], false)); - return DukeUI.showCreationMsg(tempTask); + if (commandBroken.length == 1) { + throw new DukeException("THE DESCRIPTION OF TODO CANNOT BE EMPTY"); + } + Task tempTask = tasks.addTask(new ToDos(commandBroken[1], false)); + return DukeUI.showCreationMsg(tempTask); - } else if (action.equalsIgnoreCase(("event"))) { + } else if (action.equalsIgnoreCase(("event"))) { - String context = command_broken[1]; - String[] context_broken = context.split(" /at ", 2); - Task tempTask = tasks.addTask(new Events(context_broken[0], false, context_broken[1])); - return DukeUI.showCreationMsg(tempTask); + String context = commandBroken[1]; + String[] contextBroken = context.split(" /at ", 2); + Task tempTask = tasks.addTask(new Events(contextBroken[0], false, contextBroken[1])); + return DukeUI.showCreationMsg(tempTask); - } else if (action.equalsIgnoreCase("find")) { + } else if (action.equalsIgnoreCase("find")) { - int count = 1; - for (Task t : tasks.getAllTasks()) { - if (t.containsSubstring(command_broken[1])) { - System.out.println(count + "." + t); - count++; - } + int count = 1; + for (Task t : tasks.getAllTasks()) { + if (t.containsSubstring(commandBroken[1])) { + System.out.println(count + "." + t); + count++; } - return DukeUI.showFindMsg(); - } else if (action.equalsIgnoreCase("archive")) { - - String context = command_broken[1]; - int taskNo = Integer.parseInt(context); - return tasks.archiveTask(taskNo); + } + return DukeUI.showFindMsg(); + } else if (action.equalsIgnoreCase("archive")) { - } else if (action.equalsIgnoreCase("archiveall")) { + String context = commandBroken[1]; + int taskNo = Integer.parseInt(context); + return tasks.archiveTask(taskNo); - return tasks.archiveAll(); + } else if (action.equalsIgnoreCase("archiveall")) { - } else if (action.equalsIgnoreCase("showarchive")){ - return tasks.showArchived(); - } else { - return "UNABLE TO COMPREHEND"; - } + return tasks.archiveAll(); - } catch (DukeException exception) { - System.out.println(exception.getMessage()); + } else if (action.equalsIgnoreCase("showarchive")) { + return tasks.showArchived(); + } else { + return "UNABLE TO COMPREHEND"; } + + } catch (DukeException exception) { + System.out.println(exception.getMessage()); + } return ""; } } diff --git a/src/main/java/DukeStorage.java b/src/main/java/DukeStorage.java index 50180be533..5f132dd5d3 100644 --- a/src/main/java/DukeStorage.java +++ b/src/main/java/DukeStorage.java @@ -5,7 +5,7 @@ import java.util.List; /** - * Handles all storage in duke + * Handles all storage in duke. */ public class DukeStorage { private String filePath; @@ -16,7 +16,7 @@ public DukeStorage(String filePath) { /** * Saves tasks to a .txt file in the same directory - * @param tasks + * @param tasks saves tasks */ public void saveTasks(TaskList tasks) { List allTasks = tasks.getAllTasks(); diff --git a/src/main/java/DukeUI.java b/src/main/java/DukeUI.java index bdf2c9d7a9..476cd8a232 100644 --- a/src/main/java/DukeUI.java +++ b/src/main/java/DukeUI.java @@ -1,15 +1,20 @@ /** - * User interface class for DUke + * User interface class for Duke. */ public class DukeUI { public DukeUI() { } - public static String showWelcomeMessage () { + /** + * welcome message for duke. + * ToDo: use ascii art + * @return welcome message + */ + public static String showWelcomeMessage() { StringBuilder sb = new StringBuilder(); sb.append("DEMOCRACY IS NON NEGOTIABLE\n\n"); - sb.append( "HI I'M:\n\n LIBERTY PRIME" ); + sb.append("HI I'M:\n\n LIBERTY PRIME"); return sb.toString(); } diff --git a/src/main/java/Events.java b/src/main/java/Events.java index 15be33dced..427646953a 100644 --- a/src/main/java/Events.java +++ b/src/main/java/Events.java @@ -1,9 +1,15 @@ /** - * Represents an Event object + * Represents an Event object. */ -public class Events extends Task{ +public class Events extends Task { protected String at; + /** + * Event constructor. + * @param description description of event + * @param isDone checks if event is done + * @param at more description of event + */ public Events(String description, Boolean isDone, String at) { super(description, isDone); this.at = at; diff --git a/src/main/java/MainWindow.java b/src/main/java/MainWindow.java index f53881d54a..747f83387b 100644 --- a/src/main/java/MainWindow.java +++ b/src/main/java/MainWindow.java @@ -1,16 +1,17 @@ +import javafx.application.Platform; import javafx.fxml.FXML; 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.VBox; /** * Controller for MainWindow. Provides the layout for the other controls. */ + public class MainWindow extends AnchorPane { + @FXML private ScrollPane scrollPane; @FXML @@ -41,6 +42,10 @@ public void setDuke(Duke d) { @FXML private void handleUserInput() { String input = userInput.getText(); + if (userInput.getText().equalsIgnoreCase("bye")) { + duke.saveData(); + Platform.exit(); + } String response = duke.getResponse(input); dialogContainer.getChildren().addAll( DialogBox.getUserDialog(input, userImage), @@ -49,6 +54,9 @@ private void handleUserInput() { userInput.clear(); } + /** + * greets user. + */ public void dukeGreet() { dialogContainer.getChildren().addAll( DialogBox.getDukeDialog(DukeUI.showWelcomeMessage(), dukeImage), diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 79116e0976..441ed5bc61 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,5 +1,5 @@ /** - * Represents a Task object + * Represents a Task object. */ public class Task { protected String description; @@ -12,11 +12,11 @@ public Task(String description, Boolean isDone) { /** * Creates a task using the .txt file at the start of the programme - * @param taskParams + * @param taskParams list of tasks as string of params * @return New task added to Task List - * @throws RuntimeException + * @throws RuntimeException esception */ - public static Task createStartingTask(String[] taskParams) throws RuntimeException{ + public static Task createStartingTask(String[] taskParams) throws RuntimeException { String type = taskParams[0]; boolean isDone = taskParams[1].equals("true"); String description = taskParams[2]; @@ -53,7 +53,7 @@ public String getDescription() { } /** - * Marks a task as done + * Marks a task as done. */ public Task markAsDone() { this.isDone = true; diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index b64c890688..5e236469ea 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -2,7 +2,7 @@ import java.util.List; /** - * Wrapper class for the list of Tasks + * Wrapper class for the list of Tasks. */ public class TaskList { private List allTasks; @@ -19,17 +19,13 @@ public Task addTask(Task s) { } /** - * Prints current Task + * Prints current Task. * @return List of Current Task */ public String printTasks() { return getString(allTasks); } - public Task getTask(int n) { - return this.allTasks.get(n-1); - } - public List getAllTasks() { return this.allTasks; } @@ -42,13 +38,23 @@ public Task markDone(int taskNo) { return allTasks.get(taskNo - 1).markAsDone(); } + /** + * removes task from list. + * @param taskNo task number + * @return returns confirmation message + */ public String removeTask(int taskNo) { Task tempTask = allTasks.remove(taskNo - 1); - return DukeUI.showDeleteMsg() + "\n" + - " " + tempTask + "\n" + - DukeUI.showCurrentListSize(allTasks.size()); + return DukeUI.showDeleteMsg() + + "\n" + " " + tempTask + "\n" + + DukeUI.showCurrentListSize(allTasks.size()); } + /** + * stores task in seperate list as archive. + * @param taskNo task number + * @return confirmation message + */ public String archiveTask(int taskNo) { Task tempTask = allTasks.remove(taskNo - 1); archivedTasks.add(tempTask); @@ -56,6 +62,10 @@ public String archiveTask(int taskNo) { } + /** + * archives all tasks. + * @return list of all archived tasks + */ public String archiveAll() { String archiveMessage = ""; for (int i = 1; i <= allTasks.size(); i++) { @@ -65,7 +75,7 @@ public String archiveAll() { } /** - * Prints archived task + * Prints archived task. * @return List of archived task */ public String showArchived() { diff --git a/src/main/java/ToDos.java b/src/main/java/ToDos.java index a4dbff2241..91ffffdff3 100644 --- a/src/main/java/ToDos.java +++ b/src/main/java/ToDos.java @@ -1,5 +1,5 @@ /** - * Represents a to do object + * Represents a to do object. */ public class ToDos extends Task {