|
| 1 | +package seedu.address.model.person; |
| 2 | + |
| 3 | +import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; |
| 4 | + |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +public class Expense { |
| 8 | + |
| 9 | + // Identity fields |
| 10 | + private final Amount amount; |
| 11 | + private final Date date; |
| 12 | + private final Category category; |
| 13 | + private final Description description; |
| 14 | + |
| 15 | + /** |
| 16 | + * Every field must be present and not null. |
| 17 | + */ |
| 18 | + public Expense(Amount amount, Date date, Category category, Description description) { |
| 19 | + requireAllNonNull(amount, date, category); |
| 20 | + this.amount = amount; |
| 21 | + this.date = date; |
| 22 | + this.category = category; |
| 23 | + this.description = description; |
| 24 | + } |
| 25 | + |
| 26 | + public Amount getAmount() { |
| 27 | + return this.amount; |
| 28 | + } |
| 29 | + |
| 30 | + public Date getDate() { |
| 31 | + return this.date; |
| 32 | + } |
| 33 | + |
| 34 | + public Category getCategory() { |
| 35 | + return this.category; |
| 36 | + } |
| 37 | + |
| 38 | + public Description getDescription() { |
| 39 | + return this.description; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns true if both persons of the same name have at least one other identity field that is the same. |
| 45 | + * This defines a weaker notion of equality between two persons. |
| 46 | + */ |
| 47 | + public boolean isSameExpense(Expense otherExpense) { |
| 48 | + if (otherExpense == this) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + return otherExpense != null |
| 53 | + && otherExpense.getAmount().equals(this.getAmount()) |
| 54 | + && otherExpense.getDate().equals(this.getDate()) |
| 55 | + && otherExpense.getCategory().equals(this.getCategory()) |
| 56 | + && otherExpense.getDescription().equals(this.getDescription()); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns true if both persons have the same identity and data fields. |
| 61 | + * This defines a stronger notion of equality between two persons. |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public boolean equals(Object other) { |
| 65 | + if (other == this) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + if (!(other instanceof Expense)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + Expense otherExpense = (Expense) other; |
| 74 | + return otherExpense.getAmount().equals(getAmount()) |
| 75 | + && otherExpense.getDate().equals(getDate()) |
| 76 | + && otherExpense.getCategory().equals(getCategory()) |
| 77 | + && otherExpense.getDescription().equals(getDescription()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int hashCode() { |
| 82 | + // use this method for custom fields hashing instead of implementing your own |
| 83 | + return Objects.hash(amount, date, category, description); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String toString() { |
| 88 | + final StringBuilder builder = new StringBuilder(); |
| 89 | + |
| 90 | + if (description.isEmpty()) { |
| 91 | + builder.append(" Amount: ") |
| 92 | + .append(this.getAmount()) |
| 93 | + .append(" Date: ") |
| 94 | + .append(this.getDate()) |
| 95 | + .append(" Category: ") |
| 96 | + .append(this.getCategory()); |
| 97 | + return builder.toString(); |
| 98 | + } else { |
| 99 | + builder.append(" Amount: ") |
| 100 | + .append(this.getAmount()) |
| 101 | + .append(" Date: ") |
| 102 | + .append(this.getDate()) |
| 103 | + .append(" Category: ") |
| 104 | + .append(this.getCategory()) |
| 105 | + .append(" Description: ") |
| 106 | + .append(this.getDescription()); |
| 107 | + return builder.toString(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments