Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code quality #2

Merged
merged 1 commit into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test {
}

application {
mainClassName = "duke.Launcher"
mainClassName = "duke.Duke"
}

shadowJar {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Class used to represent a task that has a due date.
*/
public class Deadline extends Task {
public static final String TASK_TYPE_CHARACTER = "D";

protected LocalDate dueDate;

/**
Expand All @@ -22,11 +24,11 @@ public Deadline(String taskName, boolean isDone, LocalDate dueDate) {

@Override
public String toSaveFormatString() {
return String.format("D|%d|%s|%s", isDone ? 1 : 0, taskName, dueDate);
return String.format("%s|%d|%s|%s", TASK_TYPE_CHARACTER, isDone ? 1 : 0, taskName, dueDate);
}

@Override
public String toString() {
return String.format("[D]%s (by: %s)", super.toString(), dueDate);
return String.format("[%s]%s (by: %s)", TASK_TYPE_CHARACTER, super.toString(), dueDate);
}
}
6 changes: 6 additions & 0 deletions src/main/java/duke/DialogueBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;

/**
* This control represents a dialogue box consisting of an ImageView to represent the speaker's face and a label
Expand All @@ -38,6 +39,11 @@ private DialogueBox(String text, Image img) {
displayPicture.setImage(img);
}

@FXML
public void initialize() {
dialogue.setFont(Font.font("Cascadia Mono", 12));
}

/**
* Flips the dialog box such that the ImageView is on the left and text on the right.
*/
Expand Down
33 changes: 11 additions & 22 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package duke;

import java.io.IOException;
import java.util.Scanner;

import javafx.application.Application;

/**
* The main class for the Duke program.
Expand All @@ -18,8 +19,6 @@ public static void initialize() {
try {
Storage.loadData();
} catch (IOException e) {
System.out.println("Error: Failed to access data");

addToResponse("Error: Failed to access data");
}

Expand All @@ -36,8 +35,6 @@ public static void exit() {
try {
Storage.saveData();
} catch (IOException e) {
System.out.println("Error: Failed to access data");

addToResponse("Error: Failed to access data");
}

Expand All @@ -58,15 +55,21 @@ public static void giveInput(String input) {
try {
Parser.parseInput(input);
} catch (DukeException e) {
System.out.println(e.getMessage());

addToResponse(e.getMessage() + "\n");
}
Ui.generateLine();
}

/**
* Gives Duke a string store as a response to the user.
* When getResponse is called, the string is retrieved for displaying.
*
* @param output The response string to be stored.
*/
public static void addToResponse(String output) {
response.append(output);

System.out.print(output);
}

/**
Expand Down Expand Up @@ -100,20 +103,6 @@ public static boolean getIsRunning() {
* @param args The command line arguments.
*/
public static void main(String[] args) {
initialize();

Scanner scanner = new Scanner(System.in);

while (isRunning) {
Ui.generateLine();
String input = scanner.nextLine();
Ui.generateLine();

try {
Parser.parseInput(input);
} catch (DukeException e) {
System.out.println(e.getMessage());
}
}
Application.launch(Main.class, args);
}
}
6 changes: 4 additions & 2 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Class used to represent a task that has a start date.
*/
public class Event extends Task {
public static final String TASK_TYPE_CHARACTER = "E";

protected LocalDate eventDate;

/**
Expand All @@ -22,11 +24,11 @@ public Event(String taskName, boolean isDone, LocalDate eventDate) {

@Override
public String toSaveFormatString() {
return String.format("E|%d|%s|%s", isDone ? 1 : 0, taskName, eventDate);
return String.format("%s|%d|%s|%s", TASK_TYPE_CHARACTER, isDone ? 1 : 0, taskName, eventDate);
}

@Override
public String toString() {
return String.format("[E]%s (at: %s)", super.toString(), eventDate);
return String.format("[%s]%s (at: %s)", TASK_TYPE_CHARACTER, super.toString(), eventDate);
}
}
12 changes: 0 additions & 12 deletions src/main/java/duke/Launcher.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void start(Stage stage) {
stage.show();
} catch (IOException e) {
e.printStackTrace();

}
}
}
1 change: 1 addition & 0 deletions src/main/java/duke/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private void handleUserInput() {

if (!Duke.getIsRunning()) {
stage.close();
return;
}

Duke.giveInput(input);
Expand Down
31 changes: 21 additions & 10 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
* A class that handles user input.
*/
public class Parser {
private static final String GREET_COMMAND = "hello";
private static final String EXIT_COMMAND = "bye";
private static final String DISPLAY_COMMAND = "list";
private static final String MARK_COMMAND = "mark";
private static final String UNMARK_COMMAND = "unmark";
private static final String CREATE_TODO_COMMAND = "todo";
private static final String CREATE_DEADLINE_COMMAND = "deadline";
private static final String CREATE_EVENT_COMMAND = "event";
private static final String DELETE_COMMAND = "delete";
private static final String FIND_COMMAND = "find";

/**
* Parses an input string and calls the relevant method (if any).
*
Expand All @@ -18,33 +29,33 @@ public static void parseInput(String input) throws DukeException {
}

switch (command) {
case "hello":
case GREET_COMMAND:
Ui.displayGreeting();
break;
case "bye":
case EXIT_COMMAND:
Duke.exit();
break;
case "list":
case DISPLAY_COMMAND:
Ui.displayTasks();
break;
case "mark":
case "unmark":
case MARK_COMMAND:
case UNMARK_COMMAND:
boolean isDone = command.equals("mark");
Ui.displayMarkTaskMessage(TaskList.markTask(isDone, args), isDone);
break;
case "todo":
case CREATE_TODO_COMMAND:
Ui.displayAddTaskMessage(TaskList.addToDo(args));
break;
case "deadline":
case CREATE_DEADLINE_COMMAND:
Ui.displayAddTaskMessage(TaskList.addDeadline(args));
break;
case "event":
case CREATE_EVENT_COMMAND:
Ui.displayAddTaskMessage(TaskList.addEvent(args));
break;
case "delete":
case DELETE_COMMAND:
Ui.displayDeleteTaskMessage(TaskList.deleteTask(args));
break;
case "find":
case FIND_COMMAND:
Ui.displaySearchTasksMessage(TaskList.searchTasks(args), args);
break;
default:
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public static void loadData() throws IOException {
String saveFormatString = scanner.nextLine();
String[] args = saveFormatString.split("\\|", 4);
if (args.length < 3) {
System.out.println("Invalid task save string, task not added");
Duke.addToResponse("Invalid task save string, task not added");
continue;
}

String taskType = args[0].strip();
boolean marked = args[1].strip().equals("1") ? true : false;
String taskName = args[2].strip();
if (taskName == "") {
System.out.println("Invalid task save string, task not added");
Duke.addToResponse("Invalid task save string, task not added");
continue;
}

Expand All @@ -58,20 +58,20 @@ public static void loadData() throws IOException {
try {
date = LocalDate.parse(args[3].strip());
} catch (DateTimeParseException e) {
System.out.println("Invalid date in task save string, task not added");
Duke.addToResponse("Invalid date in task save string, task not added");
}
}

switch (taskType) {
case "T":
case ToDo.TASK_TYPE_CHARACTER:
TaskList.addToList(new ToDo(taskName, marked));
break;
case "D":
case Deadline.TASK_TYPE_CHARACTER:
if (date != null) {
TaskList.addToList(new Deadline(taskName, marked, date));
}
break;
case "E":
case Event.TASK_TYPE_CHARACTER:
if (date != null) {
TaskList.addToList(new Event(taskName, marked, date));
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public static void saveData() throws IOException {

dataFileWriter.close();
} catch (IOException e) {
System.out.println("Error: Failed to save tasks");
Duke.addToResponse("Error: Failed to save tasks");
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/duke/ToDo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
* Class used to represent a ToDo type task that has no date.
*/
public class ToDo extends Task {
public static final String TASK_TYPE_CHARACTER = "T";

public ToDo(String taskName, boolean isDone) {
super(taskName, isDone);
}

@Override
public String toSaveFormatString() {
return String.format("T|%d|%s", isDone ? 1 : 0, taskName);
return String.format("%s|%d|%s", TASK_TYPE_CHARACTER, isDone ? 1 : 0, taskName);
}

@Override
public String toString() {
return "[T]" + super.toString();
return String.format("[%s]%s", TASK_TYPE_CHARACTER, super.toString());
}
}
Loading