Skip to content

Commit

Permalink
Merge pull request #32 from zekone/adapt-storage
Browse files Browse the repository at this point in the history
Adapt storage
  • Loading branch information
larrywang0701 authored Oct 11, 2023
2 parents 938ff07 + 37fb2c4 commit 81bd968
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 29 deletions.
58 changes: 53 additions & 5 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.event.Event;
import seedu.address.model.note.Note;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -56,7 +58,7 @@ public class EditCommand extends Command {
private final EditPersonDescriptor editPersonDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param index of the person in the filtered person list to edit
* @param editPersonDescriptor details to edit the person with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
Expand Down Expand Up @@ -100,8 +102,11 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
List<Note> updatedNotes = editPersonDescriptor.getNotes().orElse(personToEdit.getNotes());
List<Event> updatedEvents = editPersonDescriptor.getEvents().orElse(personToEdit.getEvents());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, updatedNotes,
updatedEvents);
}

@Override
Expand Down Expand Up @@ -129,7 +134,8 @@ public String toString() {
}

/**
* Stores the details to edit the person with. Each non-empty field value will replace the
* 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 EditPersonDescriptor {
Expand All @@ -138,8 +144,11 @@ public static class EditPersonDescriptor {
private Email email;
private Address address;
private Set<Tag> tags;
private List<Note> notes;
private List<Event> events;

public EditPersonDescriptor() {}
public EditPersonDescriptor() {
}

/**
* Copy constructor.
Expand All @@ -151,6 +160,8 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setEmail(toCopy.email);
setAddress(toCopy.address);
setTags(toCopy.tags);
setNotes(toCopy.notes);
setNotes(toCopy.notes);
}

/**
Expand Down Expand Up @@ -201,14 +212,51 @@ public void setTags(Set<Tag> tags) {
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* 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();
}

/**
* Sets {@code notes} to this object's {@code notes}.
* A defensive copy of {@code notes} is used internally.
*/
public void setNotes(List<Note> notes) {
this.notes = (notes != null) ? notes : null;
}

/**
* Returns an unmodifiable note list, which throws
* {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code notes} is null.
*/
public Optional<List<Note>> getNotes() {
return (notes != null) ? Optional.of(Collections.unmodifiableList(notes)) : Optional.empty();
}

/**
* Sets {@code events} to this object's {@code events}.
* A defensive copy of {@code events} is used internally.
*/
public void setEvents(List<Event> events) {
this.events = (events != null) ? events : null;
}

/**
* Returns an unmodifiable event list, which throws
* {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code notes} is null.
*/
public Optional<List<Event>> getEvents() {
return (events != null) ? Optional.of(Collections.unmodifiableList(events)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

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

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.event.Event;
import seedu.address.model.note.Note;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand All @@ -27,11 +30,12 @@ public class AddCommandParser implements Parser<AddCommand> {
/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -44,14 +48,17 @@ public AddCommand parse(String args) throws ParseException {
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
ArrayList<Note> noteList = new ArrayList<Note>();
ArrayList<Event> eventList = new ArrayList<Event>();

Person person = new Person(name, phone, email, address, tagList);
Person person = new Person(name, phone, email, address, tagList, noteList, eventList);

return new AddCommand(person);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* 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) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/seedu/address/model/event/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.address.model.event;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Event {
private LocalDateTime start;
private LocalDateTime end;
private String name;

public Event(String name, String start, String end) {
// Temporary, need update
this.name = name;
this.start = LocalDateTime.now();
this.end = LocalDateTime.now();
}

public String getName() {
return name;
}

public String getStartString() {
// Temporary, can use Util class instead
return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}

public String getEndString() {
// Temporary, can use Util class instead
return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/model/note/Note.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.address.model.note;

public class Note {
private final String title;
private final String body;

public Note(String title, String body) {
this.title = title;
this.body = body;
}

public String getTitle() {
return title;
}

public String getBody() {
return body;
}
}
37 changes: 33 additions & 4 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.event.Event;
import seedu.address.model.note.Note;
import seedu.address.model.tag.Tag;

/**
* Represents a Person in the address book.
* Guarantees: details are present and not null, field values are validated, immutable.
* Guarantees: details are present and not null, field values are validated,
* immutable.
*/
public class Person {

Expand All @@ -24,17 +29,22 @@ public class Person {
// Data fields
private final Address address;
private final Set<Tag> tags = new HashSet<>();
private final List<Note> notes = new ArrayList<>();
private final List<Event> events = new ArrayList<>();

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
requireAllNonNull(name, phone, email, address, tags);
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags, List<Note> notes,
List<Event> events) {
requireAllNonNull(name, phone, email, address, tags, notes, events);
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.notes.addAll(notes);
this.events.addAll(events);
}

public Name getName() {
Expand All @@ -54,13 +64,32 @@ public Address getAddress() {
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* Returns an immutable tag set, which throws
* {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}

/**
* Returns an immutable notes, which throws
* {@code UnsupportedOperationException}
* if modification is attempted.
*/
public List<Note> getNotes() {
return Collections.unmodifiableList(notes);
}

/**
* Returns an immutable events, which throws
* {@code UnsupportedOperationException}
* if modification is attempted.
*/
public List<Event> getEvents() {
return Collections.unmodifiableList(events);
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
Expand Down
34 changes: 22 additions & 12 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package seedu.address.model.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.event.Event;
import seedu.address.model.note.Note;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand All @@ -18,25 +21,32 @@
*/
public class SampleDataUtil {
public static Person[] getSamplePersons() {

ArrayList<Note> sampleNotes = new ArrayList<Note>();
sampleNotes.add(new Note("Hello", "Sample body"));

ArrayList<Event> sampleEvents = new ArrayList<Event>();
sampleEvents.add(new Event("Sample event", null, null));

return new Person[] {
new Person(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends")),
new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends"), sampleNotes, sampleEvents),
new Person(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends")),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends"), sampleNotes, sampleEvents),
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours")),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours"), sampleNotes, sampleEvents),
new Person(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family")),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family"), sampleNotes, sampleEvents),
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new Address("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates")),
new Address("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates"), sampleNotes, sampleEvents),
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new Address("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"))
new Address("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"), sampleNotes, sampleEvents)
};
}

Expand Down
Loading

0 comments on commit 81bd968

Please sign in to comment.