Skip to content

Commit 37e1da9

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#70 from successs404/branch-delete-grp
Branch delete grp
2 parents 9ed8bfc + a4693b9 commit 37e1da9

File tree

9 files changed

+143
-4
lines changed

9 files changed

+143
-4
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
5+
6+
import seedu.address.commons.core.Messages;
7+
import seedu.address.logic.commands.exceptions.CommandException;
8+
import seedu.address.model.Model;
9+
import seedu.address.model.group.Group;
10+
import seedu.address.model.group.GrpContainsKeywordPredicate;
11+
12+
public class DelGrpCommand extends Command {
13+
14+
public static final String COMMAND_WORD = "delgrp";
15+
public static final String MESSAGE_USAGE = COMMAND_WORD
16+
+ ": Deletes an existing tutorial group. "
17+
+ "Parameter: "
18+
+ PREFIX_GRP + "GRP \n"
19+
+ "Example: " + COMMAND_WORD + " " + PREFIX_GRP + "G04";
20+
21+
public static final String MESSAGE_DELETE_GROUP_SUCCESS = "Tutorial group deleted: %1$s";
22+
23+
private final GrpContainsKeywordPredicate grpPredicate;
24+
25+
/**
26+
* Creates a DelGrpCommand to add the specified {@code Group}
27+
*/
28+
public DelGrpCommand(GrpContainsKeywordPredicate grpPredicate) {
29+
this.grpPredicate = grpPredicate;
30+
}
31+
32+
@Override
33+
public CommandResult execute(Model model) throws CommandException {
34+
requireNonNull(model);
35+
36+
Group toDel = new Group();
37+
38+
boolean hasGroup = false;
39+
40+
if (!model.getSerenity().getGroupList().isEmpty()) {
41+
for (Group group : model.getSerenity().getGroupList()) {
42+
if (group.getName().equals(grpPredicate.getKeyword())) {
43+
toDel = group;
44+
hasGroup = true;
45+
break;
46+
}
47+
}
48+
}
49+
50+
if (!hasGroup) {
51+
throw new CommandException(Messages.MESSAGE_GROUP_EMPTY);
52+
}
53+
54+
model.deleteGroup(toDel);
55+
model.updateFilteredGroupList(grpPredicate);
56+
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_SUCCESS, toDel));
57+
}
58+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
5+
6+
import java.util.stream.Stream;
7+
8+
import seedu.address.logic.commands.DelGrpCommand;
9+
import seedu.address.logic.parser.exceptions.ParseException;
10+
import seedu.address.model.group.GrpContainsKeywordPredicate;
11+
12+
/**
13+
* Parses input arguments and creates a new AddGrpCommand object
14+
*/
15+
public class DelGrpCommandParser implements Parser<DelGrpCommand> {
16+
17+
private final ParseException delGrpCommandParserException = new ParseException(
18+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DelGrpCommand.MESSAGE_USAGE));
19+
20+
@Override
21+
public DelGrpCommand parse(String args) throws ParseException {
22+
23+
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GRP);
24+
25+
if (!arePrefixesPresent(argMultimap, PREFIX_GRP) || !argMultimap.getPreamble().isEmpty()) {
26+
throw delGrpCommandParserException;
27+
}
28+
29+
String[] grpKeyword = argMultimap.getValue(PREFIX_GRP).get().split("\\s+");
30+
31+
if (grpKeyword.length > 1) {
32+
throw delGrpCommandParserException;
33+
}
34+
35+
return new DelGrpCommand(new GrpContainsKeywordPredicate(grpKeyword[0]));
36+
}
37+
38+
/**
39+
* Returns true if none of the prefixes contains empty {@code Optional} values in the given {@code
40+
* ArgumentMultimap}.
41+
*/
42+
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
43+
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
44+
}
45+
}

src/main/java/seedu/address/logic/parser/SerenityParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import seedu.address.logic.commands.AddGrpCommand;
1111
import seedu.address.logic.commands.ClearCommand;
1212
import seedu.address.logic.commands.Command;
13+
import seedu.address.logic.commands.DelGrpCommand;
1314
import seedu.address.logic.commands.DeleteCommand;
1415
import seedu.address.logic.commands.EditCommand;
1516
import seedu.address.logic.commands.ExitCommand;
@@ -55,6 +56,9 @@ public Command parseCommand(String userInput) throws ParseException {
5556
case AddGrpCommand.COMMAND_WORD:
5657
return new AddGrpCommandParser().parse(arguments);
5758

59+
case DelGrpCommand.COMMAND_WORD:
60+
return new DelGrpCommandParser().parse(arguments);
61+
5862
case ViewGrpCommand.COMMAND_WORD:
5963
return new ViewGrpCommandParser().parse(arguments);
6064

src/main/java/seedu/address/model/Model.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public interface Model {
123123
*/
124124
boolean hasGroup(Group group);
125125

126+
/**
127+
* Deletes the given group. The group must exist in serenity.
128+
*/
129+
void deleteGroup(Group target);
130+
126131
/**
127132
* Adds the given group. {@code group} must not already exist in serenity.
128133
*/

src/main/java/seedu/address/model/ModelManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ public boolean hasGroup(Group group) {
186186
return serenity.hasGroup(group);
187187
}
188188

189+
@Override
190+
public void deleteGroup(Group target) {
191+
serenity.removeGroup(target);
192+
filteredGroups.clear();
193+
students.clear();
194+
lessons.clear();
195+
filteredLessons.clear();
196+
studentsInfo.clear();
197+
}
198+
189199
@Override
190200
public void addGroup(Group group) {
191201
requireNonNull(group);

src/main/java/seedu/address/model/group/Group.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class Group {
2525
private UniqueStudentList students;
2626
private UniqueLessonList lessons;
2727

28+
// Empty constructor
29+
public Group() {}
30+
2831
/**
2932
* Constructs a {@code Group}
3033
*
@@ -61,14 +64,14 @@ public Group(String name, UniqueStudentList students) {
6164
*
6265
* @param name A valid name.
6366
* @param students A list of students.
64-
* @param lessons A list of tutorial classes.
67+
* @param lessons A list of tutorial lessons.
6568
*/
6669

67-
public Group(String name, UniqueStudentList students, UniqueLessonList classes) {
68-
requireAllNonNull(name, students, classes);
70+
public Group(String name, UniqueStudentList students, UniqueLessonList lessons) {
71+
requireAllNonNull(name, students, lessons);
6972
this.name = name;
7073
this.students = students;
71-
this.lessons = lessons;
74+
this.lessons = this.lessons;
7275
}
7376

7477
public String getName() {

src/main/java/seedu/address/model/group/GrpContainsKeywordPredicate.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public GrpContainsKeywordPredicate(String string) {
1515
this.keyword = string;
1616
}
1717

18+
public String getKeyword() {
19+
return keyword;
20+
}
21+
1822
@Override
1923
public boolean test(Group group) {
2024
return StringUtil.containsWordIgnoreCase(group.getName(), keyword);

src/test/java/seedu/address/logic/commands/AddCommandTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public boolean hasGroup(Group group) {
179179
throw new AssertionError("This method should not be called.");
180180
}
181181

182+
@Override
183+
public void deleteGroup(Group target) {
184+
throw new AssertionError("This method should not be called.");
185+
}
186+
182187
@Override
183188
public void addGroup(Group group) {
184189
throw new AssertionError("This method should not be called.");

src/test/java/seedu/address/logic/commands/AddGrpCommandTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public boolean hasGroup(Group group) {
180180
throw new AssertionError("This method should not be called.");
181181
}
182182

183+
@Override
184+
public void deleteGroup(Group target) {
185+
throw new AssertionError("This method should not be called.");
186+
}
187+
183188
@Override
184189
public void addGroup(Group group) {
185190
throw new AssertionError("This method should not be called.");

0 commit comments

Comments
 (0)