forked from loopccoew/Buffer-4.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
477 lines (395 loc) · 11.3 KB
/
Main.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package spotify_lite;
import java.sql.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
class Song_Node {
String name;
float length;
String artist;
Song_Node prev;
Song_Node next;
int song_id;
Song_Node(String name, String artist, float length, int id) {
this.artist = artist;
this.name = name;
this.length = length;
song_id = id;
}
}
class Playlist {
List<Song_Node> SongList = new ArrayList<>();
String username;
int playlist_size = 0;
Song_Node head = null;
int size = 0;
static int user_id = 0;
Playlist() {
user_id++;
}
void play_with_shuffle() {
// HashMap <String,Integer> map=new HashMap<>();
ArrayList<String> played = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int start = 1;
int end = size;
int c = 1;
int counter = 0;
while (c != 0 && counter <= size) {
Random rand = new Random();
int number = rand.nextInt((end - start) + 1) + start;
Song_Node ptr = head;
for (int i = 0; i < number; i++) {
ptr = ptr.next;
}
if (played.contains(ptr.name)) {
ptr = ptr.next;
c = 1;
} else {
System.out.println("Playing : ");
System.out.println(ptr.name);
played.add(ptr.name);
counter++;
System.out.println("Enter 1 to continue.\nElse enter 0");
c = sc.nextInt();
}
}
}
void addSong(Song_Node newsong) {
// Song_Node newsong = new Song_Node(name, artist, length,id);
if (head == null) {
head = newsong;
} else {
Song_Node ptr = head;
if (head.next == null) {
head.next = newsong;
newsong.prev = head;
newsong.next = head;
head.prev = newsong;
} else {
while (ptr.next != head) {
ptr = ptr.next;
}
ptr.next = newsong;
newsong.next = head;
newsong.prev = ptr;
head.prev = newsong;
}
}
size++;
}
void playsong() {
Song_Node ptr = head.prev;
Song_Node temp = head.prev;
int count = 1;
do {
System.out.println(
count + "\nsong name : " + ptr.name + "\n artist : " + ptr.artist + "\n duration : " + ptr.length);
ptr = ptr.prev;
count++;
} while (ptr.prev != temp.prev);
}
void removesong(String name) {
Song_Node ptr = head;
if (name == head.name) {
head.prev.next = head.next;
head.next.prev = head.prev;
head = head.next;
} else {
while (ptr.name != name) {
ptr = ptr.next;
}
ptr.prev.next = ptr.next;
ptr.next.prev = ptr.prev;
ptr = null;
System.out.println("Song removed.");
}
size--;
}
void match_playlists(Playlist p2) {
HashMap<Integer, String> ht = new HashMap<>(26);
Song_Node ptr = head;
int count = 0;
final DecimalFormat decfor = new DecimalFormat("0.00");
for (int i = 0; i < size && ptr != null; i++) {
ht.put(ptr.song_id, ptr.name);
ptr = ptr.next;
}
Song_Node ptr2 = p2.head;
for (int i = 0; i < p2.size; i++) {
if (ht.containsValue(ptr2.name)) {
count++;
}
ptr2 = ptr2.next;
}
float percent = ((float) count / (float) size) * 100;
System.out.println(decfor.format(percent) + "% playlists match");
}
}
public class Main {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/library1463";
static final String USER = "root";
static final String PASS = "Root$1419";
Connection conn = null;
PreparedStatement pstmt = null;
String query;
Scanner scan = new Scanner(System.in);
Scanner scn = new Scanner(System.in);
void Display_Songs() {
query = "select * from songs";
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery(query);
rs = pstmt.executeQuery(query);
int i = 1;
while (rs.next())
{
int song_id = rs.getInt("song_id");
String song_name = rs.getString("name");
String artist = rs.getString("artist");
float length = rs.getFloat("length");
System.out.println(i);
// System.out.println("Song Name : " + song_name);
//
// System.out.println("Artist : " + artist);
//
// System.out.println("Duration : " + length);
//
// System.out.println("Song ID : "+ song_id);
// System.out.println();
// System.out.println(" --------------------------- Details of Resolved
// Complaint-------------------\n");
System.out.format("%-15s %-30s %-30s %-25s\n", "Song Name ", "Artist ", "Song_id ",
"Song_duration ");
System.out.format("%-15s %-30s %-30s %-25s\n", song_name, artist, song_id, length);
System.out
.println(" -------------------------------------------------------------------------------\n");
i++;
}
rs.close();
pstmt.close();
conn.close();
} // End try block
catch (ClassNotFoundException e) {
System.out.println(" ");
}
catch (SQLException se) {
System.out.println(" ");
} catch (Exception e) {
System.out.println(" ");
}
}
Song_Node search(int id) {
Song_Node node = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
int ID = id;
query = "select * from songs where song_id = ?";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, ID);
ResultSet rs = pstmt.executeQuery();
System.out.println("Query Executed");
if (rs.next()) {
int song_id = rs.getInt("song_id");
String name = rs.getString("name");
String artist = rs.getString("artist");
float length = rs.getFloat("length");
node = new Song_Node(name, artist, length, song_id);
} else {
System.out.println("No such song found.");
node = null;
}
rs.close();
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println(" ");
}
catch (SQLException se) {
System.out.println(" ");
} catch (Exception e) {
System.out.println(" ");
}
return node;
}
public static void main(String[] args) {
Main ob = new Main();
Scanner sc = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
// final ArrayList<Playlist> users = new ArrayList<>();
Playlist obj1 = new Playlist();
Song_Node new_song = ob.search(1035);
obj1.addSong(new_song);
new_song = ob.search(1145);
obj1.addSong(new_song);
new_song = ob.search(1212);
obj1.addSong(new_song);
new_song = ob.search(1003);
obj1.addSong(new_song);
new_song = ob.search(1267);
obj1.addSong(new_song);
new_song = ob.search(1349);
obj1.addSong(new_song);
new_song = ob.search(3234);
obj1.addSong(new_song);
new_song = ob.search(6353);
obj1.addSong(new_song);
new_song = ob.search(2760);
obj1.addSong(new_song);
new_song = ob.search(7782);
obj1.addSong(new_song);
new_song = ob.search(6329);
obj1.addSong(new_song);
Playlist obj2 = new Playlist();
new_song = ob.search(1789);
obj2.addSong(new_song);
new_song = ob.search(2483);
obj2.addSong(new_song);
new_song = ob.search(4263);
obj2.addSong(new_song);
new_song = ob.search(5371);
obj2.addSong(new_song);
new_song = ob.search(9837);
obj2.addSong(new_song);
new_song = ob.search(2166);
obj2.addSong(new_song);
new_song = ob.search(1427);
obj2.addSong(new_song);
new_song = ob.search(1267);
obj2.addSong(new_song);
new_song = ob.search(1349);
obj2.addSong(new_song);
new_song = ob.search(6353);
obj2.addSong(new_song);
new_song = ob.search(3762);
obj2.addSong(new_song);
new_song = ob.search(1632);
obj2.addSong(new_song);
new_song = ob.search(1953);
obj2.addSong(new_song);
new_song = ob.search(4936);
obj2.addSong(new_song);
Playlist obj3 = new Playlist();
new_song = ob.search(5241);
obj3.addSong(new_song);
new_song = ob.search(5631);
obj3.addSong(new_song);
new_song = ob.search(2346);
obj3.addSong(new_song);
new_song = ob.search(2566);
obj3.addSong(new_song);
new_song = ob.search(1273);
obj3.addSong(new_song);
new_song = ob.search(3234);
obj3.addSong(new_song);
new_song = ob.search(3762);
obj3.addSong(new_song);
new_song = ob.search(1069);
obj3.addSong(new_song);
new_song = ob.search(3280);
obj3.addSong(new_song);
new_song = ob.search(1419);
obj3.addSong(new_song);
new_song = ob.search(7623);
obj3.addSong(new_song);
new_song = ob.search(1953);
obj3.addSong(new_song);
new_song = ob.search(5827);
obj3.addSong(new_song);
Playlist obj4 = new Playlist();
new_song = ob.search(2954);
obj4.addSong(new_song);
new_song = ob.search(1632);
obj4.addSong(new_song);
new_song = ob.search(5371);
obj4.addSong(new_song);
new_song = ob.search(6329);
obj4.addSong(new_song);
new_song = ob.search(2760);
obj4.addSong(new_song);
new_song = ob.search(9837);
obj4.addSong(new_song);
new_song = ob.search(3657);
obj4.addSong(new_song);
new_song = ob.search(7623);
obj4.addSong(new_song);
new_song = ob.search(5827);
obj4.addSong(new_song);
new_song = ob.search(7139);
obj4.addSong(new_song);
new_song = ob.search(4936);
obj4.addSong(new_song);
new_song = ob.search(1003);
obj4.addSong(new_song);
new_song = ob.search(1035);
obj4.addSong(new_song);
new_song = ob.search(1010);
obj4.addSong(new_song);
new_song = ob.search(1136);
obj4.addSong(new_song);
Playlist obj6 = new Playlist();
ob.Display_Songs();
System.out.println("MAKE YOUR PLAYLIST NOW !!");
System.out.println("Enter which songs would you like to add in your playlist : ");
int ch = 1;
int id = 0;
while (ch == 1) {
System.out.println("Enter song_id : ");
id = sc.nextInt();
Song_Node new_song1 = ob.search(id);
if (new_song1 != null)
obj6.addSong(new_song1);
System.out.println("Enter 1 to continue adding songs. Else enter 0.");
ch = sc.nextInt();
}
char c = 'y';
while (c == 'y') {
System.out.println(
"Enter 1 to play all the songs serially.\nEnter 2 to shuffle.\nEnter 3 to see how much your playlist matches with other users.");
int choice = 0;
choice = sc.nextInt();
if (choice == 1) {
obj6.playsong();
}
if (choice == 2) {
System.out.println("Shuffle-Play");
System.out.println();
obj6.play_with_shuffle();
}
if (choice == 3) {
System.out.println("List of other users : ");
for (int i = 0; i < 4; i++) {
System.out.println("User " + (i + 1));
}
System.out.println("Enter with which user you want to match your playlist : ");
int uc = sc.nextInt();
if (uc == 1) {
obj6.match_playlists(obj1);
System.out.println("User "+uc+"'s"+" playlist : ");
obj1.playsong();
}
if (uc == 2) {
obj6.match_playlists(obj2);
System.out.println("User "+uc+"'s"+" playlist : ");
obj2.playsong();
}
if (uc == 3) {
obj6.match_playlists(obj3);
System.out.println("User "+uc+"'s"+" playlist : ");
obj3.playsong();
}
if (uc == 4) {
obj6.match_playlists(obj4);
System.out.println("User "+uc+"'s"+" playlist : ");
obj4.playsong();
}
}
System.out.println("Enter 'y' to continue the program. Else enter 'n'.");
c = sc.next().charAt(0);
}
}
}