-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.java
More file actions
152 lines (129 loc) · 4.49 KB
/
Order.java
File metadata and controls
152 lines (129 loc) · 4.49 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
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
package tradable;
import book.BookSide;
import price.Price;
public class Order implements Tradable {
private String user; //3 letter code no spaces, no numbers, no special characters
private String product; /*The stock symbol for the order, must be from 1 to 5 letters/numbers,
they can also have a period “.” In the symbol. Otherwise, -no spaces,
no special characters (i */
private Price price; //cannot be null
private BookSide side; //either BUY or SELL (cannot be null)
private String id; //user + product + price (String) + nanotime (from System.nanoTime()).
private int originalVolume; //must be greater than 0, less than 10000
private int remainingVolume; //initialized as originalVolume but is changable
private int cancelledVolume; //initilized to 0 but changable when cancelled
private int filledVolume; //initalized to 0 but changeable when filled
public Order(String user, String product, Price price, int originalVolume, BookSide side) throws InvalidInput {
setUser(user);
setProduct(product);
setPrice(price);
setSide(side);
setOriginalVolume(originalVolume);
setRemainingVolume(originalVolume);
setCancelledVolume(0);
setFilledVolume(0);
this.id = user + product + price.toString() + System.nanoTime();
}
private void setUser(String user) throws InvalidInput {
if(user.length() != 3) {
throw new InvalidInput("Invalid username length");
}
for(char c: user.toCharArray()) {
if(!Character.isLetter(c)) {
throw new InvalidInput("Invalid username character");
}
}
this.user = user;
}
private void setProduct(String product) throws InvalidInput {
if(product.length() < 1 || product.length() > 5) {
throw new InvalidInput("Invalid product length");
}
for(char c: product.toCharArray()) {
if(!Character.isLetter(c) || c == '.') {
throw new InvalidInput("Invalid product character");
}
}
this.product = product;
}
private void setPrice(Price price) throws InvalidInput {
if(price == null) {
throw new InvalidInput("Invalid price (null)");
}
this.price = price;
}
private void setSide(BookSide side) throws InvalidInput {
if(side == null) {
throw new InvalidInput("Invalid side (null)");
}
if(side != BookSide.BUY && side != BookSide.SELL) {
throw new InvalidInput("Invalid side");
}
this.side = side;
}
private void setOriginalVolume(int volume) throws InvalidInput {
if(volume < 0 || volume > 10000) {
throw new InvalidInput("Invalid volume");
}
this.originalVolume = volume;
}
@Override
public String toString() {
return user + " order: " + side + " " + product + " at " + price + ", Orig Vol: " + originalVolume
+ ", Rem Vol: " + remainingVolume + ", Fill Vol: " + filledVolume + ", CXL Vol: " + cancelledVolume
+ ", ID: " + id;
}
@Override
public String getId() {
return this.id;
}
@Override
public int getCancelledVolume() {
return this.cancelledVolume;
}
@Override
public void setCancelledVolume(int newVol) {
this.cancelledVolume = newVol;
}
@Override
public int getRemainingVolume() {
return this.remainingVolume;
}
@Override
public void setRemainingVolume(int newVol) {
this.remainingVolume = newVol;
}
@Override
public TradableDTO makeTradableDTO() {
return new TradableDTO(this.user, this.product, this.price, this.side, this.id,
this.originalVolume, this.remainingVolume, this.cancelledVolume, this.filledVolume);
}
@Override
public Price getPrice() {
return this.price;
}
@Override
public void setFilledVolume(int newVol) {
this.filledVolume = newVol;
}
@Override
public int getFilledVolume() {
return this.filledVolume;
}
@Override
public BookSide getSide() {
return this.side;
}
@Override
public String getUser() {
return this.user;
}
@Override
public String getProduct() {
return this.product;
}
@Override
public int getOriginalVolume() {
return this.originalVolume;
}
}