-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.java
131 lines (122 loc) · 4.19 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
129
130
131
/**
* An Item has a name and a positive weight in grammes. Objects of this class
* are immutable: after an object of class Item has been created, one cannot
* change the values of its attributes. Thus, the class Item has no mutators.
*
* @author Carsten Fuhs
*/
public class Item implements Comparable<Item> {
/** The name of the Item. Always non-null after object creation. */
private final String name;
/** The weight of an Item in grammes. Must be greater than 0. */
private final int weightInGrammes;
/**
* Constructs a new Item with given name and weightGrammes.
*
* @param name the name of the Item; must not be null
* @param weightInGrammes the weight of the Item in grammes; must be
* positive
* @throws NullPointerException if name is null
* @throws IllegalArgumentException if weightInGrammes is not positive
*/
public Item(String name, int weightInGrammes) {
if (name == null) {
throw new NullPointerException("name must not be null!");
}
if (weightInGrammes <= 0) {
throw new IllegalArgumentException("Expected weightGrammes > 0, found "
+ weightInGrammes);
}
this.name = name;
this.weightInGrammes = weightInGrammes;
}
/**
* Returns the name of this Item.
*
* @return the name; always non-null
*/
public String getName() {
return this.name;
}
/**
* Returns the weight in grammes of this Item.
*
* @return the weightInGrammes; always greater than 0
*/
public int getWeightInGrammes() {
return this.weightInGrammes;
}
/**
* Returns a String representation of this Item with a "(", the name of
* this Item, ", " (comma and space), the weight in grammes and "g)".
*
* For example, if i is an Item with name "Pen" and a weight of 20 grammes
* the call i.toString() would return "(Pen, 20g)".
*
* @return a human-readable String representation of this Item
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "(" + this.name + ", " + this.weightInGrammes + "g)";
}
/**
* Returns the hash code of this Item. The hash code of an item is
* based on its name and its weight in grammes.
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
// this implementation relies on the name attribute being non-null
return prime * this.name.hashCode() + this.weightInGrammes;
}
/**
* Returns whether this Item and another object are equal. This is the
* case if the other object is also an Item object with equal name and
* the same weight in grammes.
*
* @param obj another object to be compared with
* @return whether this Item and obj are equal
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Item)) {
return false;
}
Item other = (Item) obj;
// this implementation relies on the name attribute being non-null
return this.name.equals(other.name)
&& this.weightInGrammes == other.weightInGrammes;
}
/**
* Compares this Item to o. The comparison considers first the weights
* (higher is greater) and, in case of a tie, the names wrt their
* natural ordering.
*
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*
* @throws NullPointerException if the specified object is null
*/
@Override
public int compareTo(Item o) {
if (this.weightInGrammes > o.weightInGrammes) {
return 1;
}
if (this.weightInGrammes < o.weightInGrammes) {
return -1;
}
// this implementation relies on the name attribute being non-null
return this.name.compareTo(o.name);
}
}