-
Notifications
You must be signed in to change notification settings - Fork 454
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
[Tay Jun Yang] iP #500
base: master
Are you sure you want to change the base?
[Tay Jun Yang] iP #500
Changes from all commits
7d4a3b6
103b9a9
a48639a
5ae82e5
5c3a321
3f58e09
b71fe73
7c277bc
73492fc
0a46d99
474f703
0f100be
d89b0c1
942e26c
765d73c
483a811
4fa286f
377e1db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
D | 1 | CS2105 Assignment 0 | 2021-08-29 2359 | ||
D | 1 | PC1201 Test 1 (First Mock Test) | 2021-09-02 1400 | ||
D | 1 | CS2103 Week 4 Quiz | 2021-08-30 2359 | ||
D | 0 | CS3240 Week 4 Quiz | 2021-08-31 2000 | ||
T | 0 | Vacuum room floor | ||
T | 0 | Change bedsheet | ||
T | 0 | Download files from LumiNUS | ||
T | 0 | CS2103 Week 3 Deliverables (JUnit) | ||
T | 0 | CS2103 Week 3 Deliverables (Jar) | ||
T | 0 | CS2103 Week 3 Deliverables (Javadocs) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package duke; | ||
|
||
import duke.command.Command; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* The driver class that runs the program | ||
*/ | ||
public class Duke { | ||
private Ui ui; | ||
private Parser parser; | ||
private Storage storage; | ||
private TaskList taskList; | ||
|
||
/** | ||
* Initializes an instance of Duke class. | ||
* @param pathName Folder name of the storage file | ||
* @param fileName Name of the storage file | ||
*/ | ||
public Duke(String pathName, String fileName) { | ||
ui = new Ui(); | ||
parser = new Parser(); | ||
|
||
try { | ||
storage = new Storage(pathName, fileName); | ||
taskList = storage.loadTasksFromFile(); | ||
} catch (DukeException | IOException e) { | ||
ui.displayError(e.getMessage()); | ||
taskList = new TaskList(); | ||
} | ||
} | ||
|
||
/** | ||
* Starts the execution of the program. | ||
* @param args Command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
new Duke("data", "tasks.txt").run(); | ||
} | ||
|
||
/** | ||
* Executes the program. | ||
*/ | ||
public void run() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to add headers comments for public methods. |
||
ui.displayWelcome(); | ||
boolean isExit = false; | ||
|
||
while (!isExit) { | ||
try { | ||
String commandLine = ui.readCommand(); | ||
Command command = parser.parse(commandLine); | ||
command.execute(taskList, ui, storage); | ||
isExit = command.isExit(); | ||
} catch (DukeException e) { | ||
ui.displayError(e.getMessage()); | ||
} finally { | ||
ui.displayLine(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package duke; | ||
|
||
/** | ||
* A class to handle any exceptions during the program execution | ||
*/ | ||
public class DukeException extends Exception { | ||
/** | ||
* Initializes and instance of DukeException class. | ||
* @param errorMessage The error message. | ||
*/ | ||
public DukeException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,294 @@ | ||
package duke; | ||
|
||
import duke.command.AddDeadlineCommand; | ||
import duke.command.AddEventCommand; | ||
import duke.command.AddToDoCommand; | ||
import duke.command.Command; | ||
import duke.command.DeleteCommand; | ||
import duke.command.DoneCommand; | ||
import duke.command.ExitCommand; | ||
import duke.command.ListCommand; | ||
import duke.command.PrintCommand; | ||
|
||
/** | ||
* A class that contaisn methods to parse command line strings | ||
*/ | ||
public class Parser { | ||
/** | ||
* Parses a command line string input on screen to get the details | ||
* of the Command object | ||
* @param commandLine The command line string to parse | ||
* @return The Command object | ||
* @throws DukeException If the command line string is not valid | ||
*/ | ||
public Command parse(String commandLine) throws DukeException { | ||
String[] commandLineParts = commandLine.split("\\s+", 2); | ||
|
||
String commandType; | ||
String commandInfo; | ||
|
||
if (commandLineParts.length == 2) { | ||
commandType = commandLineParts[0]; | ||
commandInfo = commandLineParts[1]; | ||
|
||
// If user inputs extra text after "bye" or "list" commands | ||
if ((commandType.equals("bye") || commandType.equals("list")) && !commandInfo.equals("")) { | ||
throw new DukeException("INVALID COMMAND. Please try again!"); | ||
} | ||
} else { // for "bye" and "list" commands | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments should be indented relative to their position in the code. |
||
commandType = commandLineParts[0]; | ||
commandInfo = ""; | ||
} | ||
|
||
Command command; | ||
int taskNum; | ||
String taskDescription; | ||
String taskInfo; | ||
String date; | ||
|
||
switch (commandType) { | ||
case "bye": | ||
command = new ExitCommand(commandType); | ||
break; | ||
case "list": | ||
command = new ListCommand(commandType); | ||
break; | ||
case "done": | ||
taskNum = getTaskNumFromCommand(commandInfo); | ||
command = new DoneCommand(commandType, taskNum); | ||
break; | ||
case "delete": | ||
taskNum = getTaskNumFromCommand(commandInfo); | ||
command = new DeleteCommand(commandType, taskNum); | ||
break; | ||
case "todo": | ||
taskDescription = getTaskDescriptionFromToDoCommand(commandInfo); | ||
command = new AddToDoCommand(commandType, taskDescription); | ||
break; | ||
case "deadline": | ||
taskInfo = getTaskInfoFromDeadlineCommand(commandInfo); | ||
command = new AddDeadlineCommand(commandType, taskInfo); | ||
break; | ||
case "event": | ||
taskInfo = getTaskInfoFromEventCommand(commandInfo); | ||
command = new AddEventCommand(commandType, taskInfo); | ||
break; | ||
case "print": | ||
date = getDateFromPrintCommand(commandInfo); | ||
command = new PrintCommand(commandType, date); | ||
break; | ||
default: | ||
throw new DukeException("INVALID COMMAND. Please try again!"); | ||
} | ||
|
||
return command; | ||
} | ||
|
||
/** | ||
* Gets task number from the command information. | ||
* @param commandInfo The command information | ||
* @return The task number | ||
* @throws DukeException If the information is not valid | ||
*/ | ||
private int getTaskNumFromCommand(String commandInfo) throws DukeException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps a shorter name would be better e.g. getTaskNum. |
||
if (commandInfo.trim().length() > 0) { | ||
try { | ||
return Integer.parseInt(commandInfo); | ||
} catch (NumberFormatException e) { | ||
throw new DukeException("Please enter a valid task number to be marked as done!"); | ||
} | ||
} else { | ||
throw new DukeException("Please enter a task number to be marked as done!"); | ||
} | ||
} | ||
|
||
/** | ||
* Gets task description from ToDo command information. | ||
* @param commandInfo The ToDo command information | ||
* @return The task description | ||
* @throws DukeException If the information is not valid | ||
*/ | ||
private String getTaskDescriptionFromToDoCommand(String commandInfo) throws DukeException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above, method names could be shorter and more concise, and if they are specific to a command, perhaps it could be a better design to have them in the respective command classes. This applies to other method names as well. |
||
if (commandInfo.trim().length() > 0) { | ||
return commandInfo; | ||
} else { | ||
throw new DukeException("The description of a todo cannot be empty!"); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param commandInfo | ||
* @return | ||
* @throws DukeException | ||
*/ | ||
private String getTaskInfoFromDeadlineCommand(String commandInfo) throws DukeException { | ||
if (commandInfo.trim().length() > 0) { | ||
String[] deadlineTaskDetails = commandInfo.split("/", 2); | ||
|
||
if (deadlineTaskDetails.length == 2) { | ||
if (deadlineTaskDetails[0].trim().length() > 0) { | ||
if (deadlineTaskDetails[1].trim().startsWith("by")) { | ||
String beforeDateTime = deadlineTaskDetails[1].trim(); | ||
String[] beforeDateTimeParts = beforeDateTime.split("\\s+", 2); | ||
|
||
if (beforeDateTimeParts.length == 2) { | ||
return commandInfo; | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The date/time of a deadline cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /by before specifying the date/time]"); | ||
} | ||
} else { | ||
throw new DukeException("WRONG COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "Enter /by before specifying the date!"); | ||
} | ||
} else { | ||
if (deadlineTaskDetails[1].trim().startsWith("by")) { | ||
String beforeDateTime = deadlineTaskDetails[1].trim(); | ||
String[] beforeDateTimeParts = beforeDateTime.split("\\s+", 2); | ||
|
||
if (beforeDateTimeParts.length == 2) { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description of a deadline cannot be empty!"); | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description of a deadline cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "The date/time of a deadline cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /by before specifying the date/time]"); | ||
} | ||
} else { | ||
throw new DukeException("INCOMPLETE & WRONG COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description of a deadline cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "Enter /by before specifying the date/time!"); | ||
} | ||
} | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The date/time of a deadline cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /by before specifying the date/time]"); | ||
} | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description and date/time of a deadline cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /by before specifying the date/time]"); | ||
} | ||
} | ||
Comment on lines
+129
to
+189
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid deep nesting, especially if you need more than 3 levels of indentation. In particular, avoid arrowhead style code. |
||
|
||
/** | ||
* | ||
* @param commandInfo | ||
* @return | ||
* @throws DukeException | ||
*/ | ||
private String getTaskInfoFromEventCommand(String commandInfo) throws DukeException { | ||
if (commandInfo.trim().length() > 0) { | ||
String[] eventTaskDetails = commandInfo.split("/", 2); | ||
|
||
if (eventTaskDetails.length == 2) { | ||
if (eventTaskDetails[0].trim().length() > 0) { | ||
if (eventTaskDetails[1].trim().startsWith("at")) { | ||
String startEndDateTime = eventTaskDetails[1].trim(); | ||
String[] startEndDateTimeParts = startEndDateTime.split("\\s+", 2); | ||
|
||
if (startEndDateTimeParts.length == 2) { | ||
return commandInfo; | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The date/time of an event cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /at before specifying the date/time]"); | ||
} | ||
} else { | ||
throw new DukeException("WRONG COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "Enter /at before specifying the date!"); | ||
} | ||
} else { | ||
if (eventTaskDetails[1].trim().startsWith("at")) { | ||
String startEndDateTime = eventTaskDetails[1].trim(); | ||
String[] startEndDateTimeParts = startEndDateTime.split("\\s+", 2); | ||
|
||
if (startEndDateTimeParts.length == 2) { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description of an event cannot be empty!"); | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description of an event cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "The date/time of an event cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /at before specifying the date/time]"); | ||
} | ||
} else { | ||
throw new DukeException("INCOMPLETE & WRONG COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description of an event cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "Enter /at before specifying the date/time!"); | ||
} | ||
} | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The date/time of an event cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /at before specifying the date/time]"); | ||
} | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The description and date/time of an event cannot be empty!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /at before specifying the date/time]"); | ||
} | ||
Comment on lines
+198
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as comment above, avoid deep nesting. |
||
} | ||
|
||
/** | ||
* | ||
* @param commandInfo | ||
* @return | ||
* @throws DukeException | ||
*/ | ||
private String getDateFromPrintCommand(String commandInfo) throws DukeException { | ||
if (commandInfo.trim().length() > 0) { | ||
if (commandInfo.trim().startsWith("/on")) { | ||
String[] specificDateParts = commandInfo.split("\\s+", 2); | ||
|
||
if (specificDateParts.length == 2) { | ||
return specificDateParts[1]; | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "The date is not specified!" | ||
+ System.lineSeparator() + "\t" | ||
+ "[Note: Enter /on before specifying the date]"); | ||
} | ||
} else { | ||
throw new DukeException("WRONG COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "Enter /on before specifying the date!"); | ||
} | ||
} else { | ||
throw new DukeException("INCOMPLETE COMMAND" | ||
+ System.lineSeparator() + "\t" | ||
+ "Enter /on before specifying the date!"); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add header comment for the class.
Noticed that there is no JavaDocs added throughout. Perhaps it would be good practice to have them, as documentation to improve readability of your code.