Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#70 from successs404/branch-del…
Browse files Browse the repository at this point in the history
…ete-grp

Branch delete grp
  • Loading branch information
chunyongg authored Oct 12, 2020
2 parents 9ed8bfc + a4693b9 commit 37e1da9
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 4 deletions.
58 changes: 58 additions & 0 deletions src/main/java/seedu/address/logic/commands/DelGrpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;

import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.group.Group;
import seedu.address.model.group.GrpContainsKeywordPredicate;

public class DelGrpCommand extends Command {

public static final String COMMAND_WORD = "delgrp";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes an existing tutorial group. "
+ "Parameter: "
+ PREFIX_GRP + "GRP \n"
+ "Example: " + COMMAND_WORD + " " + PREFIX_GRP + "G04";

public static final String MESSAGE_DELETE_GROUP_SUCCESS = "Tutorial group deleted: %1$s";

private final GrpContainsKeywordPredicate grpPredicate;

/**
* Creates a DelGrpCommand to add the specified {@code Group}
*/
public DelGrpCommand(GrpContainsKeywordPredicate grpPredicate) {
this.grpPredicate = grpPredicate;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

Group toDel = new Group();

boolean hasGroup = false;

if (!model.getSerenity().getGroupList().isEmpty()) {
for (Group group : model.getSerenity().getGroupList()) {
if (group.getName().equals(grpPredicate.getKeyword())) {
toDel = group;
hasGroup = true;
break;
}
}
}

if (!hasGroup) {
throw new CommandException(Messages.MESSAGE_GROUP_EMPTY);
}

model.deleteGroup(toDel);
model.updateFilteredGroupList(grpPredicate);
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_SUCCESS, toDel));
}
}
45 changes: 45 additions & 0 deletions src/main/java/seedu/address/logic/parser/DelGrpCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;

import java.util.stream.Stream;

import seedu.address.logic.commands.DelGrpCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.group.GrpContainsKeywordPredicate;

/**
* Parses input arguments and creates a new AddGrpCommand object
*/
public class DelGrpCommandParser implements Parser<DelGrpCommand> {

private final ParseException delGrpCommandParserException = new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DelGrpCommand.MESSAGE_USAGE));

@Override
public DelGrpCommand parse(String args) throws ParseException {

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GRP);

if (!arePrefixesPresent(argMultimap, PREFIX_GRP) || !argMultimap.getPreamble().isEmpty()) {
throw delGrpCommandParserException;
}

String[] grpKeyword = argMultimap.getValue(PREFIX_GRP).get().split("\\s+");

if (grpKeyword.length > 1) {
throw delGrpCommandParserException;
}

return new DelGrpCommand(new GrpContainsKeywordPredicate(grpKeyword[0]));
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given {@code
* ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/SerenityParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.logic.commands.AddGrpCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DelGrpCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
Expand Down Expand Up @@ -55,6 +56,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddGrpCommand.COMMAND_WORD:
return new AddGrpCommandParser().parse(arguments);

case DelGrpCommand.COMMAND_WORD:
return new DelGrpCommandParser().parse(arguments);

case ViewGrpCommand.COMMAND_WORD:
return new ViewGrpCommandParser().parse(arguments);

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public interface Model {
*/
boolean hasGroup(Group group);

/**
* Deletes the given group. The group must exist in serenity.
*/
void deleteGroup(Group target);

/**
* Adds the given group. {@code group} must not already exist in serenity.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ public boolean hasGroup(Group group) {
return serenity.hasGroup(group);
}

@Override
public void deleteGroup(Group target) {
serenity.removeGroup(target);
filteredGroups.clear();
students.clear();
lessons.clear();
filteredLessons.clear();
studentsInfo.clear();
}

@Override
public void addGroup(Group group) {
requireNonNull(group);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/seedu/address/model/group/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class Group {
private UniqueStudentList students;
private UniqueLessonList lessons;

// Empty constructor
public Group() {}

/**
* Constructs a {@code Group}
*
Expand Down Expand Up @@ -61,14 +64,14 @@ public Group(String name, UniqueStudentList students) {
*
* @param name A valid name.
* @param students A list of students.
* @param lessons A list of tutorial classes.
* @param lessons A list of tutorial lessons.
*/

public Group(String name, UniqueStudentList students, UniqueLessonList classes) {
requireAllNonNull(name, students, classes);
public Group(String name, UniqueStudentList students, UniqueLessonList lessons) {
requireAllNonNull(name, students, lessons);
this.name = name;
this.students = students;
this.lessons = lessons;
this.lessons = this.lessons;
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public GrpContainsKeywordPredicate(String string) {
this.keyword = string;
}

public String getKeyword() {
return keyword;
}

@Override
public boolean test(Group group) {
return StringUtil.containsWordIgnoreCase(group.getName(), keyword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public boolean hasGroup(Group group) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deleteGroup(Group target) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addGroup(Group group) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ public boolean hasGroup(Group group) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deleteGroup(Group target) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addGroup(Group group) {
throw new AssertionError("This method should not be called.");
Expand Down

0 comments on commit 37e1da9

Please sign in to comment.