Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#90 from gloon99/branch-Add-Mee…
Browse files Browse the repository at this point in the history
…ting

Branch add meeting
  • Loading branch information
nopenotj authored Oct 8, 2020
2 parents 0be8caf + 72f7839 commit 678c0fd
Show file tree
Hide file tree
Showing 48 changed files with 1,469 additions and 158 deletions.
33 changes: 27 additions & 6 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.model.AddressBook;
import seedu.address.model.MeetingBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyMeetingBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.UserPrefs;
import seedu.address.model.util.SampleDataUtil;
import seedu.address.storage.AddressBookStorage;
import seedu.address.storage.JsonAddressBookStorage;
import seedu.address.storage.JsonMeetingBookStorage;
import seedu.address.storage.JsonUserPrefsStorage;
import seedu.address.storage.MeetingBookStorage;
import seedu.address.storage.Storage;
import seedu.address.storage.StorageManager;
import seedu.address.storage.UserPrefsStorage;
Expand Down Expand Up @@ -57,7 +61,8 @@ public void init() throws Exception {
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);
MeetingBookStorage meetingBookStorage = new JsonMeetingBookStorage(userPrefs.getMeetingBookFilePath());
storage = new StorageManager(addressBookStorage, meetingBookStorage, userPrefsStorage);

initLogging(config);

Expand All @@ -75,22 +80,38 @@ public void init() throws Exception {
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialData;
ReadOnlyAddressBook addressBookInitialData;
try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
addressBookInitialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
initialData = new AddressBook();
addressBookInitialData = new AddressBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initialData = new AddressBook();
addressBookInitialData = new AddressBook();
}

return new ModelManager(initialData, userPrefs);
Optional<ReadOnlyMeetingBook> meetingBookOptional;
ReadOnlyMeetingBook meetingBookInitialData;
try {
meetingBookOptional = storage.readMeetingBook();
if (!meetingBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
}
meetingBookInitialData = meetingBookOptional.orElseGet(SampleDataUtil::getSampleMeetingBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
meetingBookInitialData = new MeetingBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
meetingBookInitialData = new MeetingBook();
}

return new ModelManager(addressBookInitialData, meetingBookInitialData, userPrefs);
}

private void initLogging(Config config) {
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.MeetingBookParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
Expand All @@ -27,6 +28,7 @@ public class LogicManager implements Logic {
private final Model model;
private final Storage storage;
private final AddressBookParser addressBookParser;
private final MeetingBookParser meetingBookParser;

/**
* Constructs a {@code LogicManager} with the given {@code Model} and {@code Storage}.
Expand All @@ -35,18 +37,27 @@ public LogicManager(Model model, Storage storage) {
this.model = model;
this.storage = storage;
addressBookParser = new AddressBookParser();
meetingBookParser = new MeetingBookParser();
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");

String firstWord = commandText.split(" ")[0];

CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);
Command command;
if (firstWord.equals("meeting")) {
command = meetingBookParser.parseCommand(commandText);
} else {
command = addressBookParser.parseCommand(commandText);
}
commandResult = command.execute(model);

try {
storage.saveAddressBook(model.getAddressBook());
storage.saveMeetingBook(model.getMeetingBook());
} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
}
Expand Down
159 changes: 159 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddLabelCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
* Edits the details of an existing person in the address book.
*/
public class AddLabelCommand extends Command {

public static final String COMMAND_WORD = "label add";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a label to the person specified.\n"
+ "Parameters: NAME (must be name of person existing in ModDuke) "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ "Roy "
+ PREFIX_TAG + "classmate";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Labelled Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";

private final Name targetName;
private final LabelPersonDescriptor labelPersonDescriptor;

/**
* @param targetName of the person in the filtered person list to label
*/
public AddLabelCommand(Name targetName, LabelPersonDescriptor labelPersonDescriptor) {
requireNonNull(targetName);
requireNonNull(labelPersonDescriptor);

this.targetName = targetName;
this.labelPersonDescriptor = new LabelPersonDescriptor(labelPersonDescriptor);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
boolean isValidContact = model.hasPersonName(targetName);

if (!isValidContact) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED);
}

List<Person> filteredList = model.getFilteredPersonList().stream()
.filter(person -> person.isSameName(targetName)).collect(Collectors.toList());
Person personToLabel = filteredList.get(0);

Person labelledPerson = createLabelledPerson(personToLabel, labelPersonDescriptor);

if (!personToLabel.isSamePerson(labelledPerson) && model.hasPerson(labelledPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.setPerson(personToLabel, labelledPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, labelledPerson));
}

/**
* Creates and returns a {@code Person} with the details of {@code personToEdit}
* edited with {@code editPersonDescriptor}.
*/
private static Person createLabelledPerson(Person personToLabel, LabelPersonDescriptor labelPersonDescriptor) {
assert personToLabel != null;

Set<Tag> updatedTags = labelPersonDescriptor.getTags().orElse(personToLabel.getTags());

return new Person(personToLabel.getName(), personToLabel.getPhone(), personToLabel.getEmail(), updatedTags);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof AddLabelCommand)) {
return false;
}

// state check
AddLabelCommand e = (AddLabelCommand) other;
return targetName.equals(e.targetName)
&& labelPersonDescriptor.equals(e.labelPersonDescriptor);
}

/**
* Stores the details to edit the person with. Each non-empty field value will replace the
* corresponding field value of the person.
*/
public static class LabelPersonDescriptor {
private Set<Tag> tags;

public LabelPersonDescriptor() {}

/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public LabelPersonDescriptor(LabelPersonDescriptor toCopy) {
setTags(toCopy.tags);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof LabelPersonDescriptor)) {
return false;
}

// state check
LabelPersonDescriptor e = (LabelPersonDescriptor) other;

return getTags().equals(e.getTags());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETINGNAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONTACTADD;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
Expand All @@ -19,16 +18,13 @@ public class AddMeetingCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
+ "Parameters: "
+ PREFIX_NAME + "MEETING NAME "
+ PREFIX_MEETINGNAME + "MEETING NAME "
+ PREFIX_DATE + "DATE "
+ PREFIX_TIME + "TIME "
+ "[" + PREFIX_CONTACTADD + "PERSON]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "CS2103 weekly meeting "
+ PREFIX_MEETINGNAME + "CS2103 weekly meeting "
+ PREFIX_DATE + "2020-09-20 "
+ PREFIX_TIME+ "10:00 "
+ PREFIX_CONTACTADD + "Roy "
+ PREFIX_CONTACTADD + "Jerryl ";
+ PREFIX_TIME + "10:00 ";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_MEETING = "This meeting already exists in the address book";
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
Expand All @@ -29,36 +29,36 @@
*/
public class EditCommand extends Command {

public static final String COMMAND_WORD = "edit";
public static final String COMMAND_WORD = "contact edit";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
+ "by the name of the person in the displayed person list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_NAME + "NAME] "
+ "Parameters: NAME (must be name of person existing in ModDuke) "
+ "[" + PREFIX_NAME + "NEW_NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ "Example: " + COMMAND_WORD + " john doe "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";

private final Index index;
private final Name name;
private final EditPersonDescriptor editPersonDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param name of the person in the filtered person list to edit
* @param editPersonDescriptor details to edit the person with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(index);
public EditCommand(Name name, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(name);
requireNonNull(editPersonDescriptor);

this.index = index;
this.name = name;
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor);
}

Expand All @@ -67,11 +67,15 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
boolean isValidContact = model.hasPersonName(name);
if (!isValidContact) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
List<Person> filteredList = lastShownList.stream()
.filter(person -> person.isSameName(name)).collect(Collectors.toList());
Person personToEdit = filteredList.get(0);

Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
Expand Down Expand Up @@ -112,7 +116,7 @@ public boolean equals(Object other) {

// state check
EditCommand e = (EditCommand) other;
return index.equals(e.index)
return name.equals(e.name)
&& editPersonDescriptor.equals(e.editPersonDescriptor);
}

Expand Down
Loading

0 comments on commit 678c0fd

Please sign in to comment.