Skip to content

Commit

Permalink
Merge pull request #11 from nicholastng010601/master
Browse files Browse the repository at this point in the history
Created group class
  • Loading branch information
Kailash201 authored Oct 3, 2023
2 parents 105355e + 6e9eb03 commit 02e7f67
Show file tree
Hide file tree
Showing 18 changed files with 392 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/.idea/src.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.stream.Stream;

import seedu.address.logic.parser.Prefix;
import seedu.address.model.Group;
import seedu.address.model.GroupList;
import seedu.address.model.person.Person;

/**
Expand Down Expand Up @@ -48,4 +50,13 @@ public static String format(Person person) {
return builder.toString();
}

/**
* Formats the {@code group} for display to the user.
*/
public static String format(Group group) {
final StringBuilder builder = new StringBuilder();
builder.append(group.getGroupName());
return builder.toString();
}

}
41 changes: 41 additions & 0 deletions src/main/java/seedu/address/logic/commands/CreateGroupCommand.java
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CreateGroupCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
Expand Down Expand Up @@ -56,6 +57,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);

case CreateGroupCommand.COMMAND_WORD:
return new CreateGroupParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_GROUPTAG = new Prefix("g/");

}
46 changes: 46 additions & 0 deletions src/main/java/seedu/address/logic/parser/CreateGroupParser.java
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());
}
}
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public class AddressBook implements ReadOnlyAddressBook {

private final UniquePersonList persons;
private final GroupList groups;

/*
* The 'unusual' code block below is a non-static initialization block, sometimes used to avoid duplication
Expand All @@ -26,6 +27,7 @@ public class AddressBook implements ReadOnlyAddressBook {
*/
{
persons = new UniquePersonList();
groups = new GroupList();
}

public AddressBook() {}
Expand Down Expand Up @@ -94,6 +96,32 @@ public void removePerson(Person key) {
persons.remove(key);
}

//// group-level operations

/**
* Returns true if a group with the same identity as {@code group} exists in the address book.
*/
public boolean hasGroup(Group group) {
requireNonNull(group);
return groups.contains(group);
}

/**
* Adds a group to the address book.
* The group must not already exist in the address book.
*/
public void addGroup(Group g) {
groups.add(g);
}

/**
* Removes {@code key} from this {@code AddressBook}.
* {@code key} must exist in the address book.
*/
public void removeGroup(Group key) {
groups.remove(key);
}

//// util methods

@Override
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/seedu/address/model/Group.java
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();
}

}
75 changes: 75 additions & 0 deletions src/main/java/seedu/address/model/GroupList.java
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 src/main/java/seedu/address/model/GroupNotFoundException.java
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 {
}
Loading

0 comments on commit 02e7f67

Please sign in to comment.