Skip to content

Commit

Permalink
Merge pull request #63 from chongjunwei/event-model
Browse files Browse the repository at this point in the history
Event model and eadd command
  • Loading branch information
gordonlzy authored Oct 14, 2021
2 parents fad4289 + 79e3da9 commit fe9d643
Show file tree
Hide file tree
Showing 27 changed files with 894 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/main/java/safeforhall/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import safeforhall.logic.parser.exceptions.ParseException;
import safeforhall.model.Model;
import safeforhall.model.ReadOnlyAddressBook;
import safeforhall.model.event.Event;
import safeforhall.model.person.Person;

/**
Expand All @@ -34,6 +35,9 @@ public interface Logic {
/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<Person> getFilteredPersonList();

/** Returns an unmodifiable view of the filtered list of events */
ObservableList<Event> getFilteredEventList();

/**
* Returns the user prefs' address book file path.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/safeforhall/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import safeforhall.logic.parser.exceptions.ParseException;
import safeforhall.model.Model;
import safeforhall.model.ReadOnlyAddressBook;
import safeforhall.model.event.Event;
import safeforhall.model.person.Person;
import safeforhall.storage.Storage;

Expand Down Expand Up @@ -64,6 +65,11 @@ public ObservableList<Person> getFilteredPersonList() {
return model.getFilteredPersonList();
}

@Override
public ObservableList<Event> getFilteredEventList() {
return model.getFilteredEventList();
}

@Override
public Path getAddressBookFilePath() {
return model.getAddressBookFilePath();
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/safeforhall/logic/commands/EaddCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package safeforhall.logic.commands;

import static java.util.Objects.requireNonNull;

import safeforhall.logic.commands.exceptions.CommandException;
import safeforhall.logic.parser.CliSyntax;
import safeforhall.model.Model;
import safeforhall.model.event.Event;

public class EaddCommand extends Command {
public static final String COMMAND_WORD = "eadd";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an event to the address book. "
+ "Parameters: "
+ CliSyntax.PREFIX_NAME + "NAME "
+ CliSyntax.PREFIX_DATE + "DATE "
+ CliSyntax.PREFIX_VENUE + "VENUE "
+ CliSyntax.PREFIX_CAPACITY + "CAPACITY "

+ "Example: " + COMMAND_WORD + " "
+ CliSyntax.PREFIX_NAME + "Football "
+ CliSyntax.PREFIX_DATE + "03-01-2021 "
+ CliSyntax.PREFIX_VENUE + "NUS field "
+ CliSyntax.PREFIX_CAPACITY + "5 ";

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

private final Event toAdd;

/**
* Creates an EaddCommand to add the specified {@code Event}
*/
public EaddCommand(Event event) {
requireNonNull(event);
toAdd = event;
}

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

if (model.hasEvent(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_EVENT);
}

model.addEvent(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof EaddCommand // instanceof handles nulls
&& toAdd.equals(((EaddCommand) other).toAdd));
}
}
4 changes: 4 additions & 0 deletions src/main/java/safeforhall/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import safeforhall.logic.commands.ClearCommand;
import safeforhall.logic.commands.Command;
import safeforhall.logic.commands.DeleteCommand;
import safeforhall.logic.commands.EaddCommand;
import safeforhall.logic.commands.EditCommand;
import safeforhall.logic.commands.ExitCommand;
import safeforhall.logic.commands.FindCommand;
Expand Down Expand Up @@ -72,6 +73,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case EaddCommand.COMMAND_WORD:
return new EaddCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/safeforhall/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public class CliSyntax {
public static final Prefix PREFIX_FACULTY = new Prefix("f/");
public static final Prefix PREFIX_FETDATE = new Prefix("fd/");
public static final Prefix PREFIX_COLLECTIONDATE = new Prefix("cd/");
public static final Prefix PREFIX_DATE = new Prefix("d/");
public static final Prefix PREFIX_VENUE = new Prefix("l/");
public static final Prefix PREFIX_CAPACITY = new Prefix("c/");
}
57 changes: 57 additions & 0 deletions src/main/java/safeforhall/logic/parser/EaddCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package safeforhall.logic.parser;

import static safeforhall.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static safeforhall.logic.parser.CliSyntax.PREFIX_CAPACITY;
import static safeforhall.logic.parser.CliSyntax.PREFIX_DATE;
import static safeforhall.logic.parser.CliSyntax.PREFIX_NAME;
import static safeforhall.logic.parser.CliSyntax.PREFIX_VENUE;

import java.util.stream.Stream;

import safeforhall.logic.commands.EaddCommand;
import safeforhall.logic.parser.exceptions.ParseException;
import safeforhall.model.event.Capacity;
import safeforhall.model.event.Event;
import safeforhall.model.event.EventDate;
import safeforhall.model.event.EventName;
import safeforhall.model.event.Venue;

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

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

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_DATE,
PREFIX_VENUE, PREFIX_CAPACITY)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EaddCommand.MESSAGE_USAGE));
}

EventName eventName = ParserUtil.parseEventName(argMultimap.getValue(PREFIX_NAME).get());
EventDate eventDate = ParserUtil.parseEventDate(argMultimap.getValue(PREFIX_DATE).get());
Venue venue = ParserUtil.parseVenue(argMultimap.getValue(PREFIX_VENUE).get());
Capacity capacity = ParserUtil.parseCapacity(argMultimap.getValue(PREFIX_CAPACITY).get());

Event event = new Event(eventName, eventDate, venue, capacity);
return new EaddCommand(event);
}

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

}
64 changes: 64 additions & 0 deletions src/main/java/safeforhall/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import safeforhall.commons.core.index.Index;
import safeforhall.commons.util.StringUtil;
import safeforhall.logic.parser.exceptions.ParseException;
import safeforhall.model.event.Capacity;
import safeforhall.model.event.EventDate;
import safeforhall.model.event.EventName;
import safeforhall.model.event.Venue;
import safeforhall.model.person.Email;
import safeforhall.model.person.Faculty;
import safeforhall.model.person.LastDate;
Expand Down Expand Up @@ -163,4 +167,64 @@ public static LastDate parseDate(String date) throws ParseException {
}
return new LastDate(trimmedDate);
}

/**
* Parses a {@code String eventName} into a {@code EventName}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code eventName} is invalid.
*/
public static EventName parseEventName(String eventName) throws ParseException {
requireNonNull(eventName);
String trimmedEventName = eventName.trim();
if (!EventName.isValidEventName(trimmedEventName)) {
throw new ParseException(EventName.MESSAGE_CONSTRAINTS);
}
return new EventName(trimmedEventName);
}

/**
* Parses a {@code String eventDate} into a {@code EventDate}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code eventDate} is invalid.
*/
public static EventDate parseEventDate(String eventDate) throws ParseException {
requireNonNull(eventDate);
String trimmedEventDate = eventDate.trim();
if (!EventDate.isValidEventDate(trimmedEventDate)) {
throw new ParseException(EventDate.MESSAGE_CONSTRAINTS);
}
return new EventDate(trimmedEventDate);
}

/**
* Parses a {@code String venue} into a {@code Venue}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code venue} is invalid.
*/
public static Venue parseVenue(String venue) throws ParseException {
requireNonNull(venue);
String trimmedVenue = venue.trim();
if (!Venue.isValidVenue(trimmedVenue)) {
throw new ParseException(Venue.MESSAGE_CONSTRAINTS);
}
return new Venue(trimmedVenue);
}

/**
* Parses a {@code String capacity} into a {@code Capacity}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code capacity} is invalid.
*/
public static Capacity parseCapacity(String capacity) throws ParseException {
requireNonNull(capacity);
String trimmedCapacity = capacity.trim();
if (!Capacity.isValidCapacity(trimmedCapacity)) {
throw new ParseException(Capacity.MESSAGE_CONSTRAINTS);
}
return new Capacity(trimmedCapacity);
}
}
37 changes: 36 additions & 1 deletion src/main/java/safeforhall/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;

import javafx.collections.ObservableList;
import safeforhall.model.event.Event;
import safeforhall.model.event.UniqueEventList;
import safeforhall.model.person.Person;
import safeforhall.model.person.UniquePersonList;

Expand All @@ -15,6 +17,7 @@
public class AddressBook implements ReadOnlyAddressBook {

private final UniquePersonList persons;
private final UniqueEventList events;

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

public AddressBook() {}
Expand All @@ -47,13 +51,22 @@ public void setPersons(List<Person> persons) {
this.persons.setPersons(persons);
}

/**
* Replaces the contents of the event list with {@code events}.
* {@code events} must not contain duplicate events.
*/
public void setEvents(List<Event> events) {
this.events.setEvents(events);
}

/**
* Resets the existing data of this {@code AddressBook} with {@code newData}.
*/
public void resetData(ReadOnlyAddressBook newData) {
requireNonNull(newData);

setPersons(newData.getPersonList());
setEvents(newData.getEventList());
}

//// person-level operations
Expand All @@ -66,6 +79,14 @@ public boolean hasPerson(Person person) {
return persons.contains(person);
}

/**
* Returns true if an event with the same details as {@code event} exists in the address book.
*/
public boolean hasEvent(Event event) {
requireNonNull(event);
return events.contains(event);
}

/**
* Adds a person to the address book.
* The person must not already exist in the address book.
Expand All @@ -74,6 +95,14 @@ public void addPerson(Person p) {
persons.add(p);
}

/**
* Adds an event to the address book.
* The event must not already exist in the address book.
*/
public void addEvent(Event e) {
events.add(e);
}

/**
* Replaces the given person {@code target} in the list with {@code editedPerson}.
* {@code target} must exist in the address book.
Expand Down Expand Up @@ -106,11 +135,17 @@ public ObservableList<Person> getPersonList() {
return persons.asUnmodifiableObservableList();
}

@Override
public ObservableList<Event> getEventList() {
return events.asUnmodifiableObservableList();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddressBook // instanceof handles nulls
&& persons.equals(((AddressBook) other).persons));
&& persons.equals(((AddressBook) other).persons)
&& events.equals(((AddressBook) other).events));
}

@Override
Expand Down
Loading

0 comments on commit fe9d643

Please sign in to comment.