forked from csc301-fall-2015/observer-and-adapter-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStock.java
More file actions
57 lines (41 loc) · 1.13 KB
/
Stock.java
File metadata and controls
57 lines (41 loc) · 1.13 KB
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
package csc301.observerExample;
import java.math.BigDecimal;
/**
* A simple object that represents a stock.
* It has an id (String) and a price (BigDecimal).
*
* Side note: BigDecimal is a Java type suitable currency values.
* It prevents numerical errors.
*/
public class Stock {
private String id;
private BigDecimal price;
public Stock(String id, BigDecimal price) {
if(id == null || id.trim().length() == 0){
throw new IllegalArgumentException("Empty/null identifiers not allowed.");
}
this.id = id;
setPrice(price);
}
public String getId() {
return id;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
if(price.signum() < 0){
throw new IllegalArgumentException("Price must be non-negative.");
}
this.price = price.setScale(2, BigDecimal.ROUND_HALF_UP);
}
@Override
public String toString() {
return String.format("<%s, %s>", this.id, this.price.toString());
}
@Override
public boolean equals(Object o) {
return o instanceof Stock && ((Stock)o).getId().equals(this.id) &&
((Stock)o).getPrice().compareTo(this.price) == 0;
}
}