-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakebookOracle.java
More file actions
executable file
·373 lines (315 loc) · 11.2 KB
/
Copy pathFakebookOracle.java
File metadata and controls
executable file
·373 lines (315 loc) · 11.2 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package project2;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.TreeSet;
public abstract class FakebookOracle {
//
// Utility classes for storing result data
//
class UserInfo implements Comparable<UserInfo>{
Long userId;
String firstName;
String lastName;
public UserInfo(Long uid, String fname, String lname){
userId = uid;
firstName = fname;
lastName = lname;
}
public String toString(){
return firstName+" "+lastName+"("+userId+")";
}
public int compareTo(UserInfo arg0) {
return userId.compareTo(arg0.userId);
}
}
class PhotoInfo implements Comparable<PhotoInfo>{
String photoId;
String photoCaption;
String photoLink;
String albumId;
String albumName;
public PhotoInfo(String pid, String aid, String aName, String caption, String link){
photoId = pid;
albumId = aid;
albumName = aName;
photoLink = link;
photoCaption = caption;
}
public String toString(){
return "(ID "+photoId+") from album "+albumName+" url("+photoLink+")";
}
public int compareTo(PhotoInfo arg0) {
return photoId.compareTo(arg0.photoId);
}
}
class TaggedPhotoInfo implements Comparable<TaggedPhotoInfo>{
PhotoInfo photoInfo;
TreeSet<UserInfo> tags;
public TaggedPhotoInfo(PhotoInfo p){
photoInfo = p;
tags = new TreeSet<UserInfo>();
}
public void addTaggedUser(UserInfo u){
tags.add(u);
}
public String toString(){
return photoInfo+"\n"+tags.size()+" Tags\n"+this.tags;
}
public int compareTo(TaggedPhotoInfo t) {
if(this.tags.size() == t.tags.size()){
return this.photoInfo.compareTo(t.photoInfo);
}
return t.tags.size() - this.tags.size();
}
}
class MatchPair implements Comparable<MatchPair>{
Long femaleId;
String femaleFirstName;
String femaleLastName;
int femaleYear;
Long maleId;
String maleFirstName;
String maleLastName;
int maleYear;
TreeSet<PhotoInfo> sharedPhotos;
public MatchPair(Long id1,String fn1, String ln1, int year1,Long id2,String fn2, String ln2, int year2){
femaleId = id1;
femaleFirstName = fn1;
femaleLastName = ln1;
femaleYear = year1;
maleId = id2;
maleFirstName = fn2;
maleLastName = ln2;
maleYear = year2;
sharedPhotos = new TreeSet<PhotoInfo>();
}
public void addSharedPhoto(PhotoInfo p){
sharedPhotos.add(p);
}
public String toString(){
return femaleFirstName+" "+femaleLastName+"("+femaleId+") born in "+femaleYear+", "+maleFirstName+" "+maleLastName+"("+maleId+") born in "+maleYear+
"\nThey are not Fakebook friends and share "+this.sharedPhotos.size()+" photos\n"+this.sharedPhotos;
}
public int compareTo(MatchPair arg0) {
if(this.sharedPhotos.size() != arg0.sharedPhotos.size()){
return arg0.sharedPhotos.size()-this.sharedPhotos.size();
}
if(femaleId == arg0.femaleId){
return maleId.compareTo(arg0.maleId);
}
return femaleId.compareTo(arg0.femaleId);
}
}
class FriendsPair implements Comparable<FriendsPair>{
Long user1Id;
String user1FirstName;
String user1LastName;
Long user2Id;
String user2FirstName;
String user2LastName;
TreeSet<UserInfo> sharedFriends;
public FriendsPair(Long id1, String fn1, String ln1, Long id2, String fn2, String ln2){
if(id1 < id2){
user1Id = id1;
user1FirstName = fn1;
user1LastName = ln1;
user2Id = id2;
user2FirstName = fn2;
user2LastName = ln2;
}
else{
user1Id = id2;
user1FirstName = fn2;
user1LastName = ln2;
user2Id = id1;
user2FirstName = fn1;
user2LastName = ln1;
}
sharedFriends = new TreeSet<UserInfo>();
}
public void addSharedFriend(Long uid, String firstName, String lastName){
sharedFriends.add(new UserInfo(uid, firstName, lastName));
}
public String toString(){
return user1FirstName+" "+user1LastName+"("+user1Id+") "+user2FirstName+" "+user2LastName+"("+user2Id+") are not Fakebook friends and they share "+this.sharedFriends.size()+" friends\n"+
this.sharedFriends;
}
public int compareTo(FriendsPair arg0) {
if(this.sharedFriends.size() != arg0.sharedFriends.size()){
return arg0.sharedFriends.size() - this.sharedFriends.size();
}
if(user1Id == arg0.user1Id){
return user2Id.compareTo(arg0.user2Id);
}
return user1Id.compareTo(arg0.user1Id);
}
}
class SiblingInfo implements Comparable<SiblingInfo>{
Long userId1, userId2;
String firstName1, firstName2;
String lastName1, lastName2;
public SiblingInfo(Long uid1, String fname1, String lname1,
Long uid2, String fname2, String lname2){
userId1 = uid1;
firstName1 = fname1;
lastName1 = lname1;
userId2 = uid2;
firstName2 = fname2;
lastName2 = lname2;
}
public String toString(){
return firstName1+" "+lastName1+"("+userId1+") and "+
firstName2+" "+lastName2+"("+userId2+")";
}
public int compareTo(SiblingInfo s) {
if(userId1.compareTo(s.userId1)==0){
return userId2.compareTo(s.userId2);
}
return userId1.compareTo(s.userId1);
}
}
//
// Result set data structures
//
/*Query 0 data structures*/
protected int totalFriendsWithMonthOfBirth;
protected int monthOfMostFriend;
protected TreeSet<UserInfo> friendsInMonthOfMost;
protected int monthOfLeastFriend;
protected TreeSet<UserInfo> friendsInMonthOfLeast;
public final void printMonthOfBirthInfo(Writer writer) throws IOException{
writer.write(">>>>>>>>>Query0>>>>>>>>>>\n");
writer.write(this.totalFriendsWithMonthOfBirth+ " friends with month of birth information"+"\n");
writer.write(this.friendsInMonthOfMost.size()+ " friends in month of " +monthOfMostFriend+"(most)\n");
writer.write(this.friendsInMonthOfMost+"\n");
writer.write(this.friendsInMonthOfLeast.size()+ " friends in month of " +monthOfLeastFriend+"(least)\n");
writer.write(this.friendsInMonthOfLeast+"\n");
writer.flush();
}
/*Query 1 data structures*/
protected TreeSet<String> longestLastNames;
protected TreeSet<String> shortestLastNames;
protected TreeSet<String> mostCommonLastNames;
protected int mostCommonLastNamesCount;
public final void printNameInfo(Writer writer) throws IOException{
writer.write(">>>>>>>>>Query1>>>>>>>>>>\n");
writer.write("Longest last names are "+this.longestLastNames+"\n");
writer.write("Shortest last names are "+this.shortestLastNames+"\n");
writer.write("Most common last names are "+this.mostCommonLastNames+" - having "+this.mostCommonLastNamesCount+" users for each\n");
writer.flush();
}
/*Query 2 data structures*/
protected TreeSet<UserInfo> popularFriends;
protected int countPopularFriends;
public final void printPopularFriends(Writer writer) throws IOException {
writer.write(">>>>>>>>>Query2>>>>>>>>>>\n");
writer.write("Number of popular friends: " + this.countPopularFriends + "\n");
writer.write("Popular friends are " + this.popularFriends + "\n");
writer.flush();
}
/* Query 3 data structures */
protected TreeSet<UserInfo> liveAtHome;
protected int countLiveAtHome;
public final void printLiveAtHome(Writer writer) throws IOException {
writer.write(">>>>>>>>>Query3>>>>>>>>>>\n");
writer.write("Number of friends who live at home: " + this.countLiveAtHome + "\n");
writer.write("These friends still live at home " + this.liveAtHome + "\n");
writer.flush();
}
/*Query 4 data structures */
protected TreeSet<TaggedPhotoInfo> photosWithMostTags;
public final void printPhotosWithMostTags(Writer writer) throws IOException{
writer.write(">>>>>>>>>Query4>>>>>>>>>>\n");
writer.write("The following are top "+this.photosWithMostTags.size()+" photos with the most tags\n");
for(TaggedPhotoInfo t:this.photosWithMostTags){
writer.write(t+"\n");
}
writer.flush();
}
/*Query 5 data structures*/
protected TreeSet<MatchPair> bestMatches;
public final void printBestMatches(Writer writer) throws IOException{
writer.write(">>>>>>>>>Query5>>>>>>>>>>\n");
writer.write("Top "+this.bestMatches.size()+" matches\n");
for(MatchPair match:this.bestMatches){
writer.write(match+"\n");
}
writer.flush();
}
/*Query 6 data structures*/
protected TreeSet<FriendsPair> suggestedFriendsPairs;
public final void printMutualFriendsInfo(Writer writer) throws IOException {
writer.write(">>>>>>>>>Query6>>>>>>>>>>\n");
for (FriendsPair p : this.suggestedFriendsPairs) {
writer.write(p + "\n");
}
writer.flush();
}
/*Query 7 data structures*/
protected UserInfo oldestFriend;
protected UserInfo youngestFriend;
public final void printAgeInfo(Writer writer) throws IOException {
writer.write(">>>>>>>>>Query7>>>>>>>>>>\n");
writer.write("Oldest friend: " + oldestFriend + "\n");
writer.write("Youngest friend: " + youngestFriend + "\n");
writer.flush();
}
/*Query 8 data structures*/
protected TreeSet<String> popularCityNames;
protected int eventCount;
public final void printCityNames(Writer writer) throws IOException {
writer.write(">>>>>>>>>Query8>>>>>>>>>>\n");
writer.write("City/Cities with most events: " + popularCityNames + "\n");
writer.write("Total events in each city: " + eventCount + "\n");
writer.flush();
}
/*Query 9 data structures*/
protected TreeSet<SiblingInfo> siblings;
public final void printPotentialSiblings(Writer writer) throws IOException {
writer.write(">>>>>>>>>Query9>>>>>>>>>>\n");
writer.write(this.siblings.size()+" pairs of siblings\n");
for (SiblingInfo s : this.siblings) {
writer.write(s + "\n");
}
writer.flush();
}
// Constructor
protected FakebookOracle(){
// Query 0
friendsInMonthOfMost = new TreeSet<UserInfo>();
friendsInMonthOfLeast = new TreeSet<UserInfo>();
// Query 1
longestLastNames = new TreeSet<String>();
shortestLastNames = new TreeSet<String>();
mostCommonLastNames = new TreeSet<String>();
// Query 2
popularFriends = new TreeSet<UserInfo>();
// Query 3
liveAtHome = new TreeSet<UserInfo>();
// Query 4
photosWithMostTags = new TreeSet<TaggedPhotoInfo>();
// Query 5
bestMatches = new TreeSet<MatchPair>();
// Query 6
suggestedFriendsPairs = new TreeSet<FriendsPair>();
// Query 7
oldestFriend = new UserInfo(0L, "","");
youngestFriend = new UserInfo(0L, "", "");
// Query 8
popularCityNames = new TreeSet<String>();
eventCount = 0;
// Query 9
siblings = new TreeSet<SiblingInfo>();
}
public abstract void findMonthOfBirthInfo() throws SQLException;
public abstract void findNameInfo() throws SQLException;
public abstract void popularFriends() throws SQLException;
public abstract void liveAtHome() throws SQLException;
public abstract void findPhotosWithMostTags(int n) throws SQLException;
public abstract void matchMaker(int n, int yearDiff) throws SQLException;
public abstract void suggestFriendsByMutualFriends(int n) throws SQLException;
public abstract void findAgeInfo(Long user_id) throws SQLException;
public abstract void findEventCities() throws SQLException;
public abstract void findPotentialSiblings() throws SQLException;
}