Skip to content

Commit

Permalink
Add testcases and fix storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tingxuanp committed Oct 23, 2024
1 parent 0cea17f commit ced9046
Show file tree
Hide file tree
Showing 28 changed files with 1,091 additions and 48 deletions.
9 changes: 7 additions & 2 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void resetData(ReadOnlyAddressBook newData) {
requireNonNull(newData);

setPersons(newData.getPersonList());
setVendors(newData.getVendorList());
setTags(newData.getTagList());
setWeddings(newData.getWeddingList());
}
Expand Down Expand Up @@ -115,7 +116,6 @@ public void addPerson(Person p) {
*/
public void setPerson(Person target, Person editedPerson) {
requireNonNull(editedPerson);

persons.setPerson(target, editedPerson);
}

Expand Down Expand Up @@ -158,7 +158,7 @@ public void setVendor(Vendor target, Vendor editedVendor) {
* Removes {@code key} from this {@code vendors}.
* {@code key} must exist in the address book.
*/
public void removeVendor(Vendor key) {
public void removeVendor(Person key) {
vendors.remove(key);
}

Expand Down Expand Up @@ -288,6 +288,11 @@ public ObservableList<Person> getPersonList() {
return persons.asUnmodifiableObservableList();
}

@Override
public ObservableList<Vendor> getVendorList() {
return vendors.asUnmodifiableObservableList();
}

@Override
public ObservableList<Tag> getTagList() {
return tags.asUnmodifiableObservableList();
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,10 @@ public interface Model {
void assignVendor(Person person);

/**
* Unassigns the vendor to just be a normal person in the WedLinker.
* Unassigns the vendor to just be a normal person contact in the WedLinker.
*/
void unassignVendor(Person person);

/**
* Deletes vendor from the WedLinker.
*/
void deleteVendor(Person person);

/**
* Replaces the given person {@code target} with {@code editedPerson}.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public boolean hasPerson(Person person) {
@Override
public void deletePerson(Person target) {
addressBook.removePerson(target);
if (addressBook.hasVendor(target)) {
addressBook.removeVendor(target);
}
}

@Override
Expand Down Expand Up @@ -130,13 +133,10 @@ public void assignVendor(Person person) {

@Override
public void unassignVendor(Person person) {

requireNonNull(person);
addressBook.removeVendor(person);
}

@Override
public void deleteVendor(Person person) {

}

@Override
public void addTag(Tag tag) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/ReadOnlyAddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.collections.ObservableList;
import seedu.address.model.person.Person;
import seedu.address.model.person.Vendor;
import seedu.address.model.tag.Tag;
import seedu.address.model.wedding.Wedding;

Expand All @@ -16,6 +17,12 @@ public interface ReadOnlyAddressBook {
*/
ObservableList<Person> getPersonList();

/**
* Returns an unmodifiable view of the vendors list.
* This list will not contain any duplicate vendors.
*/
ObservableList<Vendor> getVendorList();

/**
* Returns an unmodifiable view of the tags list.
* This list will not contain any duplicate tags.
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/seedu/address/model/person/UniqueVendorList.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,25 @@ public boolean contains(Person toCheck) {
return internalList.stream().anyMatch(toCheck::isSamePerson);
}

/**
* Returns the vendor if the vendor list contains that person as a vendor.
*/
public Vendor checkVendor(Person toCheck) {
requireNonNull(toCheck);
return internalList.stream().filter(toCheck::isSamePerson).findFirst().orElse(null);
}

/**
* Adds a person as a vendor to the vendor list.
* The person must not already exist in the vendor list.
*/
public void add(Person toAdd) {
requireNonNull(toAdd);
// create a vendor object representing the person as a vendor
// need to add the task field here
Vendor vendor = new Vendor(toAdd.getName(), toAdd.getPhone(), toAdd.getEmail(),
toAdd.getAddress(), toAdd.getTags(), toAdd.getWeddings());
if (contains(toAdd)) {
throw new DuplicateVendorException();
}
Vendor vendor = new Vendor(toAdd.getName(), toAdd.getPhone(), toAdd.getEmail(),
toAdd.getAddress(), toAdd.getTags(), toAdd.getWeddings());
internalList.add(vendor);
}

Expand Down Expand Up @@ -75,11 +81,12 @@ public void setVendor(Vendor target, Vendor editedVendor) {
* Removes the equivalent vendor from the list.
* The vendor must exist in the list.
*/
public void remove(Vendor toRemove) {
public void remove(Person toRemove) {
requireNonNull(toRemove);
if (!internalList.remove(toRemove)) {
if (!contains(toRemove)) {
throw new VendorNotFoundException();
}
internalList.remove(checkVendor(toRemove));
}

public void setVendors(UniqueVendorList replacement) {
Expand Down
122 changes: 121 additions & 1 deletion src/main/java/seedu/address/storage/JsonAdaptedVendor.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,122 @@
package seedu.address.storage;public class JsonAdaptedVendor {
package seedu.address.storage;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Vendor;
import seedu.address.model.tag.Tag;
import seedu.address.model.wedding.Wedding;

class JsonAdaptedVendor {

public static final String MISSING_FIELD_MESSAGE_FORMAT = "Person's %s field is missing!";

private final String name;
private final String phone;
private final String email;
private final String address;
private final List<JsonAdaptedTag> tags = new ArrayList<>();
private final List<JsonAdaptedWedding> weddings = new ArrayList<>();

/**
* Constructs a {@code JsonAdaptedVendor} with the given vendor details.
*/
@JsonCreator
public JsonAdaptedVendor(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<JsonAdaptedTag> tags,
@JsonProperty("weddings") List<JsonAdaptedWedding> weddings) {
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
if (tags != null) {
this.tags.addAll(tags);
}
if (weddings != null) {
this.weddings.addAll(weddings);
}
}

/**
* Converts a given {@code Vendor} into this class for Jackson use.
*/
public JsonAdaptedVendor(Vendor source) {
name = source.getName().fullName;
phone = source.getPhone().value;
email = source.getEmail().value;
address = source.getAddress().value;
tags.addAll(source.getTags().stream()
.map(JsonAdaptedTag::new)
.collect(Collectors.toList()));
weddings.addAll(source.getWeddings().stream()
.map(JsonAdaptedWedding::new)
.collect(Collectors.toList()));
}

/**
* Converts this Jackson-friendly adapted vendor object into the model's {@code Vendor} object.
*
* @throws IllegalValueException if there were any data constraints violated in the adapted vendor.
*/
public Vendor toModelType() throws IllegalValueException {
final List<Tag> personTags = new ArrayList<>();
final List<Wedding> personWeddings = new ArrayList<>();

for (JsonAdaptedTag tag : tags) {
personTags.add(tag.toModelType());
}

for (JsonAdaptedWedding wedding : weddings) {
if (!Wedding.isValidWeddingName(wedding.getWeddingName())) {
throw new IllegalValueException(Wedding.MESSAGE_CONSTRAINTS);
}
personWeddings.add(wedding.toModelType());
}

if (name == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName()));
}
if (!Name.isValidName(name)) {
throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS);
}
final Name modelName = new Name(name);

if (phone == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName()));
}
if (!Phone.isValidPhone(phone)) {
throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS);
}
final Phone modelPhone = new Phone(phone);

if (email == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Email.class.getSimpleName()));
}
if (!Email.isValidEmail(email)) {
throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS);
}
final Email modelEmail = new Email(email);

if (address == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Address.class.getSimpleName()));
}
final Address modelAddress = new Address(address);

final Set<Tag> modelTags = new HashSet<>(personTags);
final Set<Wedding> modelWeddings = new HashSet<>(personWeddings);

return new Vendor(modelName, modelPhone, modelEmail, modelAddress, modelTags, modelWeddings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.Person;
import seedu.address.model.person.Vendor;
import seedu.address.model.tag.Tag;
import seedu.address.model.wedding.Wedding;

Expand All @@ -23,10 +24,12 @@
class JsonSerializableAddressBook {

public static final String MESSAGE_DUPLICATE_PERSON = "Persons list contains duplicate person(s).";
public static final String MESSAGE_DUPLICATE_VENDOR = "Vendors list contains duplicate vendor(s).";
public static final String MESSAGE_DUPLICATE_TAG = "Tags list contains duplicate tag(s).";
public static final String MESSAGE_DUPLICATE_WEDDING = "Weddings list contains duplicate wedding(s).";

private final List<JsonAdaptedPerson> persons = new ArrayList<>();
private final List<JsonAdaptedVendor> vendors = new ArrayList<>();
private final List<JsonAdaptedTag> tags = new ArrayList<>();
private final List<JsonAdaptedWedding> weddings = new ArrayList<>();

Expand All @@ -36,9 +39,11 @@ class JsonSerializableAddressBook {
@JsonCreator
public JsonSerializableAddressBook(
@JsonProperty("persons") List<JsonAdaptedPerson> persons,
@JsonProperty("vendors") List<JsonAdaptedVendor> vendors,
@JsonProperty("tags") List<JsonAdaptedTag> tags,
@JsonProperty("weddings") List<JsonAdaptedWedding> weddings) {
this.persons.addAll(persons);
this.vendors.addAll(vendors);
this.tags.addAll(tags);
this.weddings.addAll(weddings);
}
Expand All @@ -50,6 +55,7 @@ public JsonSerializableAddressBook(
*/
public JsonSerializableAddressBook(ReadOnlyAddressBook source) {
persons.addAll(source.getPersonList().stream().map(JsonAdaptedPerson::new).collect(Collectors.toList()));
vendors.addAll(source.getVendorList().stream().map(JsonAdaptedVendor::new).collect(Collectors.toList()));
tags.addAll(source.getTagList().stream().map(JsonAdaptedTag::new).collect(Collectors.toList()));
weddings.addAll(source.getWeddingList().stream().map(JsonAdaptedWedding::new).collect(Collectors.toList()));
}
Expand All @@ -68,6 +74,13 @@ public AddressBook toModelType() throws IllegalValueException {
}
addressBook.addPerson(person);
}
for (JsonAdaptedVendor jsonAdaptedVendor : vendors) {
Vendor vendor = jsonAdaptedVendor.toModelType();
if (addressBook.hasVendor(vendor)) {
throw new IllegalValueException(MESSAGE_DUPLICATE_VENDOR);
}
addressBook.addVendor(vendor);
}
for (JsonAdaptedTag jsonAdaptedTag : tags) {
Tag tag = jsonAdaptedTag.toModelType();
if (addressBook.hasTag(tag)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"email": "[email protected]",
"address": "4th street"
} ],
"vendors": [],
"tags": ["florist"],
"weddings": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"address": "123, Jurong West Ave 6, #08-111",
"tags": [ "friends" ]
}],
"vendors": [],
"tags": ["florist", "chef", "florist"],
"weddings": ["Wedding July 2026"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"address": "123, Jurong West Ave 6, #08-111",
"tags": [ "friends" ]
}],
"vendors": [],
"tags": ["florist", "chef"],
"weddings": ["Wedding July 2026", "Wedding 1", "Wedding July 2026", "Paula's Wedding"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"email": "invalid@email!3e",
"address": "4th street"
} ],
"vendors": [],
"tags": ["florist"],
"weddings": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"email": "[email protected]",
"address": "4th street"
} ],
"vendors": [],
"tags": ["florist", " "],
"weddings": ["Wedding 19"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"email": "[email protected]",
"address": "4th street"
} ],
"vendors": [],
"tags": ["florist", "musician"],
"weddings": ["Wedding 19", " "]
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"address" : "4th street",
"tags" : [ ]
} ],
"vendors": [],
"tags": ["florist"],
"weddings": ["Wedding 2", "Carla's Wedding"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,6 @@ public void unassignVendor(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deleteVendor(Person person) {
throw new AssertionError("This method should not be called.");
}

}

/**
Expand Down
Loading

0 comments on commit ced9046

Please sign in to comment.