-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryImpl.java
358 lines (341 loc) · 14.6 KB
/
FactoryImpl.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class FactoryImpl implements Factory {
private Holder first;
private Holder last;
private Integer size = 0;
@Override
public void addFirst(Product product) {
Holder holder = new Holder(null, product, null);
if (size == 0) {
// If the list is initially empty,
// the first and last elements should be the same
first = holder;
last = first;
} else {
// Else, add the holder in the beginning
holder.setNextHolder(first);
first.setPreviousHolder(holder);
// Set the 'first' element to holder
first = holder;
}
size++;
}
@Override
public void addLast(Product product) {
Holder holder = new Holder(null, product, null);
if (size == 0) {
// If the list is initially empty,
// the first and last elements should be the same
last = holder;
first = last;
} else {
// Else, add the holder at the end
last.setNextHolder(holder);
holder.setPreviousHolder(last);
// Set the 'last' element to holder
last = holder;
}
size++;
}
@Override
public void add(int index, Product product) throws IndexOutOfBoundsException {
if ((index <= size) && (index >= 0)) {
if (index == 0) {
addFirst(product);
} else if (index == size) {
addLast(product);
} else {
Holder holder = new Holder(null, product, null);
// Start from the 1st index, which is the second
Holder currentHolder = first.getNextHolder();
for (int i = 1; i < size; i++) {
if (i == index) {
Holder currentPrev = currentHolder.getPreviousHolder();
// If we found the index,
// add holder in between next & prev holders
currentPrev.setNextHolder(holder);
holder.setPreviousHolder(currentPrev);
holder.setNextHolder(currentHolder);
currentHolder.setPreviousHolder(holder);
size++;
break;
}
// Else, move to the next Holder
currentHolder = currentHolder.getNextHolder();
}
}
} else {
throw new IndexOutOfBoundsException();
}
}
@Override
public Product removeFirst() throws NoSuchElementException {
if (size != 0) {
if (size == 1) {
// If size is one, empty the list
Product product = first.getProduct();
// Set the properties of first & last to null
first.setProduct(null);
last.setProduct(null);
size--;
printProduct(product.getId(), product.getValue());
return product;
} else {
// Assign firstProduct to the value to be returned
Product firstProduct = first.getProduct();
// Set the second holder to be the first
Holder newFirst = first.getNextHolder();
first = newFirst;
// First holder isn't linked to anything form front,
// so that value must be null
newFirst.setPreviousHolder(null);
size--;
printProduct(firstProduct.getId(), firstProduct.getValue());
return firstProduct;
}
} else {
throw new NoSuchElementException();
}
}
@Override
public Product removeLast() throws NoSuchElementException {
if (size != 0) {
if (size == 1) {
// If size is one, empty the list
Product product = first.getProduct();
// Set the properties of first & last to null
first.setProduct(null);
last.setProduct(null);
size--;
printProduct(product.getId(), product.getValue());
return product;
} else {
// Assign lastProduct to the value to be returned
Product lastProduct = last.getProduct();
// Set the (last-1)th holder to the last
Holder newLast = last.getPreviousHolder();
last = newLast;
// Last holder isn't linked to anything form behind,
// so that value must be null
newLast.setNextHolder(null);
size--;
printProduct(lastProduct.getId(), lastProduct.getValue());
return lastProduct;
}
} else {
throw new NoSuchElementException();
}
}
@Override
public Product removeIndex(int index) throws IndexOutOfBoundsException {
if ((index < size) && (index >= 0)) {
// Start from the 0th index, which is the first
Holder currentHolder = first;
for (int i = 0; i < size; i++) {
if (i == index) { // If we find the index
if (i == 0) {
removeFirst();
} else if (i == size - 1) {
removeLast();
} else {
// link current Holder's prev & next to each other
// and return the Product
Product currentProduct = currentHolder.getProduct();
Holder currentPrev = currentHolder.getPreviousHolder();
Holder currentNext = currentHolder.getNextHolder();
currentPrev.setNextHolder(currentNext);
currentNext.setPreviousHolder(currentPrev);
size--;
printProduct(currentProduct.getId(), currentProduct.getValue());
}
return currentHolder.getProduct();
}
// Else, move to the next Holder and increment i by one
currentHolder = currentHolder.getNextHolder();
}
} // Else the index must be greater than size, so out of bonds
throw new IndexOutOfBoundsException();
}
@Override
public Product removeProduct(int value) throws NoSuchElementException {
if (size != 0) {
// Start from the 0th index, which is the first
Holder currentHolder = first;
for (int i = 0; i < size; i++) {
Product currentProduct = currentHolder.getProduct();
if (currentProduct.getValue() == value) { // If we find the value
if (i == 0) {
removeFirst();
} else if (i == size - 1) {
removeLast();
} else {
// link current Holder's prev & next to each other
// and return the Product
Holder currentPrev = currentHolder.getPreviousHolder();
Holder currentNext = currentHolder.getNextHolder();
currentPrev.setNextHolder(currentNext);
currentNext.setPreviousHolder(currentPrev);
printProduct(currentProduct.getId(), currentProduct.getValue());
size--;
}
return currentHolder.getProduct();
}
// Else, move to the next Holder and increment i by one
currentHolder = currentHolder.getNextHolder();
}
}
throw new NoSuchElementException();
}
@Override
public Product find(int id) throws NoSuchElementException {
if (size != 0) {
// Start from the 0th index, which is the first
Holder currentHolder = first;
for (int i = 0; i < size; i++) {
Product currentProduct = currentHolder.getProduct();
if (currentProduct.getId() == id) {
// If we found the value, return the Product
printProduct(currentProduct.getId(), currentProduct.getValue());
return currentHolder.getProduct();
}
// Else, move to the next Holder and increment i by one
currentHolder = currentHolder.getNextHolder();
}
} // Else the index must be greater than size, so out of bonds
throw new NoSuchElementException();
}
@Override
public Product get(int index) throws IndexOutOfBoundsException {
if ((index < size) && (index >= 0)) {
// Start from the 0th index, which is the first
Holder currentHolder = first;
for (int i = 0; i < size; i++) {
if (i == index) {
// If we found the index, return the Product
printProduct(currentHolder.getProduct().getId(), currentHolder.getProduct().getValue());
return currentHolder.getProduct();
}
// Else, move to the next Holder and increment i by one
currentHolder = currentHolder.getNextHolder();
}
}
throw new IndexOutOfBoundsException();
}
// TODO PRINT
@Override
public Product update(int id, Integer value) throws NoSuchElementException {
if (size != 0) {
// Start from the 0th index, which is the first
Holder currentHolder = first;
for (int i = 0; i < size; i++) {
Product currentProduct = currentHolder.getProduct();
if (currentProduct.getId() == id) {
// If we found the id, return the Product
printProduct(id, currentProduct.getValue());
currentProduct.setValue(value);
return currentHolder.getProduct();
}
// Else, move to the next Holder and increment i by one
currentHolder = currentHolder.getNextHolder();
}
}
throw new NoSuchElementException();
}
@Override
public int filterDuplicates() {
int count = 0;
if (size != 0) {
ArrayList<Integer> values = new ArrayList<>();
// Start from the 0th index, which is the first
Holder currentHolder = first;
for (int i = 0; i < size; i++) {
Product currentProduct = currentHolder.getProduct();
if (!values.contains(currentProduct.getValue())) {
// If the value isn't in the arraylist, add it
values.add(currentProduct.getValue());
} else {
// It cannot be the first element
// If it is the last element
if (i == (size - 1)) {
//removeLast();
// Set the (last-1)th holder to the last
Holder newLast = last.getPreviousHolder();
last = newLast;
// Last holder isn't linked to anything form behind,
// so that value must be null
newLast.setNextHolder(null);
} // If it is in between
else {
//removeProduct(currentProduct.getValue());
Holder currentPrev = currentHolder.getPreviousHolder();
Holder currentNext = currentHolder.getNextHolder();
currentPrev.setNextHolder(currentNext);
currentNext.setPreviousHolder(currentPrev);
}
count++;
size--;
i--;
}
currentHolder = currentHolder.getNextHolder();
}
}
System.out.println(count);
return count;
}
@Override
public void reverse() {
if (size > 1) {
// Start from the 0th index, which is the first
Holder currentHolder = first;
for (int i = 0; i < size; i++) {
if (i == 0) { // For the first element
Holder currentNext = currentHolder.getNextHolder();
// Set next holder to null and previous holder to the former next
currentHolder.setPreviousHolder(currentNext);
currentHolder.setNextHolder(null);
// Now the last element should be the former first
last = currentHolder;
} else if (i == size - 1) { // For the last element
Holder currentPrevious = currentHolder.getPreviousHolder();
// Set next holder to null and previous holder to the former next
currentHolder.setNextHolder(currentPrevious);
currentHolder.setPreviousHolder(null);
// Now the first element should be the former last
first = currentHolder;
} else { // For the elements in between
Holder currentPrevious = currentHolder.getPreviousHolder();
Holder currentNext = currentHolder.getNextHolder();
// Set next holder to the former previous
// and previous holder to the former next
currentHolder.setPreviousHolder(currentNext);
currentHolder.setNextHolder(currentPrevious);
}
// Move backwards
currentHolder = currentHolder.getPreviousHolder();
}
}
print();
}
public void print() {
if (size != 0) {
Holder currentHolder = first;
System.out.print('{');
for (int i = 0; i < size; i++) {
Product currentProduct = currentHolder.getProduct();
System.out.print('(' + Integer.toString(currentProduct.getId()) + ", " + currentProduct.getValue() + ')');
// Print comma if there are more elements
if (currentHolder.getNextHolder() != null) {
System.out.print(',');
}
currentHolder = currentHolder.getNextHolder();
}
System.out.println('}');
} else {
System.out.println("{}");
}
}
public void printProduct(int id, int value) {
System.out.println('(' + Integer.toString(id) + ", " + value + ')');
}
}