Skip to content

Commit

Permalink
GUI bug fixed
Browse files Browse the repository at this point in the history
Changed return type to String so return type can be more easily accessed  by the GUI.
  • Loading branch information
Gavzzz committed Sep 19, 2022
1 parent b98175d commit 3301a16
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 159 deletions.
6 changes: 0 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ repositories {
mavenCentral()
}

test {
useJUnitPlatform()
}



dependencies {

Expand Down Expand Up @@ -61,5 +56,4 @@ shadowJar {

run {
standardInput = System.in
enableAssertions = true
}
54 changes: 20 additions & 34 deletions src/main/java/Duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,41 @@
package Duke;

import Duke.commands.Command;
import java.util.ArrayList;

import java.util.List;

import Duke.gui.Main;
import javafx.application.Application;
import java.io.IOException;


/* Inspiration taken from Bag Devesh Kumar and Zizheng ip to solve some prevalent issues
*/
public class Duke {

private final Storage storage;
private final TaskList tasks;
private final Ui ui;

private Storage storage;
private TaskList tasks;
private Ui ui;

public Duke() throws IOException {
public Duke() throws IOException, DukeException {
ui = new Ui();
storage = new Storage();
tasks = new TaskList(storage.fileToList());
}

public void run() {
ui.showLogo();
boolean isExit = false;
while (!isExit) {
try {
String fullInput = ui.readInput();
ui.printDash();
Command c = Parser.parse(fullInput);
List<String> reply = c.execute(tasks, ui, storage);
for (String line : reply) {
ui.printLine(line);
}
isExit = c.isExit();
} catch (IOException | DukeException e) {
ui.printError(e.getMessage());
} finally {
ui.printDash();
}
}
}

public static void main(String[] args) {
try {
new Duke().run();
} catch (IOException e) {
System.out.println(e.getMessage());
}
public String getResponse(String input) throws DukeException, IOException {
Command c = Parser.parse(input);
String message = c.execute(tasks, ui, storage);
return message;
}
}











5 changes: 3 additions & 2 deletions src/main/java/Duke/commands/ByeCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
public class ByeCommands extends Command {

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
return List.of(ui.printBye());
public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
String message = ui.printBye();
return message;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Duke/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Duke.commands;

import java.util.ArrayList;
import java.util.List;
import Duke.DukeException;
import Duke.Storage;
Expand All @@ -25,7 +26,7 @@ public abstract class Command {
* @throws DukeException when there is inappropriate input or save file issues.
*/

public abstract List<String> execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException;
public abstract String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException;


/**
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/Duke/commands/DeadlineCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class DeadlineCommands extends Executor {

private String input;

private final static Pattern deadlinePattern = Pattern.compile("(?<taskName>.*) /by (?<by>.*)");

/**
Expand All @@ -38,16 +40,16 @@ public DeadlineCommands(String input) {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
ArrayList<String> text = new ArrayList<>();
Matcher match = deadlinePattern.matcher(description);
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
Matcher match = deadlinePattern.matcher(input);
if (!match.matches()) {
throw new DukeException("No deadline was given, try again");
}
text.add("I've added this task:");
String line1 = "I've added this task:";
Task addedTask = tasks.addTask(new Deadline(match.group("taskName"), match.group("by")));
text.add(addedTask.toString());
text.addAll(super.execute(tasks, ui, storage));
return text;
String line2 = addedTask.toString();
String line3 = super.execute(tasks, ui, storage);
String reply = line1 + "\n" + line2 + "\n" + line3;
return reply;
}
}
16 changes: 9 additions & 7 deletions src/main/java/Duke/commands/DeleteCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

public class DeleteCommands extends TaskCommands {

public DeleteCommands(String fullInput) throws DukeException {
super(fullInput);
private String input;

public DeleteCommands(String input) throws DukeException {
super(input);
}

/**
Expand All @@ -24,13 +26,13 @@ public DeleteCommands(String fullInput) throws DukeException {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
if (tasks.isValidTaskNumber(taskNumber)) {
ArrayList<String> text = new ArrayList<>();
text.add("I've removed this task: ");
text.add(tasks.getTaskToString(taskNumber));
String line1 = "I've removed this task: ";
String line2 = tasks.getTaskToString(taskNumber);
tasks.removeTask(taskNumber);
return text;
String reply = line1 + line2;
return reply;
} else {
throw new DukeException("This task does not exists");
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/Duke/commands/DetectDuplicateCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ public class DetectDuplicateCommands extends Command {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
int counter = 0;
ArrayList<String> text = new ArrayList<>();
for (int i = 0; i < tasks.getTaskListSize() -1 ; i++) {
for (int j = i + 1; j < tasks.getTaskListSize(); j++) {
if (tasks.getTask(i) == tasks.getTask(j)) {
text.add("There seems to be a duplicate task in your planner");
text.add("The duplicate task is " + tasks.getTaskToString(i));
counter++;
break;
}
}
}
text.add("You have " + counter + " duplicate tasks");
return text;
if (counter == 0) {
String reply = "You have no duplicate tasks";
return reply;
} else {
String reply = "You have " + counter + " duplicate tasks";
return reply;
}
}
}

12 changes: 6 additions & 6 deletions src/main/java/Duke/commands/EventCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ public EventCommands(String input) {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
ArrayList<String> text = new ArrayList<>();
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
Matcher match = eventPattern.matcher(description);
if (!match.matches()) {
throw new DukeException("No event time was given, try again");
}
text.add("I've added this task:");
String line1 = "I've added this task:";
Task addedTask = tasks.addTask(new Event(match.group("taskName"), match.group("at")));
text.add(addedTask.toString());
String line2 = addedTask.toString();
storage.addTask(addedTask);
text.addAll(super.execute(tasks, ui, storage));
return text;
String line3 = super.execute(tasks, ui, storage);
String reply = line1 + "\n" + line2 + "\n" + line3;
return reply;
}
}
4 changes: 2 additions & 2 deletions src/main/java/Duke/commands/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Executor(String input) {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
return List.of(tasks.getSizeToString());
public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
return tasks.getSizeToString();
}
}
12 changes: 6 additions & 6 deletions src/main/java/Duke/commands/FindCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public FindCommands(String input) throws DukeException {
}

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
ArrayList<String> text = new ArrayList<>();
public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
TaskList matches = tasks.filter(Keywords);

text.add(String.format("Here are the matching tasks \"%s\":",
String.join("\", \"", Keywords)));
String line1 = String.format("Here are the matching tasks \"%s\":",
String.join("\", \"", Keywords));

for (int i = 1; matches.isValidTaskNumber(i); i++) {
text.add(String.format("%d. %s", i + 1, matches.getTaskToString(i)));
String line2 = String.format("%d. %s", i + 1, matches.getTaskToString(i));
line1 += line2;
}
return text;
return line1;

}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/Duke/commands/ListCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class ListCommands extends Command {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
ArrayList<String> text = new ArrayList<>();
text.add("Here are the tasks in your list:");
public String execute(TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
String line1 = "Here are the tasks in your list:";
for (int i = 1; tasks.isValidTaskNumber(i); i++) {
text.add(i + ". " + tasks.getTaskToString(i));
String line2 = i + ". " + tasks.getTaskToString(i);
line1 += "\n" + line2;
}
return text;
return line1;
}
}
10 changes: 5 additions & 5 deletions src/main/java/Duke/commands/MarkCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public MarkCommands(String input) throws DukeException {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
if (tasks.isValidTaskNumber(taskNumber)) {
ArrayList<String> text = new ArrayList<>();
text.add("I've marked this task as done!");
String line1 = "I've marked this task as done!";
tasks.markAsDone(taskNumber);
storage.updateTask(taskNumber, Constants.MARK);
text.add(tasks.getTaskToString(taskNumber));
return text;
String line2 = tasks.getTaskToString(taskNumber);
String reply = line1 + line2;
return reply;
} else {
throw new DukeException("This task does not exist.");
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/Duke/commands/TaskCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public abstract class TaskCommands extends Command {
protected final int taskNumber;


public TaskCommands(String fullInput) throws DukeException {
String[] commands = fullInput.split(" ");
public TaskCommands(String input) throws DukeException {
String[] commands = input.split(" ");
try {
taskNumber = Integer.parseInt(commands[1]);
} catch (NumberFormatException e) {
Expand All @@ -23,11 +23,10 @@ public TaskCommands(String fullInput) throws DukeException {
}

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
if (tasks.isValidTaskNumber(taskNumber)) {
ArrayList<String> text = new ArrayList<>();
text.add(tasks.getTaskToString(taskNumber));
return text;
String line1 = tasks.getTaskToString(taskNumber);
return line1;
} else {
throw new DukeException("No such task exist.");
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/Duke/commands/TodoCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public TodoCommands(String input) {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
ArrayList<String> text = new ArrayList<>();
text.add("I've added this task:");
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
String line1 = "I've added this task:";
Task addedTask = tasks.addTask(new Todo(description));
text.add(addedTask.toString());
String line2 = addedTask.toString();
storage.addTask(addedTask);
text.addAll(super.execute(tasks, ui, storage));
return text;
String line3 = super.execute(tasks, ui, storage);
String reply = line1 + "\n" + line2 + "\n" + line3;
return reply;
}
}
10 changes: 5 additions & 5 deletions src/main/java/Duke/commands/UnmarkCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public UnmarkCommands(String input) throws DukeException {
*/

@Override
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, IOException {
if (tasks.isValidTaskNumber(taskNumber)) {
ArrayList<String> text = new ArrayList<>();
text.add("I've marked this task as not done (yet ;))");
String line1 = "I've marked this task as not done (yet ;))";
tasks.markAsNotDone(taskNumber);
storage.updateTask(taskNumber, Constants.UNMARK);
text.add(tasks.getTaskToString(taskNumber));
return text;
String line2 = tasks.getTaskToString(taskNumber);
String reply = line1 + line2;
return reply;
} else {
throw new DukeException("tasks.Task does not exist.");
}
Expand Down
Loading

0 comments on commit 3301a16

Please sign in to comment.