forked from nus-cs2103-AY1920S1/addressbook-level3
/
XpireItem.java
205 lines (182 loc) · 6.3 KB
/
XpireItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package io.xpire.model.item;
import static io.xpire.model.item.Quantity.DEFAULT_QUANTITY;
import static io.xpire.model.item.ReminderThreshold.DEFAULT_THRESHOLD;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import io.xpire.commons.util.CollectionUtil;
import io.xpire.model.tag.Tag;
import io.xpire.model.tag.TagComparator;
/**
* Represents an item in the expiry date tracker.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class XpireItem extends Item {
// Identity fields
private final ExpiryDate expiryDate;
// Data fields
private Quantity quantity = new Quantity(DEFAULT_QUANTITY);
private ReminderThreshold reminderThreshold = new ReminderThreshold(DEFAULT_THRESHOLD);
/**
* Every field must be present and not null.
* Only called in Tag and Delete commands.
*/
public XpireItem(Name name, ExpiryDate expiryDate, Quantity quantity, Set<Tag> tags) {
super(name, tags);
CollectionUtil.requireAllNonNull(expiryDate);
this.expiryDate = expiryDate;
this.quantity = quantity;
}
/**
* Every field must be present and not null.
* Tags are optional.
*/
public XpireItem(Name name, ExpiryDate expiryDate, Quantity quantity) {
super(name);
CollectionUtil.requireAllNonNull(expiryDate, quantity);
this.expiryDate = expiryDate;
this.quantity = quantity;
}
/**
* Every field must be present and not null.
* Quantity is optional.
*/
public XpireItem(Name name, ExpiryDate expiryDate) {
super(name);
CollectionUtil.requireAllNonNull(expiryDate);
this.expiryDate = expiryDate;
}
/**
* Constructor with all parameters for ItemBuilder class. (Used in testing)
*/
public XpireItem(Name name, ExpiryDate expiryDate, Quantity quantity, Set<Tag> tags,
ReminderThreshold reminderThreshold) {
super(name, tags);
CollectionUtil.requireAllNonNull(expiryDate);
this.expiryDate = expiryDate;
this.quantity = quantity;
this.reminderThreshold = reminderThreshold;
}
public XpireItem(XpireItem xpireItem) {
super(xpireItem);
this.expiryDate = xpireItem.getExpiryDate();
this.quantity = xpireItem.getQuantity();
this.reminderThreshold = xpireItem.getReminderThreshold();
}
public ExpiryDate getExpiryDate() {
return this.expiryDate;
}
public Quantity getQuantity() {
return this.quantity;
}
/**
* Sets and overrides the quantity.
*
* @param newQuantity Quantity to be updated.
*/
public void setQuantity(Quantity newQuantity) {
this.quantity = newQuantity;
}
/**
* Returns the reminder threshold.
*
* @return {@code ReminderThreshold} object.
*/
public ReminderThreshold getReminderThreshold() {
return this.reminderThreshold;
}
/**
* Sets and overrides the reminder threshold.
*
* @param reminderThreshold reminder threshold.
*/
public void setReminderThreshold(ReminderThreshold reminderThreshold) {
this.reminderThreshold = reminderThreshold;
}
public Set<Tag> getNewTagSet(Tag tag) {
Set<Tag> newTagSet = new TreeSet<>(new TagComparator());
newTagSet.addAll(this.getTags());
newTagSet.add(tag);
return newTagSet;
}
/**
* Returns {@Code true} if the item has expired.
*/
public boolean isExpired() {
return this.expiryDate.isExpired();
}
/**
* Returns {@Code true} if the item has a {@Code ReminderThreshold}.
*/
public boolean hasReminderThreshold() {
return !this.reminderThreshold.equals(new ReminderThreshold(DEFAULT_THRESHOLD));
}
/**
* Returns true if both items of the same name have at least one other identity field that is the same.
* This defines a weaker notion of equality between two items.
*/
@Override
public boolean isSameItem(Item otherItem) {
XpireItem other;
try {
other = (XpireItem) otherItem;
} catch (ClassCastException e) {
return false;
}
if (other == this) {
return true;
} else {
return other != null
&& this.name.equals(other.name)
&& this.expiryDate.equals(other.expiryDate);
}
}
//@@author JermyTan
/**
* Returns a new {@code Item} with the name and tags of the current item.
*
* @return New {@code Item}.
*/
public Item remodel() {
return new Item(this.name, this.tags);
}
/**
* Returns true if both items have the same identity and data fields.
* This defines a stronger notion of equality between two items.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (!(obj instanceof XpireItem)) {
return false;
} else {
XpireItem other = (XpireItem) obj;
return super.equals(other)
&& this.expiryDate.equals(other.expiryDate)
&& this.quantity.equals(other.quantity)
&& this.reminderThreshold.equals(other.reminderThreshold);
}
}
@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(super.hashCode(), this.expiryDate, this.quantity, this.reminderThreshold);
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
if (!this.getTags().isEmpty()) {
builder.append(this.name).append("\n")
.append(String.format("Expiry date: %s\n", this.expiryDate.toStringWithCountdown()))
.append(String.format("Quantity: %s\n", this.quantity))
.append("Tags:");
this.getTags().forEach(tag -> builder.append(" " + tag));
} else {
builder.append(this.name).append("\n")
.append(String.format("Expiry date: %s\n", this.expiryDate.toStringWithCountdown()))
.append(String.format("Quantity: %s", this.quantity));
}
return builder.toString();
}
}