-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd_shelf.java
580 lines (537 loc) · 16.8 KB
/
cd_shelf.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
// the cd shelf is a list of cds that can be added to and removed from.
public class cd_shelf {
// the array of cds
public cd[] cds;
// the number of cds in the shelf
public int numcds;
// the constructor of the cd shelf
public cd_shelf(int n) {
cds = new cd[n];
numcds = 0;
}
/**
* The add function adds a cd to the array of cds.
*
* @param c Pass the cd object to the add function
* @return Nothing
*/
// the method to add a cd to the shelf
public void add(cd c) {
cds[numcds] = c;
numcds++;
}
/**
* The remove function removes the CD at index i from the array.
*
* @param i Specify which element to remove
* @return The removed object
*/
// the method to remove a cd from the shelf
public cd remove(int i) {
cd c = cds[i];
cds[i] = cds[numcds - 1];
cds[numcds - 1] = null;
numcds--;
return c;
}
/**
* The swap function swaps the values of two elements in an array.
*
* @param pIndex1 Store the index of the first element in the array
* @param int Specify the index of the array to be sorted
* @return Nothing
*/
private void swap(int pIndex1, int pIndex2) {
cd temp2 = cds[pIndex2];
cds[pIndex2] = cds[pIndex1];
cds[pIndex1] = temp2;
}
/**
* The print function prints out the contents of the cds array.
*
* @return The number of cds in the array
*/
// the method to print the shelf
public void print() {
for (int i = 0; i < numcds; i++) {
cds[i].print();
}
}
/**
* The addcd function adds a cd to the list of cds.
*
* @param t Store the title of the cd
* @param String Store the title of the cd
* @param int Specify the number of cds to be added
* @param double Represent the price of a cd
* @param int Specify the number of tracks on a cd
* @return Nothing
*/
public void addcd(String t, String a, int tr, double p, int y) {
try {
cd c = new cd(t, a, tr, p, y);
add(c);
} catch (Exception e) {
System.err.println(e);
}
}
/**
* The search function searches for a CD in the array of CDs.
*
* @param t Search for a title or artist
* @return The index of the cd in the array if it is found, or - 1 otherwise
*/
// the method to search for a cd
public int search(String t) {
for (int i = 0; i < numcds; i++) {
if (cds[i].getTitle().equals(t) || cds[i].getArtist().equals(t)) {
return i;
}
}
return -1;
}
// we can also search for a cd by title, artist, tracks or price.
/**
* The searchArtist function searches through the cds array for a specific artist.
*
* @param a Search for the artist in the array of cd objects
* @return The index of the first occurrence of a given artist in the array
*/
// the method to search for a cd by artist
public int searchArtist(String a) {
for (int i = 0; i < numcds; i++) {
if (cds[i].getArtist().equals(a)) {
return i;
}
}
return -1;
}
/**
* The searchTracks function searches through the cds array for a CD with the given track number.
*
* @param tr Search for a track in the cds array
* @return - 1 if the track is not found
*/
// the method to search for a cd by tracksF
public int searchTracks(int tr) {
for (int i = 0; i < numcds; i++) {
if (cds[i].getTracks() == tr) {
return i;
}
}
return -1;
}
/**
* The searchPrice function searches through the array of CDs to find a CD with a given price.
*
* @param p Search for a price in the array
* @return The index of the cd in cds that has a price equal to p
*/
// the method to search for a cd by price
public int searchPrice(double p) {
for (int i = 0; i < numcds; i++) {
if (cds[i].getPrice() == p) {
return i;
}
}
return -1;
}
// save all the cds in the shelf to a file called cds.txt structured as follows:
/**
* The save function writes the contents of the cds array to a file.
*
* @return Nothing
*/
// title, artist, tracks, price on each line
public void save() throws IOException {
File file = new File("cds.txt");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < numcds; i++) {
bw.write(
cds[i].getTitle()
+ ","
+ cds[i].getArtist()
+ ","
+ cds[i].getTracks()
+ ","
+ cds[i].getPrice()
+ ","
+ cds[i].getYear());
bw.newLine();
}
bw.close();
}
/**
* The load function reads in the data from a file and stores it into an array.
*
* @return A cd array
*/
// replace the cds in the shelf with the cds in the file cds.txt
public void load() throws IOException {
BufferedReader br = new BufferedReader(new java.io.FileReader("cds.txt"));
String line = br.readLine();
cds = new cd[100];
numcds = 0;
while (line != null) {
String[] parts = line.split(",");
add(
new cd(
parts[0],
parts[1],
Integer.parseInt(parts[2]),
Double.parseDouble(parts[3]),
Integer.parseInt(parts[4])));
line = br.readLine();
}
br.close();
}
/**
* The getNumcds function returns the number of CDs in the database.
*
* @return The value of the numcds variable
*/
public int getNumcds() {
return numcds;
}
/**
* The getCds function returns the array of CDs.
*
* @return The array of cds
*/
// getCds returns the cds array
public cd[] getCds() {
return cds;
}
/**
* The removeDuplicatesFromArray function removes duplicate entries from the array of CD objects.
*
* @return The number of unique cds in the array
*/
public void removeDuplicatesFromArray() {
for (int i = 0; i < numcds; i++) {
for (int j = i + 1; j < numcds; j++) {
if (cds[i].equals(cds[j])) {
remove(j);
}
}
}
}
/**
* The removeDuplicatesFromFile function reads in the cds.txt file and removes any duplicate
* entries from the file.
*/
public void removeDuplicatesFromFile() throws IOException {
BufferedReader br = new BufferedReader(new java.io.FileReader("cds.txt"));
String line = br.readLine();
cds = new cd[100];
numcds = 0;
HashMap<String, String> map = new HashMap<String, String>();
while (line != null) {
String[] parts = line.split(",");
String key = parts[0] + parts[1] + parts[2] + parts[3] + parts[4];
if (map.containsKey(key)) {
line = br.readLine();
continue;
} else {
map.put(key, line);
}
line = br.readLine();
}
br.close();
map.clear();
}
public void removeDuplicates() throws IOException {
removeDuplicatesFromFile();
removeDuplicatesFromArray();
}
/**
* The quickSortTitle function sorts the list of CDs by title.
*
* @param low Store the index of the first element in a list
* @param int Define the low index of the list
* @return Nothing
*/
// functions to sort the cds by title using quicksort
public void quickSortTitle(int low, int high) {
int i = low, j = high;
// Get the pivot element from the middle of the list
String pivot = cds[low + (high - low) / 2].getTitle();
// Divide into two lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (cds[i].getTitle().compareTo(pivot) < 0) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (cds[j].getTitle().compareTo(pivot) > 0) {
j--;
}
// If we have found a value in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
// Recursion
if (low < j) quickSortTitle(low, j);
if (i < high) quickSortTitle(i, high);
}
/**
* The quickSortArtist function sorts the cds array using quick sort.
*
* @param low Store the index of the first element in a list
* @param int Store the index of the last element in the left list
* @return Nothing
*/
// functions to sort the cds by artist using quicksort
public void quickSortArtist(int low, int high) {
int i = low, j = high;
// Get the pivot element from the middle of the list
String pivot = cds[low + (high - low) / 2].getArtist();
// Divide into two lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (cds[i].getArtist().compareTo(pivot) < 0) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (cds[j].getArtist().compareTo(pivot) > 0) {
j--;
}
// If we have found a value in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
// Recursion
if (low < j) quickSortArtist(low, j);
if (i < high) quickSortArtist(i, high);
}
/**
* The quickSortTracks function sorts the cds array using quick sort.
*
* @param low Store the index of the first element in a range, and high is used to store the index
* of the last element
* @param int Store the index of the current pivot element
* @return Nothing
*/
// functions to sort the cds by tracks using quicksort
public void quickSortTracks(int low, int high) {
int i = low, j = high;
// Get the pivot element from the middle of the list
int pivot = cds[low + (high - low) / 2].getTracks();
// Divide into two lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (cds[i].getTracks() < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (cds[j].getTracks() > pivot) {
j--;
}
// If we have found a value in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
// Recursion
if (low < j) quickSortTracks(low, j);
if (i < high) quickSortTracks(i, high);
}
/**
* The quickSortPrice function sorts the cds array using quick sort.
*
* @param low Store the index of the first element in a list
* @param int Keep track of the number of comparisons made
* @return Nothing
*/
// functions to sort the cds by price as double using quicksort
public void quickSortPrice(int low, int high) {
int i = low, j = high;
// Get the pivot element from the middle of the list
double pivot = cds[low + (high - low) / 2].getPrice();
// Divide into two lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (cds[i].getPrice() < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (cds[j].getPrice() > pivot) {
j--;
}
// If we have found a value in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
// Recursion
if (low < j) quickSortPrice(low, j);
if (i < high) quickSortPrice(i, high);
}
/**
* The quickSortYear function sorts the list of CDs by year.
*
* @param low Store the index of the first element in a list
* @param int Store the index of the pivot element
* @return Nothing
*/
// quick sort year
public void quickSortYear(int low, int high) {
int i = low, j = high;
// Get the pivot element from the middle of the list
int pivot = cds[low + (high - low) / 2].getYear();
// Divide into two lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (cds[i].getYear() < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (cds[j].getYear() > pivot) {
j--;
}
// If we have found a value in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
// Recursion
if (low < j) quickSortYear(low, j);
if (i < high) quickSortYear(i, high);
}
/**
* The login function checks if the username and password are correct.
*
* @param username Store the username entered by the user
* @param String Store the username and password
* @return A boolean value
*/
public boolean login(String username, String password) {
// use buffered reader to read the file
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("users.txt"));
} catch (FileNotFoundException ex) {
System.err.println(ex);
}
try {
// read the file line by line
String line = br.readLine();
while (line != null) {
// split the line to get the username and password
String[] lineSplit = line.split(",");
// check if the username and password are correct
if (lineSplit[0].equals(username) && lineSplit[1].equals(password)) {
return true;
}
line = br.readLine();
}
} catch (IOException ex) {
System.err.println(ex);
}
return false;
}
// for each line in users.txt
// if username + " " + password is found return true
// skip next line
// if at end of file return false
/**
* The register function takes in a username and password, checks if the username is already
* taken, and writes the new user to users.txt. If successful, it returns true; otherwise it
* returns false.
*
* @param username Store the username entered by the user
* @param String Pass the username to the register function
* @return True if the username is not found in the file
*/
public boolean register(String username, String password) {
// write username,password to users.txt
// return true if successful
// return false if username already exists
// use buffered reader to read the file
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("users.txt"));
} catch (FileNotFoundException ex) {
System.err.println(ex);
}
try {
// read the file line by line
String line = br.readLine();
while (line != null) {
// split the line to get the username and password
String[] lineSplit = line.split(",");
// check if the username and password are correct
if (lineSplit[0].equals(username)) {
return false;
}
line = br.readLine();
}
} catch (IOException ex) {
System.err.println(ex);
}
// if the username is not found in the file
// write the username and password to the file
try {
FileWriter fw = new FileWriter("users.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
out.println(username + "," + password);
out.close();
} catch (IOException ex) {
System.err.println(ex);
}
return true;
}
}