-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlbumDatabase.java
More file actions
104 lines (95 loc) · 5.92 KB
/
AlbumDatabase.java
File metadata and controls
104 lines (95 loc) · 5.92 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
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class AlbumDatabase {
public static void main(String[] args) {
//Local variables are defined to handle file reading
//The name of the file the program is going to read from is defined as fileName
String fileName = "src/albums.txt";
//the "line" variable allows the buffered reader to keep track of what line it is reading
String line = null;
//These two variables are created to allow for a split method to be used when an Album is found in the file
String albumArtist = null;
String albumTitle = null;
try {
//FileReader object is created and is given the file to read from
FileReader fileReader = new FileReader(fileName);
//BufferedReader object is created and is given the fileReader
BufferedReader bufferedReader = new BufferedReader(fileReader);
//Two ArrayLists are created that allow albums and tracks to be added without the use of a fixed size array
ArrayList<Album> albumList = new ArrayList<>();
ArrayList<Track> trackList = new ArrayList<>();
//Two Arrays are created that act as the final method of storage of Track and Album objects
Track[] tracks;
Album[] albums;
//Start of the file reading process
//A while loop checks that the current line is not empty
while ((line = bufferedReader.readLine()) != null) {
//If the first character of the line is alphabetic then the start of an album has been found
if (Character.isAlphabetic(line.charAt(0))) {
//If the trackList is not empty this means that trackList is already populated with Track objects
//However this means that these Track objects belong to the last read Album
if (trackList.isEmpty() == false) {
//Therefore, the trackList is turned into an Array of Tracks and added to the album
tracks = new Track[trackList.size()];
tracks = trackList.toArray(tracks);
albumList.add(new Album(albumArtist, albumTitle, tracks));
//The trackList and albumTitle/Artist variables are cleared to make way for the new Album
trackList.clear();
albumArtist = null;
albumTitle = null;
}
//The line being read is split by a colon surrounded by spaces into an Array of String objects
String[] albumSplit = line.split(" : ");
//The contents before the colon is stored as albumArtist
albumArtist = albumSplit[0];
//The contents after the colon is stored as albumTitle
albumTitle = albumSplit[1];
}
//If the first character of the line is a digit then the start of a track has been found
if (Character.isDigit(line.charAt(0))) {
//The line is split by a hyphen surrounded by spaces
String[] track = line.split(" - ");
//A for loop can be used to assign variables for a Track object
//This is because a Track object only takes two arguments whereas an Album object takes three
//One of those being an array of Track objects
//Therefore an Album object cannot be created until all Track objects have been created
for (int i = 0; i < track.length - 1; i++) {
//A new Track object is created with contents from the line and added to trackList
trackList.add(new Track(track[i + 1], track[i]));
}
}
}
//This code is necessary as the last album does not pass through the if empty block
tracks = new Track[trackList.size()];
tracks = trackList.toArray(tracks);
//Here the final album object is created
albumList.add(new Album(albumArtist, albumTitle, tracks));
//The final list of albums is sorted alphabetically by implementing the compareTo method found in the Album class
Collections.sort(albumList);
//the ArrayList of album objects is converted into a fixed size Array
albums = new Album[albumList.size()];
albums = albumList.toArray(albums);
//An AlbumCollection object is created, containing all the albums from the text file
AlbumCollection albumCollection = new AlbumCollection(albums);
//The sorted collection of Albums is printed
System.out.println(albumCollection);
//The track with the longest duration is printed using the longestTrack method
System.out.println("\nThe Track With The Longest Duration Is: " + albumCollection.longestTrack());
//The total duration of all Pink Floyd albums is printed with the pinkFloyd method
System.out.println("\nTotal Playtime Of All Pink Floyd Albums: " + albumCollection.pinkFloyd());
//The album with the largest number of tracks is printed with the largestTracks method
System.out.println("\nAlbum With The Greatest Number Of Tracks Is: " + albumCollection.largestTracks());
//The buffered reader object is closed
bufferedReader.close();
}
//Here an exception will be caught if the file that the file reader is using cannot be found
catch (FileNotFoundException ex) {
System.out.println("Unable to open file: " + fileName);
}
//Any IO related exceptions thrown by the file reader will be caught here
catch (IOException ex) {
System.out.println("Error reading file: " + fileName);
}
}
}