Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Song {
- String name;
- String artist;
- int length;
- Song(String name, String artist, int length) {
- this.name = name;
- this.artist = artist;
- this.length = length;
- }
- public void play() throws InterruptedException {
- System.out.println("Playing:" + " " + name + " " + artist);
- Thread.sleep(length * 1000);
- }
- }
- public class Playlist extends Thread {
- String playlistName;
- ArrayList<Song> songs;
- Playlist(String playlistName, ArrayList<Song> songs) {
- this.playlistName = playlistName;
- this.songs = songs;
- this.setDaemon(true);
- }
- Runnable onFinishAction;
- void setOnFinishAction(Runnable action) {
- this.onFinishAction = action;
- }
- public void playPlaylist() {
- for (Song s : this.songs) {
- try {
- s.play();
- } catch (InterruptedException ex) {
- System.out.print("Plylist " + playlistName + " has been stopped!" + "\n");
- return;
- }
- }
- if (this.onFinishAction != null)
- this.onFinishAction.run();
- }
- public void run() {
- this.playPlaylist();
- }
- }
- public class Player {
- final ArrayList<Playlist> activeplaylists;
- Scanner scanner = null;
- public Player() {
- this.activeplaylists = new ArrayList<Playlist>();
- }
- public void startPlaylist(Playlist playlist) {
- this.activeplaylists.add(playlist);
- Runnable action = new Runnable(){
- public void run(){
- activeplaylists.remove(playlist);
- }
- };
- playlist.setOnFinishAction(action);
- playlist.start();
- }
- public void writePlaylists() {
- for (Playlist s : activeplaylists) {
- System.out.println(s.getName());
- }
- }
- public void stopPlaylist(int index) {
- this.activeplaylists.get(index - 1).interrupt();
- this.activeplaylists.remove(index - 1);
- }
- public void finish() {
- for (Playlist s : activeplaylists) {
- try {
- s.interrupt();
- s.join();
- this.activeplaylists.clear();
- } catch (InterruptedException ex) {
- System.out.print("All playlists have been stopped!");
- }
- }
- }
- public static Playlist readPlaylist(String name, String filePath) throws FileNotFoundException {
- Scanner scanner = new Scanner(new BufferedReader(new FileReader(filePath)));
- ArrayList<Song> songs = new ArrayList<Song>();
- while (scanner.hasNextLine()) {
- String a = scanner.nextLine();
- String[] words = a.split("#");
- String variable = words[2];
- int lengthOfTrack = Integer.parseInt(variable.trim());
- Song s = new Song(words[0], words[1], lengthOfTrack);
- songs.add(s);
- }
- scanner.close();
- Playlist playlist = new Playlist(name, songs);
- return playlist;
- }
- }
- public class Test {
- public static void main(String[] args) {
- Menu menu = new Menu();
- final Player player = new Player();
- Scanner sc = new Scanner(System.in);
- menu.addEntry(new MenuEntry("Play") {
- @Override
- public void run() {
- System.out.print("Enter the playlist name" + "\n");
- String entry = sc.nextLine();
- try {
- Playlist playlist = Player.readPlaylist(entry, entry);
- player.startPlaylist(playlist);
- playlist.setName(entry);
- } catch (FileNotFoundException ex) {
- System.out.print("There are no playlist with such name!");
- }
- }
- });
- menu.addEntry(new MenuEntry("Stop") {
- @Override
- public void run() {
- System.out.println("Enter the number: 1.Stop all playlists" + " " + "2.Stop one playlist" + " "
- + "3.Cancel and return" + "\n");
- do {
- try {
- int input;
- input = sc.nextInt();
- switch (input) {
- case 1:
- player.finish();
- return;
- case 2:
- if ((Thread.activeCount() - 1) != 0) {
- System.out.print("Number of active playlists: " + (Thread.activeCount() - 1) + "\n"
- + "They are: ");
- player.writePlaylists();
- System.out.println("Enter the number:");
- int number = sc.nextInt();
- player.stopPlaylist(number);
- return;
- } else {
- System.out.println("There are no active playlists");
- }
- case 3:
- return;
- default:
- System.out.print("Wrong input!" + "\n");
- break;
- }
- } catch (InputMismatchException ex) {
- System.out.print("Wrong input!");
- break;
- }
- } while (true);
- }
- });
- menu.run();
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement