Skip to content

Commit

Permalink
Add Java documentation for some classes.
Browse files Browse the repository at this point in the history
These classes are:
	Parser
	UserInputChecker
	CmdType
	Task
	ToDo
	Deadline
	Event
	WessyException
	CommandNotFoundException
	MissingSpacingException
	TimeSpecifierException
	UnspecifiedTimeException
	EmptyListException
	InvalidIntegerException
	NotAnIntegerException
	NotPositiveIntegerException
	MissingInputException
	TooManyInputException
  • Loading branch information
DerenC committed Feb 12, 2023
1 parent 88ec341 commit 8707b3e
Show file tree
Hide file tree
Showing 24 changed files with 775 additions and 92 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0'

}

test {
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/wessy/CmdType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.util.Map;
import java.util.HashMap;
import java.util.Set;

/**
* CmdType is an enumeration that represents all the different types of commands
* "Wessy" takes in.
*/
public enum CmdType {
LIST("list"),
BYE("bye"),
Expand All @@ -17,11 +20,6 @@ public enum CmdType {

private final String cmd;
private static final Map<String, CmdType> COMMANDS = new HashMap<>();

CmdType(String str) {
this.cmd = str;
}

static {
COMMANDS.put("bye", CmdType.BYE);
COMMANDS.put("list", CmdType.LIST);
Expand All @@ -34,19 +32,39 @@ public enum CmdType {
COMMANDS.put("clear", CmdType.CLEAR);
}

public static CmdType getCmdType(String str) {
return COMMANDS.get(str);
/** Constructs an instance of CmdType that corresponds to the different
* types of commands, along with the command in its text form.
*/
CmdType(String str) {
this.cmd = str;
}

public static Set<String> getKeys() {
return COMMANDS.keySet();
/**
* Converts a command (if valid) from its text form to the corresponding
* CmdType.
*
* @param str Specified command in its text form.
* @return A CmdType that corresponds to str.
*/
public static CmdType getCmdType(String str) {
return COMMANDS.get(str);
}

/**
* Converts CmdType to command in its text form.
*
* @return The text form of the command when it is of CmdType type.
*/
@Override
public String toString() {
return cmd;
}

/**
* Gets the length of the string when the command is in its text form.
*
* @return The length of string when the command is in its text form.
*/
public int len() {
return cmd.length();
}
Expand Down
78 changes: 72 additions & 6 deletions src/main/java/wessy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

/**
* Parser is a utility class that mainly processes the user input when "Wessy"
* first receives it. While it checks for some formats of the String, it
* delegates checking of the other formatting issues to UserInputChecker class.
*/
public class Parser {
/**
* Converts the first word in the line of user input, to the corresponding
* CmdType, if it is a valid command type.
*
* @param userInput The line which the user inputs.
* @return The corresponding CmdType, if the first word is a valid command.
*/
static CmdType getCmd(String userInput) {
int idx = userInput.indexOf(' ');
if (idx == -1) {
Expand All @@ -13,7 +25,18 @@ static CmdType getCmd(String userInput) {
return CmdType.getCmdType(userInput.substring(0, idx));
}

// Only for Deadline, Event & ToDo commands
/**
* Parses the line of user input and chunks it into the different components
* required to initialise a ToDo, Deadline or Event object. The components
* are possibly the task description, and the timings specified for the
* deadline task or the event.
*
* @param userInput The line which the user inputs.
* @param cmd The command specified, in the form of CmdType. It only accepts
* CmdType.TODO, CmdType.DEADLINE and CmdType.EVENT.
* @return An array of Strings consisting of the components required to
* initialise a task.
*/
static String[] getTaskComponents(String userInput, CmdType cmd) {
String byStr = "/by";
String fromStr = "/from";
Expand All @@ -38,7 +61,17 @@ static String[] getTaskComponents(String userInput, CmdType cmd) {
return new String[0];
}

public static LocalDateTime parseDateTime(String str) throws DateTimeParseException {
/**
* Parses the input str and converts it into a LocalDateTime object.
*
* @param str The specified date and time as a String.
* @return A LocalDateTime object that is represented by str, in its String
* form.
* @throws DateTimeParseException If the format of str is wrong and thus
* cannot be parsed into a LocalDateTime object.
*/
public static LocalDateTime parseDateTime(String str) throws
DateTimeParseException {
str = removeSpacePadding(str);
if (count(str, ':') == 2) {
return LocalDateTime.parse(str);
Expand All @@ -62,6 +95,14 @@ public static LocalDateTime parseDateTime(String str) throws DateTimeParseExcept
return LocalDateTime.parse(parseDate(str.substring(0, idx)) + "T" + str.substring(idx + 1, idx + 3) + ":" + str.substring(idx + 3) + ":00");
}

/**
* Counts the number of occurrence "target" appears in str.
*
* @param str The String we scan through while counting the number of
* occurrence.
* @param target The character we look out for when scanning through str.
* @return The number of occurrence "target" appears in str.
*/
static int count(String str, char target) {
int num = 0;
for (int i = 0; i < str.length(); i++) {
Expand All @@ -72,7 +113,15 @@ static int count(String str, char target) {
return num;
}

// Used in parseDateTime
/**
* Standardises format of date and time in str, by say making sure that the
* separator in the date is "-" instead of "/", making sure the date has 8
* digits. This is to prepare for the execution of parseDateTime(...).
*
* @param str The specified date and time in String form.
* @return A standardised String format of the specified date and time.
* @throws DateTimeParseException If str does not represent any date and time.
*/
static String parseDate(String str) throws DateTimeParseException {
try {
String[] components = str.split("-", 3);
Expand All @@ -93,15 +142,32 @@ static String parseDate(String str) throws DateTimeParseException {
}
}

public static int parseInt(String userInput, CmdType cmd) throws NotPositiveIntegerException {
int num = Integer.parseInt(removeSpacePadding(userInput.substring(cmd.len())));
/**
* Removes the command word from the user input and then the space paddings
* on the 2 sides. Afterwards, converts the processed String into a integer
* using Integer.parseInt(...).
*
* @param userInput The String to be parsed to an integer.
* @param cmd The specified command.
* @return The integer to which the String is converted.
* @throws NotPositiveIntegerException If the output integer is not positive.
*/
public static int parseInt(String userInput, CmdType cmd) throws
NotPositiveIntegerException {
int num = Integer.parseInt(removeSpacePadding(userInput.substring(
cmd.len())));
if (num <= 0) {
throw new NotPositiveIntegerException();
}
return num;
}

// HELPER FUNC
/**
* A helper function that removes the space paddings on the two ends of str.
*
* @param str The String to be processed.
* @return The shorter String after removing the space paddings on the two ends.
*/
public static String removeSpacePadding(String str) {
int start = 0;
while (str.charAt(start) == ' ') {
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/wessy/Storage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package wessy;

import wessy.Parser;
import wessy.task.Deadline;
import wessy.task.Event;
import wessy.task.ToDo;
Expand All @@ -15,10 +14,18 @@
import java.nio.file.Path;
import java.nio.file.Paths;

/**
*
*/
public class Storage {
private final String folderPath;
private final String fileName;

/**
* Constructs an instance of Storage.
*
* @param filePath
*/
public Storage(String filePath) {
int lastIdx = filePath.lastIndexOf('/');
if (lastIdx == -1) {
Expand All @@ -30,14 +37,29 @@ public Storage(String filePath) {
}
}

/**
* Constructs an instance of Storage.
*/
public Storage() {
this("data/savedTasks.txt");
}

/**
*
*
* @return
*/
String getFullPath() {
return folderPath + "/" + fileName;
}

/**
*
*
* @return
* @throws SecurityException
* @throws IOException
*/
List<Task> load() throws SecurityException, IOException {
List<Task> tasks = new ArrayList<Task>();
File savedFile = new File(folderPath + "/" + fileName);
Expand All @@ -62,6 +84,14 @@ List<Task> load() throws SecurityException, IOException {
return tasks;
}

/**
*
*
* @param tasksAsStr
* @return
* @throws IOException
* @throws SecurityException
*/
void save(String tasksAsStr) throws IOException, SecurityException {
// CREATE FOLDERS
Path path = Paths.get(folderPath);
Expand Down

0 comments on commit 8707b3e

Please sign in to comment.