forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Item.java
128 lines (107 loc) · 3.37 KB
/
Item.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
package seedu.inventory.model.item;
import static seedu.inventory.commons.util.CollectionUtil.requireAllNonNull;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import seedu.inventory.model.tag.Tag;
/**
* Represents an Item in the inventory.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Item {
// Identity fields
private final Name name;
private final Sku sku;
// Data fields
private final Image image;
private final Price price;
private final Quantity quantity;
private final Set<Tag> tags = new HashSet<>();
/**
* Every field must be present and not null.
*/
public Item(Name name, Price price, Quantity quantity, Sku sku, Image image, Set<Tag> tags) {
requireAllNonNull(name, price, quantity, sku, image, tags);
this.name = name;
this.price = price;
this.quantity = quantity;
this.sku = sku;
this.image = image;
this.tags.addAll(tags);
}
public Name getName() {
return name;
}
public Price getPrice() {
return price;
}
public Quantity getQuantity() {
return quantity;
}
public Sku getSku() {
return sku;
}
public Image getImage() {
return image;
}
/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}
/**
* Returns true if both items have the same SKU.
* This defines a weaker notion of equality between two items.
*/
public boolean isSameItem(Item otherItem) {
if (otherItem == this) {
return true;
}
return otherItem != null
&& otherItem.getSku().equals(getSku());
}
/**
* Returns true if both items have the same SKU and data fields.
* This defines a stronger notion of equality between two items.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Item)) {
return false;
}
Item otherItem = (Item) other;
return otherItem.getName().equals(getName())
&& otherItem.getPrice().equals(getPrice())
&& otherItem.getQuantity().equals(getQuantity())
&& otherItem.getSku().equals(getSku())
&& otherItem.getImage().equals(getImage())
&& otherItem.getTags().equals(getTags());
}
@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, price, quantity, sku, image, tags);
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(getName())
.append(" Price: ")
.append(getPrice())
.append(" Quantity: ")
.append(getQuantity())
.append(" Sku: ")
.append(getSku())
.append(" Image: ")
.append(getImage())
.append(" Tags: ");
getTags().forEach(builder::append);
return builder.toString();
}
}