Skip to content

Commit

Permalink
Added GUI
Browse files Browse the repository at this point in the history
Modify parser method (return value from void to String)
  • Loading branch information
SweetPotato0213 committed Sep 18, 2022
1 parent f22bafd commit 57cac3f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 139 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test {
}

application {
mainClassName = "Main.java"
mainClassName = "duke.Launcher"
}

shadowJar {
Expand All @@ -55,7 +55,7 @@ run{
standardInput = System.in
}

javafx {
/*javafx {
version = "18.0.2"
modules = [ 'javafx.controls' ]
}
}*/
15 changes: 11 additions & 4 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;

/**
* Duke is an interactive personal chat robot to keep track of user inputted tasks.
*
Expand Down Expand Up @@ -36,7 +38,7 @@ public Duke(String filePath) {
this.ui = new Ui();
this.storage = new Storage(filePath);
try {
this.tasks = new TaskList(storage.load());
this.tasks = new TaskList(storage.loadData());
} catch (DukeException e) {
ui.showLoadingError();
this.tasks = new TaskList();
Expand All @@ -48,13 +50,18 @@ public static void main(String[] args) {
}

private void run() {

ui.showWelcome();
new Parser(this.tasks).parser();
storage.save(this.tasks);
storage.saveData(this.tasks);
ui.farewell();
}

public String getResponse(String input) {
if (input.equals("bye")) {
storage.saveData(this.tasks);
}
return new Parser(this.tasks).parser(input);
}

@Override
public void start(Stage stage) {
//Step 1. Setting up required components
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class Main extends Application {

private Duke duke = new Duke();
private Duke duke = new Duke("data/duke.txt");

@Override
public void start(Stage stage) {
Expand Down
252 changes: 123 additions & 129 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,153 +23,147 @@ public Parser(TaskList tasks) {
/**
* Add corresponding tasks inputted by user to the list of tasks.
*/
public void parser() {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
public String parser(String input) {
String[] inputArr = input.split(" ");
String action = inputArr[0];

while (!action.equals("bye")) {
int number;
Task task;
String[] params;
try {
switch (action) {
case "list":
printList();
break;
case "mark":
if (inputArr.length > 2 || inputArr.length == 1) {
throw new DukeException("The format should be: mark <number>");
}
number = Integer.parseInt(inputArr[1]);
if (number > tasks.getSize()) {
throw new DukeException("The index is invalid!");
}
task = tasks.getTask(number - 1);
task.markAsDone();
System.out.println(BREAK_LINE + "\n"
+ " Nice! I've marked this task as done:\n "
+ task + "\n" + BREAK_LINE);
break;
case "unmark":
if (inputArr.length > 2 || inputArr.length == 1) {
throw new DukeException("The format should be: unmark <number>");
}
number = Integer.parseInt(inputArr[1]);
if (number > tasks.getSize()) {
throw new DukeException("The index is invalid!");
}
task = tasks.getTask(number - 1);
task.markAsNotDone();
System.out.println(BREAK_LINE + "\n"
+ " Nice! I've marked this task as not done yet:\n "
+ task + "\n" + BREAK_LINE);
break;
case "todo":
if (input.substring(4).replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of a todo cannot be empty.");
}
task = new Todo(input.substring(5));
tasks.addTask(task);
printTask(task);
break;
case "deadline":
if (input.substring(8).replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of a deadline cannot be empty.");
}
if (!input.contains("/by")) {
throw new DukeException("The timing of a deadline cannot be omitted.");
}
params = input.substring(9).split(" /by ");
task = new Deadline(params[0], params[1]);
tasks.addTask(task);
printTask(task);
break;
case "event":
if (input.substring(5).replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of an event cannot be empty.");
}
if (!input.contains("/at")) {
throw new DukeException("The timing of an event cannot be omitted.");
}
params = input.substring(6).split(" /at ");
task = new Event(params[0], params[1]);
tasks.addTask(task);
printTask(task);
break;
case "delete":
if (inputArr.length > 2 || inputArr.length == 1) {
throw new DukeException("The format should be: delete <number>");
}
number = Integer.parseInt(inputArr[1]);
if (number > tasks.getSize()) {
throw new DukeException("The index is invalid!");
}
task = tasks.getTask(number - 1);
tasks.deleteTask(number - 1);
System.out.println(BREAK_LINE + "\n"
+ " Okay! I've removed this task from the list:\n "
+ task + "\n" + BREAK_LINE + "\n");
break;
case "find":
String keyWord = input.substring(5);
if (keyWord.replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of a find query cannot be empty.");
}

ArrayList<Task> matches = new ArrayList<Task>();
for (int i = 0; i < tasks.getSize(); i++) {
task = tasks.getTask(i);
if (task.getDescription().contains(keyWord)) {
matches.add(task);
}
}
int number;
Task task;
String[] params;
try {
switch (action) {
case "list":
return printList();
case "mark":
if (inputArr.length > 2 || inputArr.length == 1) {
throw new DukeException("The format should be: mark <number>");
}
number = Integer.parseInt(inputArr[1]);
if (number > tasks.getSize()) {
throw new DukeException("The index is invalid!");
}
task = tasks.getTask(number - 1);
task.markAsDone();
return BREAK_LINE + "\n"
+ " Nice! I've marked this task as done:\n "
+ task + "\n" + BREAK_LINE;
case "unmark":
if (inputArr.length > 2 || inputArr.length == 1) {
throw new DukeException("The format should be: unmark <number>");
}
number = Integer.parseInt(inputArr[1]);
if (number > tasks.getSize()) {
throw new DukeException("The index is invalid!");
}
task = tasks.getTask(number - 1);
task.markAsNotDone();
return BREAK_LINE + "\n"
+ " Nice! I've marked this task as not done yet:\n "
+ task + "\n" + BREAK_LINE;
case "todo":
if (input.substring(4).replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of a todo cannot be empty.");
}
task = new Todo(input.substring(5));
tasks.addTask(task);
return printTask(task);
case "deadline":
if (input.substring(8).replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of a deadline cannot be empty.");
}
if (!input.contains("/by")) {
throw new DukeException("The timing of a deadline cannot be omitted.");
}
params = input.substring(9).split(" /by ");
task = new Deadline(params[0], params[1]);
tasks.addTask(task);
return printTask(task);
case "event":
if (input.substring(5).replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of an event cannot be empty.");
}
if (!input.contains("/at")) {
throw new DukeException("The timing of an event cannot be omitted.");
}
params = input.substring(6).split(" /at ");
task = new Event(params[0], params[1]);
tasks.addTask(task);
return printTask(task);
case "delete":
if (inputArr.length > 2 || inputArr.length == 1) {
throw new DukeException("The format should be: delete <number>");
}
number = Integer.parseInt(inputArr[1]);
if (number > tasks.getSize()) {
throw new DukeException("The index is invalid!");
}
task = tasks.getTask(number - 1);
tasks.deleteTask(number - 1);
return BREAK_LINE + "\n"
+ " Okay! I've removed this task from the list:\n "
+ task + "\n" + BREAK_LINE + "\n";
case "find":
String keyWord = input.substring(5);
if (keyWord.replaceAll("\\s+", "").equals("")) {
throw new DukeException("The description of a find query cannot be empty.");
}

if(matches.isEmpty()) {
throw new DukeException("There is no task matching this key word.");
} else {
System.out.println(BREAK_LINE + "\n"
+ " Okay! I've removed this task from the list:\n ");
for (int i = 0; i < matches.size(); i++) {
System.out.println(i + 1 + ". " + matches.get(i));
}
System.out.println(BREAK_LINE + "\n");
ArrayList<Task> matches = new ArrayList<Task>();
for (int i = 0; i < tasks.getSize(); i++) {
task = tasks.getTask(i);
if (task.getDescription().contains(keyWord)) {
matches.add(task);
}
break;
default:
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}
} catch (DukeException err) {
System.out.println(BREAK_LINE + "\n" + " ☹ OOPS!!! "
+ err + "\n" + BREAK_LINE + "\n");

return printMatch(matches);
default:
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}
input = sc.nextLine();
inputArr = input.split(" ");
action = inputArr[0];
} catch (DukeException err) {
return BREAK_LINE + "\n" + " ☹ OOPS!!! "
+ err + "\n" + BREAK_LINE + "\n";
}
}

private void printTask(Task task) {
System.out.println(BREAK_LINE + "\n"
private String printTask(Task task) {
String output = BREAK_LINE + "\n"
+ " Got it. I've added this task:\n "
+ task);
System.out.format(" Now you have %d tasks in the list.\n"
+ BREAK_LINE + "\n", tasks.getSize());
+ task;
output += String.format(" Now you have %d tasks in the list.\n"
+ BREAK_LINE + "\n", tasks.getSize());
return output;
}

private void printList() {
private String printList() {
if (tasks.getSize() == 0) {
System.out.println(BREAK_LINE + "\n"
return BREAK_LINE + "\n"
+ " There is no pending task for you."
+ "\n" + BREAK_LINE);
+ "\n" + BREAK_LINE;
} else {
System.out.println(BREAK_LINE + "\n"
+ " Here are the tasks in your list:");
String output = BREAK_LINE + "\n"
+ " Here are the tasks in your list:";
for (int i = 0; i < tasks.getSize(); i++) {
System.out.format(" %d.%s\n", i + 1, tasks.getTask(i));
output += String.format(" %d.%s\n", i + 1, tasks.getTask(i));
}
output += BREAK_LINE + "\n";
return output;
}
}

private String printMatch(ArrayList<Task> matches) {
if(matches.isEmpty()) {
return BREAK_LINE + "\n"
+ " There is no task matching this key word."
+ "\n" + BREAK_LINE;
} else {
String output = BREAK_LINE + "\n"
+ " Okay! I've found these matches from the list:\n ";
for (int i = 0; i < matches.size(); i++) {
output += String.format(" %d.%s\n", i + 1, matches.get(i));
}
System.out.println(BREAK_LINE);
output += BREAK_LINE + "\n";
return output;
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Storage(String path) {
* @return ArrayList of Task stored in a previously saved file if any or a new ArrayList.
* @throws DukeException If the type format in the .txt file is invalid.
*/
public ArrayList<Task> load() throws DukeException {
public ArrayList<Task> loadData() throws DukeException {
ArrayList<Task> tasks = new ArrayList<>();
try {
File taskFile = new File(this.path);
Expand Down Expand Up @@ -77,7 +77,7 @@ public ArrayList<Task> load() throws DukeException {
* Saves an ArrayList of Task in a .txt file
* @param tasks TaskList object consists of a list of task inputted by user
*/
public void save(TaskList tasks) {
public void saveData(TaskList tasks) {
try {
FileWriter fw = new FileWriter(this.path);
for(int i = 0; i < tasks.getSize(); i++) {
Expand Down

0 comments on commit 57cac3f

Please sign in to comment.