Skip to content
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ shadowJar {

run{
standardInput = System.in
enableAssertions = true
}
1 change: 0 additions & 1 deletion data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
D | 0 | task 1 | Jan. 1 2023
9 changes: 6 additions & 3 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import duke.storage.Storage;
import duke.storage.TaskList;

/**
* Duke class
*/
public class Duke {
public static final Scanner SCANNER = new Scanner(System.in);
public static final String FILEPATH = "./data/tasks.txt";

private TaskList taskList;

/**
* Constructs the Duke class.
*
Expand Down Expand Up @@ -51,12 +54,12 @@ public String getResponse(String input) {
*
* @param args arguments
*/
public static void main(String[] args) throws DukeException{
public static void main(String[] args) throws DukeException {
new Duke("data/tasks.txt");
Ui.greet();
String input = SCANNER.nextLine();

while(!input.equals("bye")) {
while (!input.equals("bye")) {
try {
Parser.parse(input);
} catch (DukeException e) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/duke/helper/DukeException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package duke.helper;

/**
* Duke Exception class
*/
public class DukeException extends Exception {

/**
Expand All @@ -10,7 +13,7 @@ public class DukeException extends Exception {
public DukeException(String message) {
super(message);
}

/**
* returns the detail message of this throwable
*
Expand All @@ -27,8 +30,7 @@ public String getMessage() {
* @return the message of the exception
*/
@Override
public String toString()
{
public String toString() {
return ":( Oh no! " + super.getMessage();
}
}
}
74 changes: 40 additions & 34 deletions src/main/java/duke/helper/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import duke.tasks.Task;
import duke.tasks.ToDo;

/**
* Parser class for taking in user input and analyzing it
*/
public class Parser {
private static TaskList taskList;

/**
* initializes the task list
*
Expand All @@ -33,30 +36,30 @@ public static void setTaskList(TaskList tasks) {
public static String parse(String input) throws DukeException {
String command = input.split("\\s")[0].toUpperCase();
String content = input.replace(input.split("\\s")[0], "");
switch(command){
case "BYE":
return parseBye(content);
case "CLEAR":
return parseClear(content);
case "DELETE":
return parseDelete(content);
case "MARK":
return parseMark(content);
case "UNMARK":
return parseUnmark(content);
case "FIND":
return parseFind(content);
case "PRINT":
return parsePrint(content);
case "TODO":
return parseTodo(content);
case "EVENT":
return parseEvent(content);
case "DEADLINE":
return parseDeadline(content);
default:
throw new DukeException("Sorry, I don't recognize this command. Please try again.");
}
switch(command) {
case "BYE":
return parseBye(content);
case "CLEAR":
return parseClear(content);
case "DELETE":
return parseDelete(content);
case "MARK":
return parseMark(content);
case "UNMARK":
return parseUnmark(content);
case "FIND":
return parseFind(content);
case "PRINT":
return parsePrint(content);
case "TODO":
return parseTodo(content);
case "EVENT":
return parseEvent(content);
case "DEADLINE":
return parseDeadline(content);
default:
throw new DukeException("Sorry, I don't recognize this command. Please try again.");
}
}

/**
Expand All @@ -71,7 +74,6 @@ private static String parseBye(String content) throws DukeException {
} else {
throw new DukeException("The clear command will clear all stored tasks, please try again.");
}

}

/**
Expand Down Expand Up @@ -208,23 +210,22 @@ private static String parseTodo(String content) throws DukeException {
*/
private static String parseEvent(String content) throws DukeException {
//when user didn't provide title and start & end time
if (content.isBlank() || content.isEmpty()
|| !content.contains(" /from ") || !content.contains(" /to ") || content == null) {
if (content.isBlank() || content.isEmpty()
|| !content.contains(" /from ") || !content.contains(" /to ") || content == null) {
throw new DukeException("Sorry, this event must have a title, start time, and end time.");
}
String[] event = new String[3];
try {
event[0] = content.substring(1, content.indexOf(" /"));
event[1] = content.substring(content.indexOf("/from") + 6,
event[1] = content.substring(content.indexOf("/from") + 6,
content.indexOf(" /to"));
event[2] = content.substring(content.indexOf("/to") + 4);
}
catch (StringIndexOutOfBoundsException e) {
} catch (StringIndexOutOfBoundsException e) {
return ("Sorry, this event must have a title, start time, and end time.");
}
//when user provide empty title
if (event[0].isBlank() || event[0].isEmpty() || event[0] == null) {
throw new DukeException("Sorry, the event must have a title.");
throw new DukeException("Sorry, the event must have a title.");
}
//when user didn't provide the starting time
if (event[1].isBlank() || event[1].isEmpty() || event[1] == null) {
Expand Down Expand Up @@ -257,8 +258,7 @@ private static String parseDeadline(String content) throws DukeException {
String[] ddl = new String[2];
try {
ddl = content.split(" /by ");
}
catch (ArrayIndexOutOfBoundsException e) {
} catch (ArrayIndexOutOfBoundsException e) {
return "Sorry, this deadline task must have a title and a deadline.";
}
//when user provide empty title
Expand Down Expand Up @@ -308,6 +308,12 @@ private static String formatTime(String input) {
}
}

/**
* dummy method for testing parse
*
* @param input the user input
* @return the detected command
*/
public static String parseTest(String input) throws DukeException {
String testCommand = input.split("\\s")[0].toUpperCase();
return testCommand;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/helper/Ui.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package duke.helper;

/**
* Ui class
*/
public class Ui {
private static final String NAME = "Duke Max";

Expand Down
18 changes: 10 additions & 8 deletions src/main/java/duke/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import duke.helper.Ui;
import duke.tasks.Task;

/**
* Storage class for import and export between txt file and TaskList.
*/
public class Storage {
private static String path;
private static ArrayList<Task> taskList = new ArrayList<Task>();
Expand All @@ -26,7 +29,7 @@ public class Storage {
public static void setPath(String filePath) {
path = filePath;
}

/**
* save the arraylist of tasks in txt format
*
Expand All @@ -35,7 +38,7 @@ public static void setPath(String filePath) {
public static void save(ArrayList<Task> tasks) {
try (PrintWriter printwriter = new PrintWriter(new FileWriter(path))) {
for (Task task : tasks) {
printwriter.write(task.toFile() +"\n");
printwriter.write(task.toFile() + "\n");
}
} catch (IOException e) {
System.out.println("There is an error saving this file: " + e.getMessage());
Expand All @@ -47,7 +50,7 @@ public static void save(ArrayList<Task> tasks) {
*
* @return an arraylist translated from the txt file
*/
public static ArrayList<Task> load() throws DukeException{
public static ArrayList<Task> load() throws DukeException {
handleMissing(path);

try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
Expand All @@ -69,21 +72,20 @@ public static ArrayList<Task> load() throws DukeException{
* @param testPath the file path to test if exist
*/
private static void handleMissing(String testPath) {
try{
try {
//if directory or path doesn't exist
Path directoryPath = Paths.get(".", "data");
Path directoryPath = Paths.get(".", "data");
if (!Files.exists(directoryPath)) {
Files.createDirectories(directoryPath);
}

Path path = directoryPath.resolve("tasks.txt");
Path path = directoryPath.resolve("tasks.txt");
if (!Files.exists(path)) {
Files.createFile(path);
Files.createFile(path);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("There is an error loading file: " + e.getMessage());
}
}

}
33 changes: 19 additions & 14 deletions src/main/java/duke/storage/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import duke.helper.Ui;
import duke.tasks.Task;

/**
* TskList that runs task-related commands and stores task in an arrayList.
*/
public class TaskList {
private static ArrayList<Task> taskList;

Expand Down Expand Up @@ -43,8 +46,8 @@ public String clear() {
* @return the bot response
*/
public String delete(int num) {
String[] messageList = {"I've removed this task:", taskList.get(num - 1).getStatus(),
"Current # of " + plural(taskList.size() - 1, "task") + ": " + (taskList.size() - 1)};
String taskNumber = "Current # of " + plural(taskList.size() - 1, "task") + ": " + (taskList.size() - 1);
String[] messageList = {"I've removed this task:", taskList.get(num - 1).getStatus(), taskNumber};
taskList.remove(num - 1);
Storage.save(taskList);
return Ui.print(messageList);
Expand Down Expand Up @@ -73,9 +76,9 @@ public String find(String keyword) {
int index = 1;
String message = "";
for (Task task: taskList) {
if(task.getTask().contains(keyword)) {
if (task.getTask().contains(keyword)) {
message = message + (index + ". " + task.getStatus()) + "\n";
index ++;
index++;
}
}
index -= 1;
Expand All @@ -97,7 +100,7 @@ public String print() {
String message = "";
for (Task task: taskList) {
message = message + (index + ". " + task.getStatus()) + "\n";
index ++;
index++;
}
message = message + ("Current # of " + plural(taskList.size(), "task") + ": " + taskList.size());
return Ui.print(message);
Expand All @@ -113,9 +116,9 @@ public String print(String time) {
int index = 1;
String message = "";
for (Task task: taskList) {
if(task.getTime() != null && task.getTime().contains(time)) {
if (task.getTime() != null && task.getTime().contains(time)) {
message = message + (index + ". " + task.getStatus()) + "\n";
index ++;
index++;
}
}
index -= 1;
Expand All @@ -136,17 +139,19 @@ public String print(String time) {
public String add(Task input) {
taskList.add(input);
Storage.save(taskList);
String[] messageList = {("Got it! This task has been added: "),
(input.getStatus()),
("Current # of " + plural(taskList.size(), "task") + ": " + taskList.size())};
String taskNumber = "Current # of " + plural(taskList.size(), "task") + ": " + taskList.size();
String[] messageList = {("Got it! This task has been added: "), (input.getStatus()), taskNumber};
return Ui.print(messageList);
}

/**
* dummy test method for add command
*
* @param input the task to add to the taskList
*/
public void addTest(Task input) {
taskList.add(input);
String[] messageList = {("Got it! This task has been added: "),
(input.getStatus()),
("Current # of " + plural(taskList.size(), "task") + ": " + taskList.size())};
String taskNumber = "Current # of " + plural(taskList.size(), "task") + ": " + taskList.size();
String[] messageList = {("Got it! This task has been added: "), (input.getStatus()), taskNumber};
Ui.print(messageList);
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package duke.tasks;

/**
* Deadline class with due time
*/
public class Deadline extends Task {
private String deadline;

Expand All @@ -13,7 +16,7 @@ public Deadline(String task, String deadline) {
super(task);
this.deadline = deadline;
}

/**
* returns the status of the deadline task
*
Expand All @@ -40,4 +43,4 @@ public String toFile() {
return super.isDone ? ("D | 1 | " + super.task + " | " + this.deadline)
: ("D | 0 | " + super.task + " | " + this.deadline);
}
}
}
Loading