-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWarehouse.java
More file actions
259 lines (235 loc) · 5.15 KB
/
Copy pathWarehouse.java
File metadata and controls
259 lines (235 loc) · 5.15 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
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
package assignment2;
public class Warehouse{
protected Shelf[] storage;
protected int nbShelves;
public Box toShip;
public UrgentBox toShipUrgently;
static String problem = "problem encountered while performing the operation";
static String noProblem = "operation was successfully carried out";
public Warehouse(int n, int[] heights, int[] lengths){
this.nbShelves = n;
this.storage = new Shelf[n];
for (int i = 0; i < n; i++){
this.storage[i]= new Shelf(heights[i], lengths[i]);
}
this.toShip = null;
this.toShipUrgently = null;
}
public String printShipping(){
Box b = toShip;
String result = "not urgent : ";
while(b != null){
result += b.id + ", ";
b = b.next;
}
result += "\n" + "should be already gone : ";
b = toShipUrgently;
while(b != null){
result += b.id + ", ";
b = b.next;
}
result += "\n";
return result;
}
public String print(){
String result = "";
for (int i = 0; i < nbShelves; i++){
result += i + "-th shelf " + storage[i].print();
}
return result;
}
public void clear(){
toShip = null;
toShipUrgently = null;
for (int i = 0; i < nbShelves ; i++){
storage[i].clear();
}
}
/**
* initiate the merge sort algorithm
*/
public void sort(){
mergeSort(0, nbShelves -1);
}
/**
* performs the induction step of the merge sort algorithm
*/
protected void mergeSort(int start, int end){
if(start<end)
{
int mid = (start+end)/2;
mergeSort(start, mid);
mergeSort(mid+1, end);
merge(start, mid, end);
}
}
/**
* performs the merge part of the merge sort algorithm
*/
protected void merge(int start, int mid, int end){
int n1 = mid -start+1;
int n2 = end-mid;
Shelf[] L = new Shelf[n1];
Shelf[] R = new Shelf[n2];
for (int i=0; i<n1; i++)
{
L[i] = storage[start+i];
}
for (int j=0; j<n2; j++)
{
R[j] = storage[mid+1+j];
}
int i=0;
int j=0;
int k=start;
while((i<n1)&&(j<n2))
{
if (L[i].height<=R[j].height)
{
storage[k] = L[i];
i++;
}
else
{
storage[k] = R[j];
j++;
}
k++;
}
//copy the remaining elements in two arrays
while(i<n1)
{
storage[k] = L[i];
i++;
k++;
}
while(j<n2)
{
storage[k] = R[j];
j++;
k++;
}
}
/**
* Adds a box is the smallest possible shelf where there is room available.
* Here we assume that there is at least one shelf (i.e. nbShelves >0)
*/
public String addBox (Box b){
for (int i=0; i<nbShelves; i++)
{
if (storage[i].height>=b.height) //since the shelves are already sorted, this is the optimal height
{
if (storage[i].availableLength>=b.length)
{
storage[i].addBox(b);
return noProblem;
}
}
}
return problem;
}
/**
* Adds a box to its corresponding shipping list and updates all the fields
*/
public String addToShip (Box b){
try {
if(b instanceof UrgentBox)
{
if(toShipUrgently == null)
{
toShipUrgently = (UrgentBox)b;
toShipUrgently.next = null;
return noProblem;
}
if(toShipUrgently != null)
{
Box n = toShipUrgently;
toShipUrgently = (UrgentBox)b;
toShipUrgently.next = n;
toShipUrgently.next.next = null;
return noProblem;
}
}
else
{
if(toShip == null)
{
toShip = b;
toShip.next = null;
return noProblem;
}
if(toShip != null)
{
Box n = toShip;
toShip = b;
toShip.next = n;
toShip.next.next = null;
return noProblem;
}
}
} catch (Exception e) {
return problem;
}
return problem;
}
/**
* Find a box with the identifier (if it exists)
* Remove the box from its corresponding shelf
* Add it to its corresponding shipping list
*/
public String shipBox (String identifier){
for(int i=0; i<nbShelves; i++)
{
boolean b = (storage[i].availableLength == storage[i].totalLength);
if(b==false)
{
Box n = storage[i].firstBox;
while (n!= null)
{
if((n.id).equals(identifier))
{
addToShip(n);
storage[i].removeBox(identifier);
return noProblem;
}
n = n.next;
}
}
}
return problem;
}
/**
* if there is a better shelf for the box, moves the box to the optimal shelf.
* If there are none, do not do anything
*/
public void moveOneBox (Box b, int position){
int h = storage[position].height;
for(int i=0; i<nbShelves; i++)
{
if((storage[i].height>=b.height)&&(storage[i].height<h))
{
if(storage[i].availableLength>=b.length)
{
// b.next = null;
storage[position].removeBox(b.id);
storage[i].addBox(b);
return;
}
}
}
}
/**
* reorganize the entire warehouse : start with smaller shelves and first box on each shelf.
*/
public void reorganize (){
for(int i=0; i<nbShelves; i++)
{
Box n = storage[i].firstBox;
while(n != null)
{
moveOneBox(n, i);
n = n.next;
}
}
}
}