-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlbumCollectionManager.java
More file actions
89 lines (67 loc) · 2.71 KB
/
AlbumCollectionManager.java
File metadata and controls
89 lines (67 loc) · 2.71 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
package albumcollection;
import albumcollection.album.Album;
import albumcollection.album.CassetteTape;
import albumcollection.album.CompactDisc;
import albumcollection.album.MusicGenre;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class AlbumCollectionManager {
private static final String DATABASE_FILE_NAME = "database.dat";
private static ArrayList<Album> albumCollection = new ArrayList<Album>();
public static Album createAlbumFrom(String type, String title, String artist, int year, String genre) {
switch (type) {
case "Compact Disc":
return new CompactDisc(title, artist, year, MusicGenre.fromString(genre));
case "Cassette Tape":
return new CassetteTape(title, artist, year, MusicGenre.fromString(genre), 1800.0);
default:
return null;
}
}
public static ArrayList<Album> getAlbumCollection() {
return (ArrayList<Album>) albumCollection.clone();
}
public static boolean isAlbumCollectionEmpty() {
return albumCollection.isEmpty();
}
public static void addAlbumToCollection(Album album) {
albumCollection.add(album);
albumCollection.sort(Album::compareTo);
}
public static void removeAlbumFromCollection(Album album) {
albumCollection.remove(album);
albumCollection.sort(Album::compareTo);
}
public static ArrayList<Album> albumsMatchingSearchTerm(String searchTerm) {
ArrayList<Album> results = new ArrayList<Album>();
for (Album album : albumCollection) {
String searchTermLowerCase = searchTerm.toLowerCase();
String artistLowerCase = album.getArtist().toLowerCase();
String titleLowerCase = album.getTitle().toLowerCase();
if (artistLowerCase.contains(searchTermLowerCase) || titleLowerCase.contains(searchTermLowerCase)) {
results.add(album);
}
}
return results;
}
public static void loadAlbumCollectionFromDatabase() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(DATABASE_FILE_NAME));
albumCollection = (ArrayList<Album>) in.readObject();
in.close();
albumCollection.sort(Album::compareTo);
} catch (Exception ex) {
System.out.println(ex);
}
}
public static void saveAlbumCollectionToDatabase() {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(DATABASE_FILE_NAME));
out.writeObject(albumCollection);
out.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
}