Skip to content

Commit

Permalink
Merge pull request #61 from kohkakohla/addAddCommands
Browse files Browse the repository at this point in the history
Add add commands
  • Loading branch information
mingyang143 authored Oct 17, 2024
2 parents 8756a2c + 25d8b98 commit f3590d4
Show file tree
Hide file tree
Showing 30 changed files with 1,051 additions and 158 deletions.
5 changes: 2 additions & 3 deletions docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,8 @@ _{Explain here how the data archiving feature will be implemented}_
* is reasonably comfortable using CLI apps
* has a need to generate and visualize reports (e.g., volunteer hours, visit frequency) in a simple and efficient way

**Value proposition**: Manage volunteering _tutor_ and _tutee_ records, site visits, and scheduling faster than a typical
mouse/GUI-driven app, while efficiently tracking and enhancing volunteer engagement.

**Value proposition**: Manage volunteering _tutor_ and _tutee_ records, site visits, and scheduling faster than a
typical mouse/GUI-driven app, while efficiently tracking and enhancing volunteer engagement.

### User stories

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic;

import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Path;
import java.util.logging.Logger;

Expand Down Expand Up @@ -66,8 +67,10 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
logger.info("Address book modified, saving to file.");
try {
storage.saveAddressBook(model.getAddressBook());
} catch (AccessDeniedException ade) {
throw new CommandException(FILE_OPS_PERMISSION_ERROR_FORMAT, ade);
} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
throw new CommandException(FILE_OPS_ERROR_FORMAT, ioe);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.person.Tutee;

/**
* Adds a person to the address book.
*/
public class AddCommand extends Command {
public class AddTuteeCommand extends Command {

public static final String COMMAND_WORD = "add";
public static final String COMMAND_WORD = "addTutee";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a tutee to the address book. "
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
Expand All @@ -36,17 +36,17 @@ public class AddCommand extends Command {
+ PREFIX_HOURS + "6 ";


public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
public static final String MESSAGE_SUCCESS = "New tutee added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This tutee already exists in the address book";

private final Person toAdd;
private final Tutee toAdd;

/**
* Creates an AddCommand to add the specified {@code Person}
*/
public AddCommand(Person person) {
requireNonNull(person);
toAdd = person;
public AddTuteeCommand(Tutee tutee) {
requireNonNull(tutee);
toAdd = tutee;
}

@Override
Expand All @@ -69,18 +69,18 @@ public boolean equals(Object other) {
}

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

AddCommand otherAddCommand = (AddCommand) other;
AddTuteeCommand otherAddCommand = (AddTuteeCommand) other;
return toAdd.equals(otherAddCommand.toAdd);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.add("toAddTutee", toAdd)
.toString();
}
}
86 changes: 86 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddTutorCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_HOURS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Tutor;

/**
* Adds a person to the address book.
*/
public class AddTutorCommand extends Command {

public static final String COMMAND_WORD = "addTutor";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a tutor to the address book. "
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_HOURS + "HOURS "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_HOURS + "6 ";


public static final String MESSAGE_SUCCESS = "New tutor added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This tutor already exists in the address book";

private final Tutor toAdd;

/**
* Creates an AddCommand to add the specified {@code Person}
*/
public AddTutorCommand(Tutor tutor) {
requireNonNull(tutor);
toAdd = tutor;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addPerson(toAdd);
model.commitAddressBook();
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

AddTutorCommand otherAddCommand = (AddTutorCommand) other;
return toAdd.equals(otherAddCommand.toAdd);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAddTutor", toAdd)
.toString();
}
}
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Tutor;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -106,7 +107,8 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Hours updatedHours = editPersonDescriptor.getHours().orElse(personToEdit.getHours());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedHours, updatedTags);
// TODO CHANGE
return new Tutor(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedHours, updatedTags);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@
import java.util.Set;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddTuteeCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Hours;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Tutee;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new AddCommand object
* Parses input arguments and creates a new AddTuteeCommand object
*/
public class AddCommandParser implements Parser<AddCommand> {
public class AddTuteeCommandParser implements Parser<AddTuteeCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* Parses the given {@code String} of arguments in the context of the AddTuteeCommand
* and returns an AddTuteeCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
public AddTuteeCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_HOURS, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_HOURS)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTuteeCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_HOURS);
Expand All @@ -49,9 +49,9 @@ public AddCommand parse(String args) throws ParseException {
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Person person = new Person(name, phone, email, address, hours, tagList);
Tutee tutee = new Tutee(name, phone, email, address, hours, tagList);

return new AddCommand(person);
return new AddTuteeCommand(tutee);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_HOURS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Set;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddTutorCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Hours;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Tutor;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new AddTutorCommand object
*/
public class AddTutorCommandParser implements Parser<AddTutorCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddTutorCommand
* and returns an AddTutorCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddTutorCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_HOURS, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_HOURS)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTutorCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_HOURS);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Hours hours = ParserUtil.parseHours(argMultimap.getValue(PREFIX_HOURS).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Tutor tutor = new Tutor(name, phone, email, address, hours, tagList);

return new AddTutorCommand(tutor);
}

/**
* 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import java.util.regex.Pattern;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddTuteeCommand;
import seedu.address.logic.commands.AddTutorCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -57,9 +58,11 @@ public Command parseCommand(String userInput) throws ParseException {

switch (commandWord) {

case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);
case AddTuteeCommand.COMMAND_WORD:
return new AddTuteeCommandParser().parse(arguments);

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

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
* Represents a Person in the address book.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Person {
public abstract class Person {

// Identity fields
private final Name name;
private final Phone phone;
private final Email email;

// Data fields
private final Hours hours;
private final Address address;
private final Hours hours;
private final Set<Tag> tags = new HashSet<>();

/**
Expand All @@ -39,6 +39,10 @@ public Person(Name name, Phone phone, Email email, Address address, Hours hours,
this.tags.addAll(tags);
}

public abstract boolean isTutor();

public abstract boolean isTutee();

public Name getName() {
return name;
}
Expand All @@ -59,6 +63,11 @@ public Hours getHours() {
return hours;
}

public String getRole() {
return "Person";
}


/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
Expand Down
Loading

0 comments on commit f3590d4

Please sign in to comment.