Skip to content

Commit

Permalink
Fix all the Homework Bugs Found
Browse files Browse the repository at this point in the history
  • Loading branch information
Yufannnn committed Mar 29, 2023
1 parent b96cdb1 commit 7918497
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dependencies {
}

shadowJar {
archiveFileName = 'addressbook.jar'
archiveFileName = 'TutorPro.jar'
}

defaultTasks 'clean', 'test'
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,18 @@ public CreateHomeworkCommand parse(String args) throws ParseException {
for (int i = 0; i < nameKeywords.size(); i++) {
String name = nameKeywords.get(i);
name = name.trim();
// int spaceIndex = name.indexOf(" ");
// if (spaceIndex != -1) {
// name = name.substring(0, spaceIndex);
// }
nameKeywords.set(i, name);
}
names = nameKeywords;

if (nameKeywords.size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one name is allowed for mark homework as done command."));
"Only one student name is allowed."));
}
// name cannot be an empty string
if (nameKeywords.get(0).isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Name cannot be empty."));
}

return new CreateHomeworkCommand(names, new NamePredicate(nameKeywords),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,27 @@ public DeleteHomeworkCommand parse(String args) throws ParseException {

if (nameKeywords.size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one name is allowed for mark homework as done command."));
"Only one student name is allowed."));
}

// name cannot be an empty string
if (nameKeywords.get(0).isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Name cannot be empty."));
}

// there should also be one index keyword
if (argMultimap.getAllValues(PREFIX_INDEX).size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one index is allowed for mark homework as done command."));
}

// index cannot be an empty string
if (argMultimap.getValue(PREFIX_INDEX).get().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Index cannot be empty."));
}
Index index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_INDEX).get());

return new DeleteHomeworkCommand(names, new NamePredicate(nameKeywords), index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,30 @@ public MarkHomeworkAsDoneCommand parse(String args) throws ParseException {
for (int i = 0; i < nameKeywords.size(); i++) {
String name = nameKeywords.get(i);
name = name.trim();
// int spaceIndex = name.indexOf(" ");
// if (spaceIndex != -1) {
// name = name.substring(0, spaceIndex);
// }
nameKeywords.set(i, name);
}
names = nameKeywords;

if (nameKeywords.size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one name is allowed for mark homework as done command."));
"Only one student name is allowed."));
}
// name cannot be an empty string
if (nameKeywords.get(0).isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Name cannot be empty."));
}

// there should also be one index keyword
if (argMultimap.getAllValues(PREFIX_INDEX).size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one index is allowed for mark homework as done command."));
}

// index cannot be an empty string
if (argMultimap.getValue(PREFIX_INDEX).get().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Index cannot be empty."));
}
Index index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_INDEX).get());

return new MarkHomeworkAsDoneCommand(names, new NamePredicate(nameKeywords), index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,30 @@ public MarkHomeworkAsUndoCommand parse(String args) throws ParseException {
for (int i = 0; i < nameKeywords.size(); i++) {
String name = nameKeywords.get(i);
name = name.trim();
int spaceIndex = name.indexOf(" ");
// if (spaceIndex != -1) {
// name = name.substring(0, spaceIndex);
// }
nameKeywords.set(i, name);
}
names = nameKeywords;

if (nameKeywords.size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one name is allowed for mark homework as Undone command."));
"Only one student name is allowed."));
}
// name cannot be an empty string
if (nameKeywords.get(0).isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Name cannot be empty."));
}

// there can only be one index, if there are more than one then throw an exception
if (argMultimap.getAllValues(PREFIX_INDEX).size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one index is allowed for mark homework as undone command."));
}

// index cannot be an empty string
if (argMultimap.getValue(PREFIX_INDEX).get().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Index cannot be empty."));
}
Index index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_INDEX).get());

return new MarkHomeworkAsUndoCommand(names, new NamePredicate(nameKeywords), index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,50 @@ public UpdateHomeworkCommand parse(String args) throws ParseException {
for (int i = 0; i < nameKeywords.size(); i++) {
String name = nameKeywords.get(i);
name = name.trim();
// int spaceIndex = name.indexOf(" ");
// if (spaceIndex != -1) {
// name = name.substring(0, spaceIndex);
// }
nameKeywords.set(i, name);
}
names = nameKeywords;

if (nameKeywords.size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one name is allowed for update homework command."));
"Only one student one be updated at a time."));
}
// name cannot be an empty string
if (nameKeywords.get(0).isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Name cannot be empty."));
}

if (argMultimap.getAllValues(PREFIX_INDEX).size() > 1) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Only one index is allowed for update homework command."));
}
// index cannot be an empty string
if (argMultimap.getValue(PREFIX_INDEX).get().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Index cannot be empty."));
}

// if homework name is not present, set it to null, else parse it
Optional<String> homeworkName = Optional.empty();
if (argMultimap.getValue(PREFIX_HOMEWORK).isPresent()) {
homeworkName = Optional.of(argMultimap.getValue(PREFIX_HOMEWORK).get());
// homework name cannot be an empty string
if (homeworkName.get().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Homework name cannot be empty."));
}
}

// if deadline is not present, set it to null, else parse it
Optional<LocalDateTime> deadline = Optional.empty();
if (argMultimap.getValue(PREFIX_DEADLINE).isPresent()) {
deadline = Optional.of(ParserUtil.parseDeadline(argMultimap.getValue(PREFIX_DEADLINE).get()));
// deadline cannot be an empty string
if (argMultimap.getValue(PREFIX_DEADLINE).get().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
"Deadline cannot be empty."));
}

// deadline must be in the future
if (deadline.get().isBefore(LocalDateTime.now())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
*/
public class ViewHomeworkCommandParser implements Parser<ViewHomeworkCommand> {
private List<String> names = new ArrayList<>();

/**
* Checks if the list of strings contains an empty string.
*
* @param list the list of strings to be checked.
* @return true if the list does not contain an empty string, false otherwise.
*/
private boolean checkEmptyString(List<String> list) {
for (String s : list) {
if (s.isEmpty()) {
return false;
}
}
return true;
}

/**
* Parses the given {@code String} of arguments in the context of the ViewHomeworkCommand
* and returns a ViewHomeworkCommand object for execution.
Expand Down Expand Up @@ -53,14 +69,15 @@ public ViewHomeworkCommand parse(String args) throws ParseException {
for (int i = 0; i < nameKeywords.size(); i++) {
String name = nameKeywords.get(i);
name = name.trim();
// int spaceIndex = name.indexOf(" ");
// if (spaceIndex != -1) {
// name = name.substring(0, spaceIndex);
// }
nameKeywords.set(i, name);
}
names = nameKeywords;

// it cannot be an empty string
if (!checkEmptyString(nameKeywords)) {
throw new ParseException("Name cannot be empty.");
}

namePredicate = new NamePredicate(nameKeywords);
defaultPredicateFlag = false;
} else {
Expand All @@ -70,6 +87,15 @@ public ViewHomeworkCommand parse(String args) throws ParseException {

// If status is present, create a predicate to filter by status
if (argMultimap.getValue(PREFIX_STATUS).isPresent()) {
// there can only be one status prefix
if (argMultimap.getAllValues(PREFIX_STATUS).size() > 1) {
throw new ParseException("Only one status prefix allowed.");
}
// it cannot be an empty string
if (argMultimap.getValue(PREFIX_STATUS).get().isEmpty()) {
throw new ParseException("Status cannot be empty.");
}

String status = argMultimap.getValue(PREFIX_STATUS).get();
boolean isCompleted = ParserUtil.parseStatus(status);
HomeworkIsCompletePredicate statusPredicate = new HomeworkIsCompletePredicate(isCompleted);
Expand Down

0 comments on commit 7918497

Please sign in to comment.