forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Quantity.java
117 lines (102 loc) · 3.62 KB
/
Quantity.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
package io.xpire.model.item;
import static java.util.Objects.requireNonNull;
import io.xpire.commons.util.AppUtil;
import io.xpire.commons.util.StringUtil;
import io.xpire.logic.parser.exceptions.ParseException;
/**
* Represents the quantity of an item.
* Users are only allowed to key in positive integers.
* Internally, there can be quantity of value 0.
* Guarantees: immutable and valid in {@link #isValidInputQuantity(String test)}.
*/
public class Quantity {
public static final String DEFAULT_QUANTITY = "1";
public static final String MESSAGE_CONSTRAINTS =
"Quantity added should be a positive integer and should not be blank";
public static final int MAX_VALUE = 100000;
private static final String INTERNAL_MESSAGE_CONSTRAINTS =
"Quantity added should be a non-negative integer and should not be blank";
private int quantity;
/**
* Constructs a {@code Quantity}.
*
* @param quantity A valid input quantity, i.e. positive integer.
*/
public Quantity(String quantity) {
String trimmedQuantity = quantity.trim();
requireNonNull(trimmedQuantity);
AppUtil.checkArgument(isValidInputQuantity(trimmedQuantity), MESSAGE_CONSTRAINTS);
this.quantity = Integer.parseInt(trimmedQuantity);
}
/**
* Constructs a {@code Quantity}.
*
* @param quantity A valid quantity, i.e. a non-negative integer.
*/
public Quantity(String quantity, boolean internalCall) {
requireNonNull(quantity);
String trimmedQuantity = quantity.trim();
if (internalCall) {
AppUtil.checkArgument(isValidQuantity(trimmedQuantity), INTERNAL_MESSAGE_CONSTRAINTS);
this.quantity = Integer.parseInt(trimmedQuantity);
}
}
private Quantity(int quantity) throws ParseException {
if (quantity < 0) {
throw new ParseException(MESSAGE_CONSTRAINTS);
}
this.quantity = quantity;
}
public static boolean isValidQuantity(String test) {
return StringUtil.isNonNegativeInteger(test) && Integer.parseInt(test) <= MAX_VALUE;
}
/**
* Returns true if a given input string is a valid quantity.
*/
/**
* Returns true if a given input string is a valid quantity and lies below the maximum value.
*/
public static boolean isValidInputQuantity(String test) {
return StringUtil.isNonZeroUnsignedInteger(test) && Integer.parseInt(test) <= MAX_VALUE;
}
/**
* Returns true if quantity is zero.
*/
public static boolean quantityIsZero(Quantity quantity) {
return quantity.quantity == 0;
}
public boolean isLessThan(Quantity deductAmount) {
return this.quantity < deductAmount.quantity;
}
/**
* Reduces quantity by specified amount.
*
* @param deductAmount Amount to be deducted.
* @return Quantity to be deducted.
* @throws ParseException if new quantity is negative.
*/
public Quantity deductQuantity(Quantity deductAmount) throws ParseException {
Quantity newQuantity;
newQuantity = new Quantity(this.quantity - deductAmount.quantity);
return newQuantity;
}
@Override
public String toString() {
return "" + this.quantity;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (!(obj instanceof Quantity)) {
return false;
} else {
Quantity other = (Quantity) obj;
return this.quantity == other.quantity;
}
}
@Override
public int hashCode() {
return this.quantity;
}
}