Advertisement
Mary_Loskutova

New

Mar 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. public class Song {
  2.  
  3.     String name;
  4.     String artist;
  5.     int length;
  6.  
  7.     Song(String name, String artist, int length) {
  8.         this.name = name;
  9.         this.artist = artist;
  10.         this.length = length;
  11.     }
  12.  
  13.     public void play() throws InterruptedException {
  14.  
  15.         System.out.println("Playing:" + " " + name + " " + artist);
  16.         Thread.sleep(length * 1000);
  17.     }
  18. }
  19.  
  20. public class Playlist extends Thread {
  21.     String playlistName;
  22.     ArrayList<Song> songs;
  23.  
  24.     Playlist(String playlistName, ArrayList<Song> songs) {
  25.         this.playlistName = playlistName;
  26.         this.songs = songs;
  27.         this.setDaemon(true);
  28.     }
  29.  
  30.     Runnable onFinishAction;
  31.     void setOnFinishAction(Runnable action) {
  32.         this.onFinishAction = action;
  33.     }
  34.  
  35.     public void playPlaylist() {
  36.         for (Song s : this.songs) {
  37.             try {
  38.                 s.play();
  39.  
  40.             } catch (InterruptedException ex) {
  41.                 System.out.print("Plylist " + playlistName + " has been stopped!" + "\n");
  42.                 return;
  43.             }
  44.         }
  45.         if (this.onFinishAction != null)
  46.             this.onFinishAction.run();
  47.     }
  48.  
  49.     public void run() {
  50.         this.playPlaylist();
  51.     }
  52. }
  53. public class Player {
  54.     final ArrayList<Playlist> activeplaylists;
  55.     Scanner scanner = null;
  56.  
  57.     public Player() {
  58.         this.activeplaylists = new ArrayList<Playlist>();
  59.     }
  60.  
  61.     public void startPlaylist(Playlist playlist) {
  62.         this.activeplaylists.add(playlist);
  63.         Runnable action = new Runnable(){
  64.             public void run(){
  65.             activeplaylists.remove(playlist);
  66.             }
  67.         };
  68.         playlist.setOnFinishAction(action);
  69.         playlist.start();
  70.            
  71.     }
  72.  
  73.     public void writePlaylists() {
  74.         for (Playlist s : activeplaylists) {
  75.             System.out.println(s.getName());
  76.         }
  77.     }
  78.  
  79.     public void stopPlaylist(int index) {
  80.         this.activeplaylists.get(index - 1).interrupt();
  81.         this.activeplaylists.remove(index - 1);
  82.     }
  83.  
  84.     public void finish() {
  85.         for (Playlist s : activeplaylists) {
  86.             try {
  87.                 s.interrupt();
  88.                 s.join();
  89.                 this.activeplaylists.clear();
  90.             } catch (InterruptedException ex) {
  91.                 System.out.print("All playlists have been stopped!");
  92.             }
  93.         }
  94.     }
  95.  
  96.     public static Playlist readPlaylist(String name, String filePath) throws FileNotFoundException {
  97.         Scanner scanner = new Scanner(new BufferedReader(new FileReader(filePath)));
  98.         ArrayList<Song> songs = new ArrayList<Song>();
  99.         while (scanner.hasNextLine()) {
  100.             String a = scanner.nextLine();
  101.             String[] words = a.split("#");
  102.             String variable = words[2];
  103.             int lengthOfTrack = Integer.parseInt(variable.trim());
  104.             Song s = new Song(words[0], words[1], lengthOfTrack);
  105.             songs.add(s);
  106.         }
  107.         scanner.close();
  108.         Playlist playlist = new Playlist(name, songs);
  109.         return playlist;
  110.     }
  111. }
  112.  
  113. public class Test {
  114.  
  115.     public static void main(String[] args) {
  116.  
  117.         Menu menu = new Menu();
  118.         final Player player = new Player();
  119.         Scanner sc = new Scanner(System.in);
  120.  
  121.         menu.addEntry(new MenuEntry("Play") {
  122.             @Override
  123.             public void run() {
  124.                 System.out.print("Enter the playlist name" + "\n");
  125.                 String entry = sc.nextLine();
  126.                 try {
  127.                     Playlist playlist = Player.readPlaylist(entry, entry);
  128.                     player.startPlaylist(playlist);
  129.                     playlist.setName(entry);
  130.                 } catch (FileNotFoundException ex) {
  131.                     System.out.print("There are no playlist with such name!");
  132.  
  133.                 }
  134.             }
  135.         });
  136.  
  137.         menu.addEntry(new MenuEntry("Stop") {
  138.             @Override
  139.             public void run() {
  140.                 System.out.println("Enter the number: 1.Stop all playlists" + " " + "2.Stop one playlist" + " "
  141.                         + "3.Cancel and return" + "\n");
  142.  
  143.                 do {
  144.                     try {
  145.                         int input;
  146.                         input = sc.nextInt();
  147.                         switch (input) {
  148.                         case 1:
  149.                             player.finish();
  150.                             return;
  151.                         case 2:
  152.                             if ((Thread.activeCount() - 1) != 0) {
  153.                                 System.out.print("Number of active playlists: " + (Thread.activeCount() - 1) + "\n"
  154.                                         + "They are: ");
  155.                                 player.writePlaylists();
  156.                                 System.out.println("Enter the number:");
  157.                                 int number = sc.nextInt();
  158.                                 player.stopPlaylist(number);
  159.                                 return;
  160.                             } else {
  161.                                 System.out.println("There are no active playlists");
  162.                             }
  163.                         case 3:
  164.                             return;
  165.                         default:
  166.                             System.out.print("Wrong input!" + "\n");
  167.                             break;
  168.                         }
  169.                     } catch (InputMismatchException ex) {
  170.                         System.out.print("Wrong input!");
  171.                         break;
  172.                     }
  173.                 } while (true);
  174.             }
  175.         });
  176.  
  177.         menu.run();
  178.         sc.close();
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement