Commit 2c08e4df authored by MostafaRahmati's avatar MostafaRahmati

A Simple Model For Java Music Player

parents
Pipeline #5886 failed with stages
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../../../../:\Users\Mostafa\Desktop\MusicCollection\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="openjdk-15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/MusicCollection.iml" filepath="$PROJECT_DIR$/MusicCollection.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import java.util.Scanner;
public class Driver {
private static MusicCollection pop = new MusicCollection(new MusicPlayer());
private static MusicCollection rock = new MusicCollection(new MusicPlayer());
private static MusicCollection jazz = new MusicCollection(new MusicPlayer());
private static MusicCollection country = new MusicCollection(new MusicPlayer());
private static MusicCollection favorites = new MusicCollection(new MusicPlayer());
private static MusicCollection searchResults = new MusicCollection(new MusicPlayer());
private static int playingFolder=-1;
private static int playingIndex=-1;
/**
* this is the main driver for our program
* @param args given args in console
*
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int order = -1;
while (order != 0) {
System.out.println("""
Welcome To MyPlayer!
1- Play From Library
2- Add Music
3- List Musics
4- Stop Playing
0- Exit
""");
order = scanner.nextInt();
switch (order) {
case 1 -> playFromLibrary();
case 2 -> addMusic();
case 3 -> listMusics();
case 4 -> stopPlayer();
case 0 -> System.out.println("FareWell!");
default -> System.out.println("WRONG ENTRY!");
}
clearScreen();
}
}
/**
* Stop the file which is being played
*/
private static void stopPlayer() {
if (playingFolder!=-1 && playingIndex!=-1)
switch (playingFolder){
case (0) -> pop.stopPlaying();
case (1) -> rock.stopPlaying();
case (2) -> jazz.stopPlaying();
case (3) -> country.stopPlaying();
case (4) -> favorites.stopPlaying();
}
else
System.out.println("No File Is Being Played!");
}
/**
* shows a list of all music tracks in any willing folder
*/
private static void listMusics() {
System.out.println("Please Choose Genre:");
Scanner scanner=new Scanner(System.in);
int genre = scanner.nextInt();
switch (genre){
case (0) -> pop.listAllFiles();
case (1) -> rock.listAllFiles();
case (2) -> jazz.listAllFiles();
case (3) -> country.listAllFiles();
case (4) -> favorites.listAllFiles();
}
}
/**
* method to add a music track to the list
*/
private static void addMusic() {
Scanner scanner = new Scanner(System.in);
Music track = new Music();
System.out.println("Please Enter:\nMusic Genre:\n0- Pop\n1-Rock\n2-Jazz\n3-Country\n");
int genre = scanner.nextInt();
System.out.println("Music Name:");
scanner.nextLine();
String name = scanner.nextLine();
System.out.println("Music Artist:");
String artist = scanner.nextLine();
System.out.println("Music Release Year:");
int year = scanner.nextInt();
scanner.nextLine();
System.out.println("Music Directory:");
String dir = scanner.nextLine();
track.setName(name);
track.setArtist(artist);
track.setDistributionYear(year);
track.setFileDirectory(dir);
switch (genre){
case (0) -> pop.addFile(track);
case (1) -> rock.addFile(track);
case (2) -> jazz.addFile(track);
case (3) -> country.addFile(track);
}
}
/**
* method to play a music track from any willing folder
*/
private static void playFromLibrary() {
Scanner scanner = new Scanner(System.in);
int folder;
clearScreen();
System.out.println("""
Please Choose Library...
0- Pop
1- Jazz
2- Rock
3- Country
4- Favorites
""");
folder = scanner.nextInt();
clearScreen();
System.out.println("1- Play By Index\n2- Search");
int order = scanner.nextInt();
if (order == 1) {
System.out.println("Please Enter The Index...");
int index = scanner.nextInt();
switch (folder){
case (0) -> pop.startPlaying(index);
case (1) -> rock.startPlaying(index);
case (2) -> jazz.startPlaying(index);
case (3) -> country.startPlaying(index);
}
playingFolder=folder;
playingIndex=index;
} else if (order == 2) {
System.out.println("Enter Keyword");
String keyWord = scanner.next();
switch (folder){
case (0) -> searchResults=pop.search(keyWord);
case (1) -> searchResults=rock.search(keyWord);
case (2) -> searchResults=jazz.search(keyWord);
case (3) -> searchResults=country.search(keyWord);
}
searchResults.listAllFiles();
System.out.println("Please Enter Track Index To Be Chosen...");
int index = scanner.nextInt();
System.out.println("1- Play\n2- Add To Favorites");
int order2 = scanner.nextInt();
if (order2 == 1)
searchResults.startPlaying(index);
else if (order2 == 2)
favorites.addFile(searchResults.getFile(index-1));
}
}
/**
* if we run the program in Command Prompt, this method will erase the whole console
*/
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
public class Music {
/**
* name of the track
*/
private String name;
/**
* artist of the track
*/
private String artist;
/**
* the YEAR of release
* Caution!
* year must be greater than 1900 and less than 2020
*/
private int distributionYear;
/**
* where file is saved.
*/
private String fileDirectory;
/**
* @return name of the track
*/
public String getName() {
return name;
}
/**
* @param name sets the name for track
*/
public void setName(String name) {
this.name = name;
}
/**
* @return artist name of the track
*/
public String getArtist() {
return artist;
}
/**
* @param artist sets name of track artist
*/
public void setArtist(String artist) {
this.artist = artist;
}
/**
* @return track release year
*/
public int getDistributionYear() {
return distributionYear;
}
/**
* @param distributionYear sets the year which track was released
*
*/
public void setDistributionYear(int distributionYear) {
if (distributionYear < 2022 && distributionYear > 1900)
this.distributionYear = distributionYear;
else
System.out.println("Wrong Distribution Year");
}
/**
* @return directory of the track
*/
public String getFileDirectory() {
return fileDirectory;
}
/**
* @param fileDirectory sets the directory of where our track is saved
*/
public void setFileDirectory(String fileDirectory) {
this.fileDirectory = fileDirectory;
}
}
import java.util.ArrayList;
/**
* A class to hold details of audio files.
*
* @author David J. Barnes and Michael Kolling
* @version 2011.07.31
*/
public class MusicCollection {
// An ArrayList for storing the file names of music files.
private ArrayList<Music> files;
// A player for the music files.
private MusicPlayer player;
/**
* Create a MusicCollection
*/
public MusicCollection(MusicPlayer player) {
files=new ArrayList<>();
this.player=player;
}
/**
* Add a file to the collection.
*
* @param track The track to be added.
*/
public void addFile(Music track) {
files.add(track);
}
/**
* Return the number of files in the collection.
*
* @return The number of files in the collection.
*/
public int getNumberOfFiles() {
return files.size();
}
/**
* List a file from the collection.
*
* @param index The index of the file to be listed.
*/
public void listFile(int index) {
if (validIndex(index)) {
Music track = files.get(index);
System.out.println(track.getName() + "\nArtist:\t" + track.getArtist() +
"\nDistributed in:\t" + track.getDistributionYear() + "\nDirectory:\t" +
track.getFileDirectory());
}
}
/**
* Show a list of all the files in the collection.
*/
public void listAllFiles() {
int i = 0;
for (Music track : files) {
i++;
System.out.println(i+"- "+track.getName() + "\nArtist:\t" + track.getArtist() +
"\nDistributed in:\t" + track.getDistributionYear() + "\nDirectory:\t" +
track.getFileDirectory()+"\n");
}
}
/**
* Remove a file from the collection.
*
* @param index The index of the file to be removed.
*/
public void removeFile(int index) {
if (validIndex(index))
files.remove(index);
}
/**
* Start playing a file in the collection.
* Use stopPlaying() to stop it playing.
*
* @param index The index of the file to be played.
*/
public void startPlaying(int index) {
if (validIndex(index))
player.startPlaying(files.get(index).getName());
}
/**
* Stop the player and shows a message for that.
*/
public void stopPlaying() {
player.stop();
System.out.println("Player Stopped Successfully...\n");
}
/**
* Determine whether the given index is valid for the collection.
* Print an error message if it is not.
*
* @param index The index to be checked.
* @return true if the index is valid, false otherwise.
*/
private boolean validIndex(int index) {
// The return value.
// Set according to whether the index is valid or not.
if (index >= files.size()) System.out.println("Wrong Index!");
return index < files.size() && index >= 0;
}
/**
* This function will iterate through our collection and
* to search for given keyword.
*
* @param keyWord the keyword which is being searched
* @return an object containing all matches
*/
public MusicCollection search(String keyWord) {
MusicCollection searchResults = new MusicCollection(new MusicPlayer());
for (Music track : files) {
if (track.getFileDirectory().contains(keyWord) || track.getArtist().equals(keyWord) ||
track.getName().equals(keyWord))
searchResults.addFile(track);
}
return searchResults;
}
/**
* @param index the index of the file
* @return the file with the given index in our collection
*/
public Music getFile(int index) {
return files.get(index);
}
}
\ No newline at end of file
/**
* Provide basic playing of MP3 files via the javazoom library.
* See http://www.javazoom.net/
*
* @author David J. Barnes and Michael Kolling.
* @version 2011.07.31
*/
public class MusicPlayer {
// The current player. It might be null.
private boolean isPlaying;
/**
* Constructor for objects of class MusicFilePlayer
*/
public MusicPlayer() {
isPlaying = false;
}
/**
* Start playing the given audio file.
* The method returns once the playing has been started.
*
* @param filename The file to be played.
*/
public void startPlaying(String filename) {
System.out.println(filename + " is playing...");
isPlaying = true;
}
/**
* shows a message for stopping player
*/
public void stop() {
System.out.println("player is stopped!");
isPlaying = false;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment