-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsack.java
45 lines (38 loc) · 1.16 KB
/
Knapsack.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
package lab2.knapsack;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.AbstractListModel;
import lab2.shapes.plump.Shape3D;
public class Knapsack extends AbstractListModel<Shape3D> implements Serializable {
private List<Shape3D> content;
private double volume;
public Knapsack(double newVolume) {
volume = newVolume;
content = new ArrayList<Shape3D>();
}
public void put(Shape3D shape) throws KnapsackFullException {
double shapeVolume = shape.getVolume();
if (volume < shapeVolume) {
throw new KnapsackFullException(volume, shapeVolume);
}
volume -= shapeVolume;
int insertionPoint = Collections.binarySearch(content, shape);
if (insertionPoint < 0) {
insertionPoint = ~insertionPoint;
}
content.add(insertionPoint, shape);
fireIntervalAdded(this, insertionPoint, insertionPoint);
}
public void remove(int index) {
content.remove(index);
fireIntervalRemoved(this, index, index);
}
public Shape3D getElementAt(int index) {
return content.get(index);
}
public int getSize() {
return content.size();
}
}