Skip to content

Commit fc52d0c

Browse files
authored
Merge pull request nus-cs2103-AY2122S1#90 from justintanyf/branch_Cca_Functionality
Added additional functionality to CCAs
2 parents 9352cfa + c59e43b commit fc52d0c

22 files changed

Lines changed: 370 additions & 349 deletions

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ test {
2525
finalizedBy jacocoTestReport
2626
}
2727

28+
run {
29+
enableAssertions = true
30+
}
31+
2832
task coverage(type: JacocoReport) {
2933
sourceDirectories.from files(sourceSets.main.allSource.srcDirs)
3034
classDirectories.from files(sourceSets.main.output)
@@ -66,7 +70,7 @@ dependencies {
6670
}
6771

6872
shadowJar {
69-
archiveName = 'addressbook.jar'
73+
archiveName = 'ePoch.jar'
7074
}
7175

7276
defaultTasks 'clean', 'test'

src/main/java/seedu/address/logic/commands/cca/CcaAddCommand.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import static java.util.Objects.requireNonNull;
44
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
56

7+
import java.util.logging.Level;
8+
import java.util.logging.Logger;
9+
10+
import seedu.address.commons.core.LogsCenter;
611
import seedu.address.logic.commands.Command;
712
import seedu.address.logic.commands.CommandResult;
813
import seedu.address.logic.commands.exceptions.CommandException;
@@ -19,12 +24,17 @@ public class CcaAddCommand extends Command {
1924
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a CCA to the address book. "
2025
+ "Parameters: "
2126
+ PREFIX_NAME + "NAME "
27+
+ "[" + PREFIX_TAG + "TAG]...\n"
2228
+ "Example: " + COMMAND_WORD + " "
23-
+ PREFIX_NAME + "NUS Symphony Orchestra ";
29+
+ PREFIX_NAME + "NUS Symphony Orchestra "
30+
+ PREFIX_TAG + "Music "
31+
+ PREFIX_TAG + "InterviewDependent";
2432

2533
public static final String MESSAGE_SUCCESS = "New CCA added: %1$s";
2634
public static final String MESSAGE_DUPLICATE_CCA = "This CCA already exists in the address book";
2735

36+
private final Logger logger = LogsCenter.getLogger(CcaAddCommand.class);
37+
2838
private final Cca toAdd;
2939

3040
/**
@@ -44,6 +54,7 @@ public CommandResult execute(Model model) throws CommandException {
4454
}
4555

4656
model.addCca(toAdd);
57+
logger.log(Level.FINE, "CCA successfully added");
4758
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
4859
}
4960

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package seedu.address.logic.commands.cca;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
6+
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CCAS;
7+
8+
import java.util.Collections;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Optional;
12+
import java.util.Set;
13+
14+
import seedu.address.commons.core.Messages;
15+
import seedu.address.commons.core.index.Index;
16+
import seedu.address.commons.util.CollectionUtil;
17+
import seedu.address.logic.commands.Command;
18+
import seedu.address.logic.commands.CommandResult;
19+
import seedu.address.logic.commands.exceptions.CommandException;
20+
import seedu.address.model.Model;
21+
import seedu.address.model.cca.Cca;
22+
import seedu.address.model.cca.CcaName;
23+
import seedu.address.model.person.Person;
24+
import seedu.address.model.tag.Tag;
25+
26+
/**
27+
* Edits the details of an existing person in the address book.
28+
*/
29+
public class CcaEditCommand extends Command {
30+
31+
public static final String COMMAND_WORD = "editc";
32+
33+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the CCA identified "
34+
+ "by the index number used in the displayed CCA list. "
35+
+ "Existing values will be overwritten by the input values.\n"
36+
+ "Parameters: INDEX (must be a positive integer) "
37+
+ "[" + PREFIX_NAME + "NAME] "
38+
+ "[" + PREFIX_TAG + "TAG]...\n"
39+
+ "Example: " + COMMAND_WORD + " 1 "
40+
+ PREFIX_NAME + "New Name "
41+
+ PREFIX_TAG + "newTag";
42+
43+
public static final String MESSAGE_EDIT_CCA_SUCCESS = "Edited CCA: %1$s";
44+
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
45+
public static final String MESSAGE_DUPLICATE_CCA = "This CCA already exists in the address book.";
46+
47+
private final Index index;
48+
private final EditCcaDescriptor editCcaDescriptor;
49+
50+
/**
51+
* @param index of the cca in the filtered CCA list to edit
52+
* @param editCcaDescriptor details to edit the person with
53+
*/
54+
public CcaEditCommand(Index index, EditCcaDescriptor editCcaDescriptor) {
55+
requireNonNull(index);
56+
requireNonNull(editCcaDescriptor);
57+
58+
this.index = index;
59+
this.editCcaDescriptor = new EditCcaDescriptor(editCcaDescriptor);
60+
}
61+
62+
@Override
63+
public CommandResult execute(Model model) throws CommandException {
64+
requireNonNull(model);
65+
List<Cca> lastShownList = model.getFilteredCcaList();
66+
67+
if (index.getZeroBased() >= lastShownList.size()) {
68+
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
69+
}
70+
71+
Cca ccaToEdit = lastShownList.get(index.getZeroBased());
72+
Cca editedCca = createEditedCca(ccaToEdit, editCcaDescriptor);
73+
74+
if (!ccaToEdit.isSameCca(editedCca) && model.hasCca(editedCca)) {
75+
throw new CommandException(MESSAGE_DUPLICATE_CCA);
76+
}
77+
78+
model.setCca(ccaToEdit, editedCca);
79+
model.updateFilteredCcaList(PREDICATE_SHOW_ALL_CCAS);
80+
return new CommandResult(String.format(MESSAGE_EDIT_CCA_SUCCESS, editedCca));
81+
}
82+
83+
/**
84+
* Creates and returns a {@code Cca} with the details of {@code ccaToEdit}
85+
* edited with {@code editCcaDescriptor}.
86+
*/
87+
private static Cca createEditedCca(Cca ccaToEdit, EditCcaDescriptor editCcaDescriptor) {
88+
assert ccaToEdit != null;
89+
90+
CcaName updatedName = editCcaDescriptor.getName().orElse(ccaToEdit.getName());
91+
Set<Person> copyOfPersonArrayList = ccaToEdit.getPersonArrayList();
92+
Set<Tag> updatedTags = editCcaDescriptor.getTags().orElse(ccaToEdit.getTags());
93+
94+
return new Cca(updatedName, copyOfPersonArrayList, updatedTags);
95+
}
96+
97+
@Override
98+
public boolean equals(Object other) {
99+
// short circuit if same object
100+
if (other == this) {
101+
return true;
102+
}
103+
104+
// instanceof handles nulls
105+
if (!(other instanceof CcaEditCommand)) {
106+
return false;
107+
}
108+
109+
// state check
110+
CcaEditCommand e = (CcaEditCommand) other;
111+
return index.equals(e.index)
112+
&& editCcaDescriptor.equals(e.editCcaDescriptor);
113+
}
114+
115+
/**
116+
* Stores the details to edit the CCA with. Each non-empty field value will replace the
117+
* corresponding field value of the person.
118+
*/
119+
public static class EditCcaDescriptor {
120+
private CcaName name;
121+
private Set<Tag> tags;
122+
123+
public EditCcaDescriptor() {}
124+
125+
/**
126+
* Copy constructor.
127+
* A defensive copy of {@code tags} is used internally.
128+
*/
129+
public EditCcaDescriptor(EditCcaDescriptor toCopy) {
130+
setName(toCopy.name);
131+
setTags(toCopy.tags);
132+
}
133+
134+
/**
135+
* Returns true if at least one field is edited.
136+
*/
137+
public boolean isAnyFieldEdited() {
138+
return CollectionUtil.isAnyNonNull(name, tags);
139+
}
140+
141+
public void setName(CcaName name) {
142+
this.name = name;
143+
}
144+
145+
public Optional<CcaName> getName() {
146+
return Optional.ofNullable(name);
147+
}
148+
149+
/**
150+
* Sets {@code tags} to this object's {@code tags}.
151+
* A defensive copy of {@code tags} is used internally.
152+
*/
153+
public void setTags(Set<Tag> tags) {
154+
this.tags = (tags != null) ? new HashSet<>(tags) : null;
155+
}
156+
157+
/**
158+
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
159+
* if modification is attempted.
160+
* Returns {@code Optional#empty()} if {@code tags} is null.
161+
*/
162+
public Optional<Set<Tag>> getTags() {
163+
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
164+
}
165+
166+
@Override
167+
public boolean equals(Object other) {
168+
// short circuit if same object
169+
if (other == this) {
170+
return true;
171+
}
172+
173+
// instanceof handles nulls
174+
if (!(other instanceof EditCcaDescriptor)) {
175+
return false;
176+
}
177+
178+
// state check
179+
EditCcaDescriptor e = (EditCcaDescriptor) other;
180+
181+
return getName().equals(e.getName())
182+
&& getTags().equals(e.getTags());
183+
}
184+
}
185+
}

src/main/java/seedu/address/logic/commands/person/PersonAddCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public class PersonAddCommand extends Command {
2020

21-
public static final String COMMAND_WORD = "add";
21+
public static final String COMMAND_WORD = "addp";
2222

2323
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
2424
+ "Parameters: "

src/main/java/seedu/address/logic/commands/person/PersonEditCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
public class PersonEditCommand extends Command {
3535

36-
public static final String COMMAND_WORD = "edit";
36+
public static final String COMMAND_WORD = "editp";
3737

3838
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
3939
+ "by the index number used in the displayed person list. "

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import seedu.address.logic.commands.ListCommand;
1515
import seedu.address.logic.commands.cca.CcaAddCommand;
1616
import seedu.address.logic.commands.cca.CcaDeleteCommand;
17+
import seedu.address.logic.commands.cca.CcaEditCommand;
1718
import seedu.address.logic.commands.cca.CcaEnrolCommand;
1819
import seedu.address.logic.commands.cca.CcaExpelCommand;
1920
import seedu.address.logic.commands.cca.CcaFindCommand;
@@ -26,6 +27,7 @@
2627
import seedu.address.logic.commands.reminder.ReminderFindCommand;
2728
import seedu.address.logic.parser.cca.CcaAddCommandParser;
2829
import seedu.address.logic.parser.cca.CcaDeleteCommandParser;
30+
import seedu.address.logic.parser.cca.CcaEditCommandParser;
2931
import seedu.address.logic.parser.cca.CcaEnrolCommandParser;
3032
import seedu.address.logic.parser.cca.CcaExpelCommandParser;
3133
import seedu.address.logic.parser.cca.CcaFindCommandParser;
@@ -99,6 +101,9 @@ public Command parseCommand(String userInput) throws ParseException {
99101
case CcaAddCommand.COMMAND_WORD:
100102
return new CcaAddCommandParser().parse(arguments);
101103

104+
case CcaEditCommand.COMMAND_WORD:
105+
return new CcaEditCommandParser().parse(arguments);
106+
102107
case CcaDeleteCommand.COMMAND_WORD:
103108
return new CcaDeleteCommandParser().parse(arguments);
104109

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

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
import seedu.address.commons.util.StringUtil;
1212
import seedu.address.logic.parser.exceptions.ParseException;
1313
import seedu.address.model.cca.CcaName;
14-
import seedu.address.model.cca.Cid;
1514
import seedu.address.model.person.Address;
1615
import seedu.address.model.person.Email;
1716
import seedu.address.model.person.Name;
1817
import seedu.address.model.person.Phone;
19-
import seedu.address.model.person.Pid;
2018
import seedu.address.model.reminder.ReminderName;
2119
import seedu.address.model.reminder.ReminderStartDate;
2220
import seedu.address.model.tag.Tag;
@@ -180,49 +178,4 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
180178
}
181179
return tagSet;
182180
}
183-
184-
// todo: Should be able to delete if the usual index works
185-
/**
186-
* Parses a {@code String cid} into an {@code int}.
187-
* Leading and trailing whitespaces will be trimmed.
188-
*
189-
* @throws ParseException if the given {@code cid} is invalid.
190-
*/
191-
public static Cid parseCid(String cid) throws ParseException {
192-
requireNonNull(cid);
193-
String trimmedCid = cid.trim();
194-
int intCid = -1;
195-
try {
196-
intCid = Integer.parseInt(trimmedCid);
197-
} catch (Exception e) {
198-
throw new ParseException("Please input cid as a number");
199-
} finally {
200-
if (intCid == -1) {
201-
throw new ParseException("Please input cid as a number");
202-
}
203-
return new Cid(cid);
204-
}
205-
}
206-
207-
/**
208-
* Parses a {@code String pid} into an {@code int}.
209-
* Leading and trailing whitespaces will be trimmed.
210-
*
211-
* @throws ParseException if the given {@code pid} is invalid.
212-
*/
213-
public static Pid parsePid(String pid) throws ParseException {
214-
requireNonNull(pid);
215-
String trimmedPid = pid.trim();
216-
int intPid = -1;
217-
try {
218-
intPid = Integer.parseInt(trimmedPid);
219-
} catch (Exception e) {
220-
throw new ParseException("Please input pid as a number");
221-
} finally {
222-
if (intPid == -1) {
223-
throw new ParseException("Please input pid as a number");
224-
}
225-
return new Pid(pid);
226-
}
227-
}
228181
}

src/main/java/seedu/address/logic/parser/cca/CcaAddCommandParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
44
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
56

7+
import java.util.Set;
68
import java.util.stream.Stream;
79

810
import seedu.address.logic.commands.cca.CcaAddCommand;
@@ -14,6 +16,7 @@
1416
import seedu.address.logic.parser.exceptions.ParseException;
1517
import seedu.address.model.cca.Cca;
1618
import seedu.address.model.cca.CcaName;
19+
import seedu.address.model.tag.Tag;
1720

1821
public class CcaAddCommandParser implements Parser<CcaAddCommand> {
1922
/**
@@ -23,17 +26,18 @@ public class CcaAddCommandParser implements Parser<CcaAddCommand> {
2326
*/
2427
public CcaAddCommand parse(String args) throws ParseException {
2528
ArgumentMultimap argMultimap =
26-
ArgumentTokenizer.tokenize(args, PREFIX_NAME);
29+
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_TAG);
2730

2831
if (!arePrefixesPresent(argMultimap, PREFIX_NAME)
2932
|| !argMultimap.getPreamble().isEmpty()) {
3033
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CcaAddCommand.MESSAGE_USAGE));
3134
}
3235

3336
CcaName ccaName = ParserUtil.parseCcaName(argMultimap.getValue(PREFIX_NAME).get());
37+
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
3438

3539
// Create a new CCA here
36-
Cca cca = new Cca(ccaName);
40+
Cca cca = new Cca(ccaName, tagList);
3741

3842
return new CcaAddCommand(cca);
3943
}

0 commit comments

Comments
 (0)