-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from nicholastng010601/master
Created group class
- Loading branch information
Showing
18 changed files
with
392 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/seedu/address/logic/commands/CreateGroupCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Group; | ||
import seedu.address.model.Model; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUPTAG; | ||
|
||
public class CreateGroupCommand extends Command { | ||
public static final String COMMAND_WORD = "new"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a group to the address book. " | ||
+ "Parameters: " | ||
+ PREFIX_GROUPTAG + "GROUPNAME \n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_GROUPTAG + "CS2103T"; | ||
|
||
public static final String MESSAGE_SUCCESS = "New group added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_GROUP = "This group already exists in the address book"; | ||
|
||
private final Group toAdd; | ||
|
||
public CreateGroupCommand(Group group) { | ||
requireNonNull(group); | ||
toAdd = group; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (model.hasGroup(toAdd)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_GROUP); | ||
} | ||
|
||
model.addGroup(toAdd); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/seedu/address/logic/parser/CreateGroupParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import seedu.address.logic.commands.AddCommand; | ||
import seedu.address.logic.commands.CreateGroupCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.Group; | ||
import seedu.address.model.person.Name; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.*; | ||
|
||
public class CreateGroupParser implements Parser<CreateGroupCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the CreateGroupCommand | ||
* and returns a CreateGroupCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
// uses name prefix for now | ||
@Override | ||
public CreateGroupCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
// create a group name prefix? or use name | ||
ArgumentTokenizer.tokenize(args, PREFIX_GROUPTAG); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_GROUPTAG) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CreateGroupCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String groupName = ParserUtil.parseName(argMultimap.getValue(PREFIX_GROUPTAG).get()).toString(); | ||
Group group = new Group(groupName); | ||
|
||
return new CreateGroupCommand(group); | ||
} | ||
|
||
/** | ||
* 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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package seedu.address.model; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import javafx.collections.FXCollections; | ||
import javafx.collections.ObservableList; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.exceptions.DuplicatePersonException; | ||
import seedu.address.model.person.exceptions.PersonNotFoundException; | ||
|
||
public class Group { | ||
ObservableList<Person> listOfGroupMates = FXCollections.observableArrayList(); | ||
String groupName; | ||
|
||
/** | ||
* Name field must be present and not null. | ||
*/ | ||
public Group(String groupName) { | ||
requireNonNull(groupName); | ||
this.groupName = groupName; | ||
} | ||
|
||
public String getGroupName() { | ||
return groupName; | ||
} | ||
|
||
/** | ||
* Returns true if both groups have the same name. | ||
* This defines a weaker notion of equality between two groups. | ||
*/ | ||
public boolean isSameGroup(Group otherGroup) { | ||
if (otherGroup == this) { | ||
return true; | ||
} | ||
|
||
return otherGroup != null | ||
&& otherGroup.getGroupName().equals(getGroupName()); | ||
} | ||
|
||
/** | ||
* Removes the person from the group. | ||
* The person must exist in the group. | ||
*/ | ||
public void remove(Person toRemove) { | ||
requireNonNull(toRemove); | ||
if (!listOfGroupMates.remove(toRemove)) { | ||
throw new PersonNotFoundException(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if the list contains an equivalent person as the given argument. | ||
*/ | ||
public boolean contains(Person toCheck) { | ||
requireNonNull(toCheck); | ||
return listOfGroupMates.stream().anyMatch(toCheck::isSamePerson); | ||
} | ||
|
||
/** | ||
* Adds a person to the list. | ||
* The person must not already exist in the list. | ||
*/ | ||
public void add(Person toAdd) { | ||
requireNonNull(toAdd); | ||
if (contains(toAdd)) { | ||
throw new DuplicatePersonException(); | ||
} | ||
listOfGroupMates.add(toAdd); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("Group name", groupName) | ||
.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package seedu.address.model; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import javafx.collections.FXCollections; | ||
import javafx.collections.ObservableList; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.exceptions.DuplicatePersonException; | ||
import seedu.address.model.person.exceptions.PersonNotFoundException; | ||
|
||
public class GroupList { | ||
|
||
private final ObservableList<Group> internalList = FXCollections.observableArrayList(); | ||
|
||
/** | ||
* Returns true if the list contains an equivalent group as the given argument. | ||
*/ | ||
public boolean contains(Group toCheck) { | ||
requireNonNull(toCheck); | ||
return internalList.stream().anyMatch(toCheck::isSameGroup); | ||
} | ||
|
||
/** | ||
* Adds a group to the list. | ||
* The group must not already exist in the list. | ||
*/ | ||
public void add(Group toAdd) { | ||
requireNonNull(toAdd); | ||
if (contains(toAdd)) { | ||
throw new DuplicatePersonException(); | ||
} | ||
internalList.add(toAdd); | ||
} | ||
|
||
/** | ||
* Removes the equivalent person from the list. | ||
* The person must exist in the list. | ||
*/ | ||
public void remove(Group toRemove) { | ||
requireNonNull(toRemove); | ||
if (!internalList.remove(toRemove)) { | ||
throw new GroupNotFoundException(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof GroupList)) { | ||
return false; | ||
} | ||
|
||
GroupList otherGroupList = (GroupList) other; | ||
return internalList.equals(otherGroupList.internalList); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return internalList.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return internalList.toString(); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/seedu/address/model/GroupNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package seedu.address.model; | ||
|
||
/** | ||
* Signals that the operation is unable to find the specified group. | ||
*/ | ||
public class GroupNotFoundException extends RuntimeException { | ||
} |
Oops, something went wrong.