Skip to content

Commit

Permalink
Merge pull request #37 from SherwynNg/branch-update-add-command
Browse files Browse the repository at this point in the history
Branch update add command
  • Loading branch information
Xilef121 authored Mar 21, 2024
2 parents be298d3 + cf1b70d commit 846ffa7
Show file tree
Hide file tree
Showing 35 changed files with 226 additions and 187 deletions.
28 changes: 13 additions & 15 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ public class AddCommand extends Command {

public static final String COMMAND_WORD = "add";

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 company to the address book. "
+ "Parameters: "
+ PREFIX_NAME + " NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ PREFIX_TAG + " TAG "
+ PREFIX_EMAIL + " EMAIL "
+ "[" + PREFIX_PHONE + " PHONE]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";

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";
+ PREFIX_NAME + " NUS "
+ PREFIX_TAG + " SoftwareEngineer "
+ PREFIX_EMAIL + " [email protected] "
+ PREFIX_PHONE + " 91234567 ";
public static final String MESSAGE_SUCCESS = "New company added: %1$s";
public static final String MESSAGE_DUPLICATE_COMPANY = "This company already exists in the address book";

private final Company toAdd;

Expand All @@ -49,11 +47,11 @@ public AddCommand(Company company) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public CommandResult execute(Model model) throws CommandException {
Company companyToEdit = lastShownList.get(index.getZeroBased());
Company editedCompany = createEditedCompany(companyToEdit, editCompanyDescriptor);

if (!companyToEdit.isSameCompany(editedCompany) && model.hasPerson(editedCompany)) {
if (!companyToEdit.isSameCompany(editedCompany) && model.hasCompany(editedCompany)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_TAG);

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

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_EMAIL, PREFIX_PHONE);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Phone phone;
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
} else {
phone = new Phone();
}
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static Name parseName(String name) throws ParseException {
public static Phone parsePhone(String phone) throws ParseException {
requireNonNull(phone);
String trimmedPhone = phone.trim();

if (!Phone.isValidPhone(trimmedPhone)) {
throw new ParseException(Phone.MESSAGE_CONSTRAINTS);
}
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import javafx.collections.ObservableList;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.Company;
import seedu.address.model.person.UniquePersonList;
import seedu.address.model.person.UniqueCompanyList;

/**
* Wraps all data at the address-book level
* Duplicates are not allowed (by .isSamePerson comparison)
*/
public class AddressBook implements ReadOnlyAddressBook {

private final UniquePersonList persons;
private final UniqueCompanyList companies;

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

public AddressBook() {}
Expand All @@ -44,8 +44,8 @@ public AddressBook(ReadOnlyAddressBook toBeCopied) {
* Replaces the contents of the person list with {@code persons}.
* {@code persons} must not contain duplicate persons.
*/
public void setPersons(List<Company> companies) {
this.persons.setPersons(companies);
public void setCompanies(List<Company> companies) {
this.companies.setCompany(companies);
}

/**
Expand All @@ -54,58 +54,58 @@ public void setPersons(List<Company> companies) {
public void resetData(ReadOnlyAddressBook newData) {
requireNonNull(newData);

setPersons(newData.getPersonList());
setCompanies(newData.getCompanyList());
}

//// person-level operations

/**
* Returns true if a person with the same identity as {@code person} exists in the address book.
*/
public boolean hasPerson(Company company) {
public boolean hasCompany(Company company) {
requireNonNull(company);
return persons.contains(company);
return companies.contains(company);
}

/**
* Adds a person to the address book.
* The person must not already exist in the address book.
* Adds a company to the address book.
* The company must not already exist in the address book.
*/
public void addPerson(Company p) {
persons.add(p);
public void addCompany(Company p) {
companies.add(p);
}

/**
* Replaces the given person {@code target} in the list with {@code editedPerson}.
* {@code target} must exist in the address book.
* The person identity of {@code editedPerson} must not be the same as another existing person in the address book.
*/
public void setPerson(Company target, Company editedCompany) {
public void setCompany(Company target, Company editedCompany) {
requireNonNull(editedCompany);

persons.setPerson(target, editedCompany);
companies.setPerson(target, editedCompany);
}

/**
* Removes {@code key} from this {@code AddressBook}.
* {@code key} must exist in the address book.
*/
public void removePerson(Company key) {
persons.remove(key);
public void removeCompany(Company key) {
companies.remove(key);
}

//// util methods

@Override
public String toString() {
return new ToStringBuilder(this)
.add("persons", persons)
.add("persons", companies)
.toString();
}

@Override
public ObservableList<Company> getPersonList() {
return persons.asUnmodifiableObservableList();
public ObservableList<Company> getCompanyList() {
return companies.asUnmodifiableObservableList();
}

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

AddressBook otherAddressBook = (AddressBook) other;
return persons.equals(otherAddressBook.persons);
return companies.equals(otherAddressBook.companies);
}
/**
* Sorts the list of companies by their name in ascending order, ignoring case.
*/
public void sortPersonList() {
persons.sort();
companies.sort();
}
@Override
public int hashCode() {
return persons.hashCode();
return companies.hashCode();
}
}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public interface Model {
/**
* Returns true if a person with the same identity as {@code person} exists in the address book.
*/
boolean hasPerson(Company company);
boolean hasCompany(Company company);

/**
* Deletes the given person.
Expand All @@ -67,7 +67,7 @@ public interface Model {
* Adds the given person.
* {@code person} must not already exist in the address book.
*/
void addPerson(Company company);
void addCompany(Company company);

/**
* Replaces the given company {@code target} with {@code editedCompany}.
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs

this.addressBook = new AddressBook(addressBook);
this.userPrefs = new UserPrefs(userPrefs);
filteredCompanies = new FilteredList<>(this.addressBook.getPersonList());
filteredCompanies = new FilteredList<>(this.addressBook.getCompanyList());
}

public ModelManager() {
Expand Down Expand Up @@ -88,27 +88,27 @@ public ReadOnlyAddressBook getAddressBook() {
}

@Override
public boolean hasPerson(Company company) {
public boolean hasCompany(Company company) {
requireNonNull(company);
return addressBook.hasPerson(company);
return addressBook.hasCompany(company);
}

@Override
public void deletePerson(Company target) {
addressBook.removePerson(target);
addressBook.removeCompany(target);
}

@Override
public void addPerson(Company company) {
addressBook.addPerson(company);
public void addCompany(Company company) {
addressBook.addCompany(company);
updateFilteredCompanyList(PREDICATE_SHOW_ALL_PERSONS);
}

@Override
public void setPerson(Company target, Company editedCompany) {
requireAllNonNull(target, editedCompany);

addressBook.setPerson(target, editedCompany);
addressBook.setCompany(target, editedCompany);
}

//=========== Filtered Person List Accessors =============================================================
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/ReadOnlyAddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public interface ReadOnlyAddressBook {
* Returns an unmodifiable view of the persons list.
* This list will not contain any duplicate persons.
*/
ObservableList<Company> getPersonList();
ObservableList<Company> getCompanyList();

}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/person/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public boolean isSameCompany(Company otherCompany) {
}

return otherCompany != null
&& otherCompany.getName().equals(getName());
&& otherCompany.getName().equals(getName())
&& otherCompany.getEmail().equals(getEmail());
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/seedu/address/model/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Phone {
public static final String MESSAGE_CONSTRAINTS =
"Phone numbers should only contain numbers, and it should be at least 3 digits long";
public static final String VALIDATION_REGEX = "\\d{3,}";
public static final String DEFAULT_NUMBER = "000";
public final String value;

/**
Expand All @@ -26,6 +27,13 @@ public Phone(String phone) {
value = phone;
}

/**
* Constructs a {@code Phone}.
*/
public Phone() {
value = DEFAULT_NUMBER;
}

/**
* Returns true if a given string is a valid phone number.
*/
Expand All @@ -35,7 +43,7 @@ public static boolean isValidPhone(String test) {

@Override
public String toString() {
return value;
return value.equals(DEFAULT_NUMBER) ? "NIL" : value;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @see Company#isSameCompany(Company)
*/
public class UniquePersonList implements Iterable<Company> {
public class UniqueCompanyList implements Iterable<Company> {

private final ObservableList<Company> internalList = FXCollections.observableArrayList();
private final ObservableList<Company> internalUnmodifiableList =
Expand Down Expand Up @@ -80,7 +80,7 @@ public void remove(Company toRemove) {
}
}

public void setPersons(UniquePersonList replacement) {
public void setCompany(UniqueCompanyList replacement) {
requireNonNull(replacement);
internalList.setAll(replacement.internalList);
}
Expand All @@ -89,7 +89,7 @@ public void setPersons(UniquePersonList replacement) {
* Replaces the contents of this list with {@code persons}.
* {@code persons} must not contain duplicate persons.
*/
public void setPersons(List<Company> companies) {
public void setCompany(List<Company> companies) {
requireAllNonNull(companies);
if (!companiesAreUnique(companies)) {
throw new DuplicatePersonException();
Expand Down Expand Up @@ -117,12 +117,12 @@ public boolean equals(Object other) {
}

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

UniquePersonList otherUniquePersonList = (UniquePersonList) other;
return internalList.equals(otherUniquePersonList.internalList);
UniqueCompanyList otherUniqueCompanyList = (UniqueCompanyList) other;
return internalList.equals(otherUniqueCompanyList.internalList);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static Company[] getSampleCompanies() {
public static ReadOnlyAddressBook getSampleAddressBook() {
AddressBook sampleAb = new AddressBook();
for (Company sampleCompany : getSampleCompanies()) {
sampleAb.addPerson(sampleCompany);
sampleAb.addCompany(sampleCompany);
}
return sampleAb;
}
Expand Down
Loading

0 comments on commit 846ffa7

Please sign in to comment.