Skip to content

Commit

Permalink
Merge master into edit-birthdays
Browse files Browse the repository at this point in the history
  • Loading branch information
zhekaiii committed Mar 17, 2024
2 parents 7b4a3d9 + d444e2a commit c62cea7
Show file tree
Hide file tree
Showing 23 changed files with 296 additions and 80 deletions.
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDAY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MONEY_OWED;
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;
Expand All @@ -28,12 +29,14 @@ public class AddCommand extends Command {
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ "[" + PREFIX_BIRTHDAY + "BIRTHDAY]"
+ PREFIX_MONEY_OWED + "AMOUNT_OWED"
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_MONEY_OWED + "24.50 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";

Expand Down
42 changes: 27 additions & 15 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import seedu.address.model.person.Address;
import seedu.address.model.person.Birthday;
import seedu.address.model.person.Email;
import seedu.address.model.person.MoneyOwed;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
Expand Down Expand Up @@ -58,7 +59,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 All @@ -83,9 +84,10 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Remark updatedRemark = personToEdit.getRemark(); // edit command does not allow editing remarks
Birthday updatedBirthday = editPersonDescriptor.getBirthday().orElse(personToEdit.getBirthday());
MoneyOwed updatedMoneyOwed = editPersonDescriptor.getMoneyOwed().orElse(personToEdit.getMoneyOwed());

return new Person(updatedName, updatedPhone, updatedEmail,
updatedAddress, updatedRemark, updatedTags, updatedBirthday);
updatedAddress, updatedRemark, updatedTags, updatedBirthday, updatedMoneyOwed);
}

@Override
Expand Down Expand Up @@ -144,6 +146,7 @@ public static class EditPersonDescriptor {
private Address address;
private Set<Tag> tags;
private Birthday birthday;
private MoneyOwed moneyOwed;

public EditPersonDescriptor() {
}
Expand All @@ -159,6 +162,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setAddress(toCopy.address);
setTags(toCopy.tags);
setBirthday(toCopy.birthday);
setMoneyOwed(toCopy.moneyOwed);
}

public Optional<Birthday> getBirthday() {
Expand All @@ -176,22 +180,22 @@ public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags, birthday);
}

public Optional<Name> getName() {
return Optional.ofNullable(name);
}

public void setName(Name name) {
this.name = name;
}

public Optional<Phone> getPhone() {
return Optional.ofNullable(phone);
public Optional<Name> getName() {
return Optional.ofNullable(name);
}

public void setPhone(Phone phone) {
this.phone = phone;
}

public Optional<Phone> getPhone() {
return Optional.ofNullable(phone);
}

public Optional<Email> getEmail() {
return Optional.ofNullable(email);
}
Expand All @@ -208,13 +212,12 @@ public void setAddress(Address address) {
this.address = address;
}

/**
* 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();
public void setMoneyOwed(MoneyOwed moneyOwed) {
this.moneyOwed = moneyOwed;
}

public Optional<MoneyOwed> getMoneyOwed() {
return Optional.ofNullable(moneyOwed);
}

/**
Expand All @@ -225,6 +228,15 @@ public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}

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

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/RemarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public CommandResult execute(Model model) throws CommandException {

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = new Person(
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(),
personToEdit.getAddress(), remark, personToEdit.getTags(), personToEdit.getBirthday());
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getAddress(),
remark, personToEdit.getTags(), personToEdit.getBirthday(), personToEdit.getMoneyOwed());

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDAY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MONEY_OWED;
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;
Expand All @@ -16,6 +17,7 @@
import seedu.address.model.person.Address;
import seedu.address.model.person.Birthday;
import seedu.address.model.person.Email;
import seedu.address.model.person.MoneyOwed;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
Expand All @@ -27,14 +29,6 @@
*/
public class AddCommandParser implements Parser<AddCommand> {

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

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
Expand All @@ -44,7 +38,7 @@ private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Pre
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG,
PREFIX_BIRTHDAY);
PREFIX_BIRTHDAY, PREFIX_MONEY_OWED);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -59,10 +53,19 @@ public AddCommand parse(String args) throws ParseException {
Remark remark = new Remark(""); // add command does not allow adding remarks straight away
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Birthday birthday = ParserUtil.parseBirthday(argMultimap.getValue(PREFIX_BIRTHDAY).orElse(""));
MoneyOwed moneyOwed = ParserUtil.parseMoneyOwed(argMultimap.getValue(PREFIX_MONEY_OWED).orElse("0"));

Person person = new Person(name, phone, email, address, remark, tagList, birthday);
Person person = new Person(name, phone, email, address, remark, tagList, birthday, moneyOwed);

return new AddCommand(person);
}

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

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class CliSyntax {
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_MONEY_OWED = new Prefix("$/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_REMARK = new Prefix("r/");

public static final Prefix PREFIX_BIRTHDAY = new Prefix("b/");

}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.model.person.Address;
import seedu.address.model.person.Birthday;
import seedu.address.model.person.Email;
import seedu.address.model.person.MoneyOwed;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
Expand Down Expand Up @@ -97,6 +98,21 @@ public static Email parseEmail(String email) throws ParseException {
return new Email(trimmedEmail);
}

/**
* Parses a {@code String moneyOwed} into a {@code MoneyOwed}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code MoneyOwed} is invalid.
*/
public static MoneyOwed parseMoneyOwed(String moneyOwed) throws ParseException {
requireNonNull(moneyOwed);
String trimmedMoneyOwed = moneyOwed.trim();
if (!MoneyOwed.isValidMoney(trimmedMoneyOwed)) {
throw new ParseException(MoneyOwed.MESSAGE_CONSTRAINTS);
}
return new MoneyOwed(trimmedMoneyOwed);
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/seedu/address/model/person/MoneyOwed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package seedu.address.model.person;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents a Person's money owed in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidMoney(String)}
*/
public class MoneyOwed {

public static final String MESSAGE_CONSTRAINTS =
"Money Owed should be at most 2 decimal places in the following format 'xxx.xx' or '-xxx.xx'. ";
public static final String VALIDATION_REGEX = "^(?:-)?\\d+(\\.\\d{0,2})?";

public final Float moneyOwed;

/**
* Constructs a {@code MoneyOwed}.
*
* @param money A valid amount of money owed.
*/
public MoneyOwed(String money) {
requireNonNull(money);
checkArgument(isValidMoney(money), MESSAGE_CONSTRAINTS);
moneyOwed = Float.parseFloat(money);
}

/**
* Returns true if a given string is a valid money amount.
*/
public static boolean isValidMoney(String test) {
if (test == null) {
return true;
}
return test.matches(VALIDATION_REGEX);
}

/**
* Returns true if a moneyOwed is negative.
*/
public boolean isNegativeMoney() {
return (moneyOwed < 0);
}

/**
* Returns message to display on UI in String.
*/
public String getMessage() {
if (moneyOwed == 0) {
return String.format("You don't owe each other anything");
}
if (isNegativeMoney()) {
return String.format("You owe $" + toString().substring(1));
} else {
return String.format("Owes you $" + this);
}
}

@Override
public String toString() {
return String.format("%.2f", moneyOwed);
}

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

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

MoneyOwed otherName = (MoneyOwed) other;
return moneyOwed.equals(otherName.moneyOwed);
}

@Override
public int hashCode() {
return moneyOwed.hashCode();
}
}
14 changes: 11 additions & 3 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public class Person {
private final Remark remark;
private final Set<Tag> tags = new HashSet<>();
private final Birthday birthday;
private final MoneyOwed moneyOwed;

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email,
Address address, Remark remark, Set<Tag> tags, Birthday birthday) {
public Person(Name name, Phone phone, Email email, Address address,
Remark remark, Set<Tag> tags, Birthday birthday, MoneyOwed moneyOwed) {
requireAllNonNull(name, phone, email, address, tags);
this.name = name;
this.phone = phone;
Expand All @@ -40,6 +41,7 @@ public Person(Name name, Phone phone, Email email,
this.remark = remark;
this.tags.addAll(tags);
this.birthday = birthday;
this.moneyOwed = moneyOwed;
}

public Name getName() {
Expand All @@ -54,6 +56,10 @@ public Email getEmail() {
return email;
}

public MoneyOwed getMoneyOwed() {
return moneyOwed;
}

public Address getAddress() {
return address;
}
Expand Down Expand Up @@ -108,7 +114,8 @@ public boolean equals(Object other) {
&& email.equals(otherPerson.email)
&& address.equals(otherPerson.address)
&& tags.equals(otherPerson.tags)
&& birthday.equals(otherPerson.birthday);
&& birthday.equals(otherPerson.birthday)
&& moneyOwed.equals(otherPerson.moneyOwed);
}

@Override
Expand All @@ -127,6 +134,7 @@ public String toString() {
.add("remark", remark)
.add("tags", tags)
.add("birthday", birthday)
.add("moneyOwed", moneyOwed)
.toString();
}

Expand Down
Loading

0 comments on commit c62cea7

Please sign in to comment.