aboutsummaryrefslogtreecommitdiff
path: root/jMusicHub.java
blob: 799c7154bc70021e8fb2dfeb2dd478db6f2059dc (plain)
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
import java.util.*;
import java.io.*;

/** <h1>jMusicHub</h1>
 *
 * The jMusicHub class is basically the app.
 * It is used to launch the whole process.
 *
 * @author  Aimeric ADJUTOR
 * @version 1.0
 * @since   2020-11-13
 * */

public class jMusicHub {

	/** <h2>addSong</h2>
	 * add Song is used to add songs thanks to the "c" option
	 * @param scan Scanner Object
	 */
	public static Song addSong(Scanner scan){
		System.out.println("Adding a song...");
		System.out.printf("Title : ");
		String title = scan.nextLine();
		System.out.printf("Duration (in seconds) : ");
		int duration = scan.nextInt();
		System.out.printf("Content path : ");
		String trash = scan.nextLine(); //Using this because the content scan is skip after a nexInt
		String content = scan.nextLine();
		System.out.printf("Artist : ");
		String artist = scan.nextLine();
		System.out.printf("Genre (JAZZ, CLASSICAL, HIPHOP, ROCK, POP, RAP) : ");
		String choosedGenre = scan.nextLine();
		choosedGenre = choosedGenre.toUpperCase();
		Genre genre = Genre.valueOf(choosedGenre);
		System.out.println("");

		System.out.println("Do you confirm the addition of the following song ?");
		System.out.println("Title : " + title);
		System.out.println("Duration : " + duration);
		System.out.println("Content path : " + content);
		System.out.println("Artist : " + artist);
		System.out.println("Genre : " + genre);
		System.out.println("[Y/n]");
		String confirm = scan.nextLine();

		if (confirm.equalsIgnoreCase("Y")){ //if the user is ok with what he typed, create a song obj
			Song newSong = new Song(title, duration, content, artist,genre);
			return newSong;
		} else {
			System.out.println("Aborting...");
			System.out.println("");
			return null;
		}
	}

/** <h2>addAudioBook</h2>
	 * add Song is used to add songs thanks to the "c" option
	 * @param scan Scanner Object
	 */
	public static AudioBook addAudioBook(Scanner scan){
		System.out.println("Adding an audiobook...");
		System.out.printf("Title : ");
		String title = scan.nextLine();
		System.out.printf("Duration (in seconds) : ");
		int duration = scan.nextInt();
		System.out.printf("Content path : ");
		String trash = scan.nextLine(); //Using this because the content scan is skip after a nexInt
		String content = scan.nextLine();
		System.out.printf("Author : ");
		String author = scan.nextLine();
		System.out.printf("Language (FRENCH, ENGLISH, ITALIAN, SPANISH, GERMAN) : ");
		String choosedLanguage = scan.nextLine();
		choosedLanguage = choosedLanguage.toUpperCase();
		Language language = Language.valueOf(choosedLanguage);
		System.out.printf("Category (TEEN, NOVEL, THEATER, SPEECH, DOCUMENTARY) : ");
		String choosedCategory = scan.nextLine();
		choosedCategory = choosedCategory.toUpperCase();
		Category category = Category.valueOf(choosedCategory);
		System.out.println("");

		System.out.println("Do you confirm the addition of the following song ?");
		System.out.println("Title : " + title);
		System.out.println("Duration : " + duration);
		System.out.println("Content path : " + content);
		System.out.println("Author : " + author);
		System.out.println("Language : " + language);
		System.out.println("Category : " + category);
		System.out.println("[Y/n]");
		String confirm = scan.nextLine();

		if (confirm.equalsIgnoreCase("Y")){ //if the user is ok with what he typed, create a audiobook obj
			AudioBook newAudioBook = new AudioBook(title, duration, content, author, language, category);
			return newAudioBook;
		} else {
			System.out.println("Aborting...");
			System.out.println("");
			return null;
		}
	}

/**<h2>saveSongs</h2>
 * saveSongs is used by the command "s"
 * @param songs ArrayList<Song> Object
 * */
	public static void saveSongs(ArrayList<Song> songs){

		String filename = "songs";

		// Serialization
		try {

		    // Saving of object in a file
		    FileOutputStream file = new FileOutputStream(filename);
		    ObjectOutputStream out = new ObjectOutputStream(file);

		    // Method for serialization of object
		    out.writeObject(songs);

		    out.close();
		    file.close();

		    System.out.println("Your songs have been saved.\n");
		}
		catch (IOException ex) {
		    System.out.println("IOException is caught");
		}
	}

/**<h2>saveAudioBooks</h2>
 * saveAudioBooks is used by the command "l"
 * @param audiobooks ArrayList<AudioBook> Object
 * */
	public static void saveAudioBooks(ArrayList<AudioBook> audiobooks){

		String filename = "audiobooks";

		// Serialization
		try {

		    // Saving of object in a file
		    FileOutputStream file = new FileOutputStream(filename);
		    ObjectOutputStream out = new ObjectOutputStream(file);

		    // Method for serialization of object
		    out.writeObject(audiobooks);

		    out.close();
		    file.close();

		    System.out.println("Your audiobooks have been saved.\n");
		}
		catch (IOException ex) {
		    System.out.println("IOException is caught");
		}
	}

/**<h2>listSongs</h2>
 * listSongs is called when using the "S" command in order to list the songs inside the "songs" file.
 * */

	public static void listSongs(){
		String filename="songs";

		// Deserialization
	        try {
			// Reading the object from a file
			FileInputStream file = new FileInputStream(filename);
			ObjectInputStream in = new ObjectInputStream(file);

			@SuppressWarnings("unchecked")

			// Method for deserialization of object
			ArrayList<Song> songsInFile = (ArrayList<Song>)in.readObject();

			in.close();
			file.close();
			System.out.println("\nSaved songs :\n");
			for (Song s : songsInFile ){
				System.out.println(s);
			}
		}
 		catch (IOException ex) {System.out.println("listSongs : Audio file missing.");}
		catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");}
	}

/**<h2>listAudioBooks</h2>
 * listAudioBooks is called when using the "AB" command in order to list the songs inside the "audiobooks" file.
 * */

	public static void listAudioBooks(){
		String filename="audiobooks";

		// Deserialization
	        try {
			// Reading the object from a file
			FileInputStream file = new FileInputStream(filename);
			ObjectInputStream in = new ObjectInputStream(file);

			@SuppressWarnings("unchecked")
			// Method for deserialization of object
			ArrayList<AudioBook> audiobooks = (ArrayList<AudioBook>)in.readObject();

			in.close();
			file.close();

			System.out.println("\nSaved audiobooks :\n");
			for (AudioBook s : audiobooks ){
				System.out.println(s);
			}
		}
 		catch (IOException ex) {System.out.println("listAudioBooks : AudioBook file missing.");}
		catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");}
	}

/**<h2>extractSongs</h2>
 * Extraction is used to print the content of the files and put them in the ArrayList used to add elements.
 * */

	public static ArrayList<Song> extractSongs(){

		// Deserialization
	        try {

			String songFileName="songs";
			FileInputStream songFile = new FileInputStream(songFileName);
			ObjectInputStream inSong = new ObjectInputStream(songFile);

			@SuppressWarnings("unchecked")
			ArrayList<Song> songs = (ArrayList<Song>)inSong.readObject();
			//used to temporarly save the songs before any "s" command

			inSong.close();
			songFile.close();

			return new ArrayList<Song>(songs);
		//Here will be the number of playlist, albums, songs and audiobook extracted


		}
 		catch (IOException ex) { //mainly occur bc file is missing, leading to an empty ListArray
			System.out.println("\nextractSongs : Songs file missing.\n");
			return new ArrayList<Song>() ;}
		catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");return null;}
}

/**<h2>extractAudioBooks</h2>
 * Extraction is used to print the content of the files and put them in the ArrayList used to add elements.
 * */

	public static ArrayList<AudioBook> extractAudioBooks(){

		// Deserialization
	        try {

			String audiobookFileName="audiobooks";
			FileInputStream audiobookFile = new FileInputStream(audiobookFileName);
			ObjectInputStream inAudioBook = new ObjectInputStream(audiobookFile);

			@SuppressWarnings("unchecked")
			ArrayList<AudioBook> audiobooks = (ArrayList<AudioBook>)inAudioBook.readObject();

			inAudioBook.close();
			audiobookFile.close();

			return new ArrayList<AudioBook>(audiobooks);
		//Here will be the number of playlist, albums, songs and audiobook extracted


		}
 		catch (IOException ex) { //mainly occur bc file is missing, leading to an empty ListArray
			System.out.println("\nextractAudioBooks : AudioBooks file missing.\n");
			return new ArrayList<AudioBook>() ;}
		catch (ClassNotFoundException ex) {System.out.println("ClassNotFoundException is caught");return null;}
}

	public jMusicHub() {

		System.out.println("Welcome to the jMusicHub !\n");

		Scanner scan = new Scanner(System.in);
		String userInput; //Used to get the user's inputs.

		ArrayList<Song> songs = extractSongs();
		ArrayList<AudioBook> audiobooks = extractAudioBooks();
		listSongs();
		listAudioBooks();

		do {
			System.out.println("What do you want to do? [h for help]");
			userInput = scan.nextLine();
			switch(userInput) {
				case "h" : //page help
					System.out.printf("c: add a new song\na: add a new album\n+: add an existing song to an album\nl: add a new audiobook\np: create a new playlist from existing songs and audiobooks\n-: delete a playlist\ns: save playlists, albums, songs and audiobooks into the concerned files\nS: List all your saved songs\nAB : List all your saved audiobooks\nh: print this help screen\nq: quit the program\n");
					break;
				case "q" : //quit
					System.out.println("Goodbye !");
					break;

				case "c": //add a new song
					try { //If something goes wrong, abort
						Song newSong=addSong(scan);
						if (newSong != null){
							int songsSize = songs.size();
							newSong.setId(songsSize);
							songs.add(newSong);
							System.out.println("\nActual content of your songs list (you must save it (s) to do anything else with your songs) :");
							for (Song iSong : songs){
							   System.out.println(iSong);
							}
							System.out.println("");

						}
					} catch (InputMismatchException | IllegalArgumentException e) {
						System.out.println("You typed something wrong... I'm aborting..");
						System.out.println("");
					}
					break;

				case "a": //add a new album
					break;
				case "+":
					break;
				case "l": //add a new audiobook
					try { //If something goes wrong, abort
						AudioBook newAudioBook=addAudioBook(scan);
						if (newAudioBook != null){
							int audiobookSize = audiobooks.size();
							newAudioBook.setId(audiobookSize);
							audiobooks.add(newAudioBook);
							System.out.println("\nActual content of your audiobook list (you must save it (s) to do anything else with your audiobooks) :");
							for (AudioBook iAudioBook : audiobooks){
							   System.out.println(iAudioBook);
							}
							System.out.println("");

						}
					} catch (InputMismatchException | IllegalArgumentException e) {
						System.out.println("You typed something wrong... I'm aborting..");
						System.out.println("");
					}
					break;
				case "p":
					break;
				case "-":
					break;
				case "s": //save songs, audiobooks, playlist and albums
					saveSongs(songs);
					saveAudioBooks(audiobooks);
					break;
				case "S": //print songs contained in songs
					listSongs();
					break;
				case "AB": //print audiobooks containd in audiobooks
					listAudioBooks();
					break;
				default :
					System.out.println("Unknown command. Type h for help.");

			}
		} while(!userInput.equals("q"));

	}

	public static void main(String[] args) {
		new jMusicHub();
	}
}