-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicManager.java
More file actions
151 lines (124 loc) · 3.93 KB
/
MusicManager.java
File metadata and controls
151 lines (124 loc) · 3.93 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
// MusicManager.java
import java.util.Scanner;
import java.util.ArrayList;
import java.net.URL;
import java.net.URLConnection;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
public class MusicManager{
private ArtistBucket artistList;
private TitleBucket tracksList;
// Constructor
public MusicManager(){
artistList = new ArtistBucket();
tracksList = new TitleBucket();
}
/* Once all of the music track data is imported and organized in your
application, the application will prompt the user to write the
information in a specific format. The user may enter A or a to write
the Music Library to a file sorted by Artist, T or t to write the
Music Library to a file sorted by Track title, or Q or q to quit the
application.
Your program will continue to prompt the user for the correct input
if the valued entered is incorrect or until the user enters Q or q.
The program will write to a file named artistOutput.txt if the user
wants to sort the music library based on the Artist name.
The program will write to a file named titleOutput.txt if the user
wants to sort the music library based on the Track name.
*/
private MusicTrack createTrack(String s){
// making the track object
Scanner trackAttributes = new Scanner(s);
trackAttributes.useDelimiter(";");
int attributeCounter = 1;
String attribute;
String tempTitle = "";
String tempLength = "";
String tempArtist = "";
String tempAlbum = "";
int tempYear = 0;
String tempType = "";
String tempAdditionalInfo = "";
while(trackAttributes.hasNext()){
attribute = trackAttributes.next();
switch (attributeCounter){
case 1: // set title
tempTitle = attribute;
attributeCounter++;
break;
case 2: // set length
tempLength = attribute;
attributeCounter++;
break;
case 3: // set artist name
tempArtist = attribute;
attributeCounter++;
break;
case 4: // set album
tempAlbum = attribute;
attributeCounter++;
break;
case 5: // set year
tempYear = Integer.parseInt(attribute);
attributeCounter++;
break;
case 6: // set type
tempType = attribute;
attributeCounter++;
break;
case 7: // set additional information
tempAdditionalInfo = attribute;
break;
}
}
if (tempType.equals("D")){
DigitalTrack newTrack = new DigitalTrack(tempTitle, tempLength, tempArtist,
tempAlbum, tempYear, tempAdditionalInfo);
//newTrack.printTrack();
return newTrack;
} else {
VinylTrack newTrack = new VinylTrack( tempTitle, tempLength, tempArtist,
tempAlbum, tempYear, tempAdditionalInfo);
//newTrack.printTrack();
return newTrack;
}
}
// taking the file and reading the input
private void inputFromInfile(String choice){
try{
Scanner infile;
if (choice.toLowerCase().equals("d"))
infile = new Scanner(new File("MusicList.txt"));
else{
URL remoteFileLocation =
new URL("https://sites.cs.ucsb.edu/~richert/cs56/misc/lab04/MusicList.txt");
URLConnection connection = remoteFileLocation.openConnection();
infile = new Scanner(connection.getInputStream());
}
String trackInfo;
while(infile.hasNextLine()){
trackInfo = infile.nextLine();
MusicTrack newTrack = createTrack(trackInfo);
artistList.addItem(newTrack);
tracksList.addItem(newTrack);
//newTrack.printTrack();
}
} catch (IOException e) {
e.toString();
}
}
public void start(){
/* the part that gets the file from the user */
System.out.println("Welcome to the Music Library Application!");
MusicLibraryUI.printFirst();
String choice1 = MusicLibraryUI.getFirstCommand();
this.inputFromInfile(choice1);
// artistList.printArtistBucket();
MusicLibraryUI.printSecond();
MusicLibraryUI.getSecondCommand(artistList, tracksList);
MusicLibraryUI.closeScanner();
}
}