-
Notifications
You must be signed in to change notification settings - Fork 0
/
Album.java
107 lines (89 loc) · 3.21 KB
/
Album.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
import java.util.Arrays;
public class Album {
String name, condition;
PhotoManager manager;
int numberOfComparisons = 0;
LinkedList<Photo> photosList;
// Constructor
public Album(String name, String condition, PhotoManager manager) {
this.name = name;
this.condition = condition;
this.manager = manager;
photosList = new LinkedList<>();
numberOfComparisons = 1;
}
// Return the name of the album
public String getName() {
return name;
}
// Return the condition associated with the album
public String getCondition() {
return condition;
}
// Return the manager
public PhotoManager getManager() {
return manager;
}
// Return all photos that satisfy the album condition
public LinkedList<Photo> getPhotos() {
photosList = new LinkedList<>();
BST<LinkedList<Photo>> bst = manager.getPhotos();
if (condition == null || condition.length() == 0) {
getAllPhotos(bst.getRoot());
} else {
String[] conditions = condition.trim().split(" AND ");
for (String s : conditions) {
if (s.length() != 0 && !bst.findKey(s)) {
return new LinkedList<>();
}
if (s.length() != 0 && bst.findKey(s)) {
numberOfComparisons += bst.getNumberOfComparisons(s);
LinkedList<Photo> photos = bst.retrieve();
photos.findFirst();
while (photos.isThereNext()) {
Photo photo = photos.retrieve();
LinkedList<String> tagsList = photo.getTags();
tagsList.findFirst();
int counter = 0;
while (tagsList.isThereNext()) {
for (String c : conditions) {
if (c.length() != 0 && tagsList.retrieve().trim().equals(c)) {
counter++;
}
}
if (!photosList.find(photo) && conditions.length == counter)
photosList.insert(photo);
tagsList.findNext();
}
photos.findNext();
}
}
}
}
return photosList;
}
public void getAllPhotos(BSTNode<LinkedList<Photo>> node) {
if (node == null)
return;
numberOfComparisons++;
LinkedList<Photo> photos = node.data;
photos.findFirst();
while (photos.isThereNext()) {
Photo photo = photos.retrieve();
if (!photosList.find(photo)) {
photosList.insert(photo);
}
photos.findNext();
}
// first recur on left subtree
getAllPhotos(node.left);
// then recur on right subtree
getAllPhotos(node.right);
}
// Return the number of tag comparisons used to find all photos of the album
public int getNbComps() {
numberOfComparisons = 0;
getPhotos();
return numberOfComparisons;
}
}