Skip to content

Commit

Permalink
Merge pull request #3 from dreammac3816547290/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
Examines the code and refactor to improve the code quality where nece…
  • Loading branch information
dreammac3816547290 authored Sep 18, 2022
2 parents c42cbc0 + 76739f2 commit 392fb2b
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 205 deletions.
2 changes: 1 addition & 1 deletion src/main/java/duke/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AddCommand(char type, String description, String detail) {
* @param duke Duke instance to run the AddCommand at.
*/
@Override
public void run(Duke duke) {
public void run(Duke duke) throws DukeException {
Task task;
switch (type) {
case 'T':
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public abstract class Command {
*
* @param duke Duke instance to run the Command at.
*/
public abstract void run(Duke duke);
public abstract void run(Duke duke) throws DukeException;
}
11 changes: 8 additions & 3 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Represents a Deadline, which is a Task with a deadline.
Expand All @@ -15,9 +16,13 @@ public class Deadline extends Task {
* @param description Description of the Task.
* @param deadline Deadline of the Task.
*/
public Deadline(String description, String deadline) {
public Deadline(String description, String deadline) throws DukeException {
super(description);
this.deadline = LocalDate.parse(deadline);
try {
this.deadline = LocalDate.parse(deadline);
} catch (DateTimeParseException e) {
throw new DukeException("Incorrect date format!");
}
type = 'D';
}

Expand All @@ -28,7 +33,7 @@ public Deadline(String description, String deadline) {
* @param isDone Boolean to set the Task as done or not done.
* @param deadline Deadline of the Task.
*/
public Deadline(String description, boolean isDone, String deadline) {
public Deadline(String description, boolean isDone, String deadline) throws DukeException {
this(description, deadline);
this.isDone = isDone;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public DeleteCommand(int index) {
* @param duke Duke instance to run the Command at.
*/
@Override
public void run(Duke duke) {
public void run(Duke duke) throws DukeException {
duke.deleteTask(index);
}
}
22 changes: 14 additions & 8 deletions src/main/java/duke/DialogBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class DialogBox extends HBox {
@FXML
private ImageView displayPicture;

/**
* Constructor of DialogBox, with text and image to put inside the DialogBox.
*
* @param text Text to put in the DialogBox.
* @param img Image to put in the DialogBox.
*/
private DialogBox(String text, Image img) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("/view/DialogBox.fxml"));
Expand All @@ -50,11 +56,11 @@ private void flip() {
}

/**
* Takes user input as text and user image as img and returns user's DialogBox, which is left-aligned.
*
*
* @param text
* @param img
* @return
* @param text User input.
* @param img User image.
* @return User's DialogBox, left-aligned.
*/
public static DialogBox getUserDialog(String text, Image img) {
var db = new DialogBox(text, img);
Expand All @@ -63,11 +69,11 @@ public static DialogBox getUserDialog(String text, Image img) {
}

/**
* Takes output from Duke as text and Duke image as img and returns Duke's DialogBox, which is right-aligned.
*
*
* @param text
* @param img
* @return
* @param text Duke output.
* @param img Duke image.
* @return Duke's DialogBox, right-aligned.
*/
public static DialogBox getDukeDialog(String text, Image img) {
return new DialogBox(text, img);
Expand Down
81 changes: 33 additions & 48 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand All @@ -16,9 +15,9 @@
* Represents Duke, a Personal Assistant Chatterbot that helps a person to keep track of various things.
*/
public class Duke extends Application {
private final Storage storage = new Storage("./duke.txt");
private final TaskList tasks = storage.load();
private final Ui ui = new Ui();
private Storage storage;
private TaskList tasks;
private Ui ui;
private boolean isActive = true;

private ScrollPane scrollPane;
Expand All @@ -30,27 +29,29 @@ public class Duke extends Application {
private Image duke = new Image(this.getClass().getResourceAsStream("/images/Duke.png"));

/**
* Start the Duke Chatterbot.
* Constructor of Duke.
*/
public void startDuke() {
ui.greet();
while (isActive) {
String input = ui.read();
Command command = Parser.parseInput(input);
command.run(this);
storage.save(tasks);
} // System.out.println(e.getMessage());
ui.close();
public Duke() {
try {
storage = new Storage("./duke.txt");
tasks = storage.load();
ui = new Ui();
} catch (DukeException e) {
System.out.println(e.getMessage());
}
}

/**
* Add a Task to Duke.
*
* @param task Task to add.
*/
public void addTask(Task task) { // error if description is empty
public void addTask(Task task) throws DukeException { // error if description is empty
assert tasks != null;
assert task != null;
if (task.description.isBlank()) {
throw new DukeException("Task description is empty!");
}
tasks.add(task);
ui.addTask(task);
ui.infoCount(tasks.size());
Expand All @@ -70,10 +71,10 @@ public void printTasks() {
* @param index Index of Task to mark.
* @param isDone Boolean to mark the Task as done or not done.
*/
public void markTask(int index, boolean isDone) {
public void markTask(int index, boolean isDone) throws DukeException {
assert tasks != null;
if (index < 0 || index >= tasks.size()) {
return; // throw new duke.DukeException("Index out of bound!");
throw new DukeException("Index out of bound!");
}
Task task = tasks.get(index);
assert task != null;
Expand All @@ -91,10 +92,10 @@ public void markTask(int index, boolean isDone) {
*
* @param index Index of Task to delete.
*/
public void deleteTask(int index) {
public void deleteTask(int index) throws DukeException {
assert tasks != null;
if (index < 0 || index >= tasks.size()) {
return; // throw new duke.DukeException("Index out of bound!");
throw new DukeException("Index out of bound!");
}
Task task = tasks.remove(index);
assert task != null;
Expand All @@ -121,12 +122,13 @@ public void findTasks(String keyword) {
public void exit() {
assert isActive;
isActive = false;
ui.close();
}

/**
* Starts the Duke GUI.
*
*
* @param stage
* @param stage Stage to show the GUI elements.
*/
@Override
public void start(Stage stage) {
Expand Down Expand Up @@ -198,21 +200,6 @@ public void start(Stage stage) {
}

/**
* Iteration 1:
* Creates a label with the specified text and adds it to the dialog container.
* @param text String containing text to add
* @return a label with the specified text that has word wrap enabled.
*/
private Label getDialogLabel(String text) {
// You will need to import `javafx.scene.control.Label`.
Label textToAdd = new Label(text);
textToAdd.setWrapText(true);

return textToAdd;
}

/**
* 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.
*/
Expand All @@ -227,22 +214,20 @@ private void handleUserInput() {
}

/**
* Takes user input and returns Duke's response to the input.
*
* @param input User input.
* @return Duke's response to the input.
*/
protected String getResponse(String input) {
assert input != null;
Command command = Parser.parseInput(input);
command.run(this);
storage.save(tasks);
try {
Command command = Parser.parseInput(input);
command.run(this);
storage.save(tasks);
} catch (DukeException e) {
ui.print(e.getMessage());
}
return ui.collect();
}

/**
*
*
* @param args
*/
public static void main(String[] args) {
new Duke().startDuke();
}
}
11 changes: 8 additions & 3 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Represents an Event, which is a Task with time.
Expand All @@ -15,9 +16,13 @@ public class Event extends Task {
* @param description Description of the Event.
* @param time Time of the Event.
*/
public Event(String description, String time) {
public Event(String description, String time) throws DukeException {
super(description);
this.time = LocalDate.parse(time);
try {
this.time = LocalDate.parse(time);
} catch (DateTimeParseException e) {
throw new DukeException("Incorrect date format!");
}
type = 'E';
}

Expand All @@ -28,7 +33,7 @@ public Event(String description, String time) {
* @param isDone Boolean to set the Event as done or not done.
* @param time Time of the Event.
*/
public Event(String description, boolean isDone, String time) {
public Event(String description, boolean isDone, String time) throws DukeException {
this(description, time);
this.isDone = isDone;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class Main extends Application {

private Duke duke = new Duke();

/**
* Starts the Duke GUI.
*
* @param stage Stage to show the GUI elements.
*/
@Override
public void start(Stage stage) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/duke/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public class MainWindow extends AnchorPane {
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/Duke.png"));

/**
*
* Initializes the MainWindow.
*/
@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
}

/**
* Sets the associated Duke instance.
*
*
* @param d
* @param d Duke instance to be associated.
*/
public void setDuke(Duke d) {
duke = d;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MarkCommand(int index, boolean isDone) {
* @param duke Duke instance to run the MarkCommand at.
*/
@Override
public void run(Duke duke) {
public void run(Duke duke) throws DukeException {
duke.markTask(index, isDone);
}
}
Loading

0 comments on commit 392fb2b

Please sign in to comment.