Advertisement
Mary_Loskutova

Player

Mar 8th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.93 KB | None | 0 0
  1. import java.lang.InterruptedException;
  2.  
  3. public class Song {
  4.  
  5.     String name;
  6.     String artist;
  7.     int length;
  8.  
  9.     Song(String name, String artist, int length) {
  10.         this.name = name;
  11.         this.artist = artist;
  12.         this.length = length;
  13.     }
  14.  
  15.     public void play() throws InterruptedException {
  16.  
  17.         System.out.println("Playing:" + " " + name + " " + artist);
  18.         Thread.sleep(length * 100);
  19.     }
  20.  
  21. }
  22.  
  23. import java.util.ArrayList;
  24.  
  25. public class Playlist extends Thread {
  26.     String playlistName;
  27.     ArrayList<Song> songs = new ArrayList<Song>();
  28.  
  29.     Playlist(String playlistName, ArrayList<Song> songs) {
  30.         this.playlistName = playlistName;
  31.         this.songs = songs;
  32.         this.setDaemon(true);
  33.     }
  34.  
  35.     public void playListplay() {
  36.         for (Song s : this.songs) {
  37.             try {
  38.                 s.play();
  39.             } catch (InterruptedException ex) {
  40.                 System.out.print("Plylist " + playlistName + " has been stopped!" + "\n");
  41.                 return;
  42.             }
  43.         }
  44.     }
  45.  
  46.     public void run() {
  47.         this.playListplay();
  48.     }
  49. }
  50.  
  51.  
  52. import java.io.BufferedReader;
  53. import java.io.FileNotFoundException;
  54. import java.io.FileReader;
  55. import java.util.ArrayList;
  56. import java.util.Scanner;
  57.  
  58. public class Player {
  59.     ArrayList<Playlist> activeplaylists;
  60.     Scanner scanner = null;
  61.  
  62.     public Player() {
  63.         this.activeplaylists = new ArrayList<Playlist>();
  64.     }
  65.  
  66.     public void addPlaylist(Playlist playlist) {
  67.         this.activeplaylists.add(playlist);
  68.     }
  69.  
  70.     public void stopPlayList(int index) {
  71.         this.activeplaylists.get(index - 1).interrupt();
  72.     }
  73.  
  74.     public void finish() {
  75.         for (Playlist s : activeplaylists) {
  76.             try {
  77.                 s.interrupt();
  78.                 s.join();
  79.  
  80.             } catch (InterruptedException ex) {
  81.                 System.out.print("All playlists have been stopped!");
  82.             }
  83.         }
  84.     }
  85.  
  86.     public static Playlist readPlaylist(String name, String filePath) throws FileNotFoundException {
  87.         Scanner scanner = new Scanner(new BufferedReader(new FileReader(filePath)));
  88.         ArrayList<Song> songs = new ArrayList<Song>();
  89.         while (scanner.hasNextLine()) {
  90.             String a = scanner.nextLine();
  91.             String[] words = a.split("#");
  92.             String variable = words[2];
  93.             int lengthOfTrack = Integer.parseInt(variable);
  94.             Song s = new Song(words[0], words[1], lengthOfTrack);
  95.             songs.add(s);
  96.         }
  97.         scanner.close();
  98.         Playlist playlist = new Playlist(name, songs);
  99.         return playlist;
  100.     }
  101. }
  102.  
  103. public abstract class MenuEntry {
  104.  
  105.     private String title;
  106.  
  107.     public MenuEntry(String title) {
  108.         this.title = title;
  109.     }
  110.  
  111.     public String getTitle() {
  112.         return title;
  113.     }
  114.  
  115.     public void setTitle(String title) {
  116.         this.title = title;
  117.     }
  118.  
  119.     public abstract void run();
  120. }
  121.  
  122. import java.util.ArrayList;
  123. import java.util.Scanner;
  124.  
  125. public class Menu {
  126.     private static final String MENU_PATTERN = "%s - %s\n";
  127.     private ArrayList<MenuEntry> entries = new ArrayList<MenuEntry>();
  128.     private boolean isExit = false;
  129.  
  130.     public Menu() {
  131.         entries.add(new MenuEntry("Exit") {
  132.             @Override
  133.             public void run() {
  134.                 isExit = true;
  135.             }
  136.         });
  137.     }
  138.  
  139.     public void run() {
  140.         Scanner sc = new Scanner(System.in);
  141.         while (!isExit) {
  142.             printMenu();
  143.  
  144.             try {
  145.                 String line = sc.nextLine();
  146.  
  147.                 int choice = Integer.parseInt(line);
  148.                 MenuEntry entry = entries.get(choice - 1);
  149.                 entry.run();
  150.  
  151.             } catch (NumberFormatException e) {
  152.                 System.out.print("Wrong input!");
  153.             } catch (IndexOutOfBoundsException e2) {
  154.                 System.out.print("Wrong input!");
  155.             }
  156.         }
  157.         sc.close();
  158.     }
  159.  
  160.     public Menu addEntry(MenuEntry entry) {
  161.  
  162.         int index = entries.size() - 1;
  163.         entries.add(index, entry);
  164.         return this;
  165.  
  166.     }
  167.  
  168.     private void printMenu() {
  169.         StringBuffer buffer = new StringBuffer();
  170.         buffer.append("\nMenu:\n");
  171.         for (int i = 0; i < entries.size(); i++) {
  172.             MenuEntry entry = entries.get(i);
  173.             String entryFormatted = String.format(MENU_PATTERN, (i + 1), entry.getTitle());
  174.             buffer.append(entryFormatted);
  175.         }
  176.         System.out.print(buffer.toString());
  177.     }
  178. }
  179.  
  180.  
  181. import java.io.FileNotFoundException;
  182. import java.util.Scanner;
  183. import java.util.InputMismatchException;
  184.  
  185. public class Test {
  186.  
  187.     public static void main(String[] args) {
  188.  
  189.         Menu menu = new Menu();
  190.         final Player player = new Player();
  191.         Scanner sc = new Scanner(System.in);
  192.  
  193.         menu.addEntry(new MenuEntry("Play") {
  194.             @Override
  195.             public void run() {
  196.                 System.out.print("Enter the playlist name" + "\n");
  197.  
  198.                 String entry = sc.nextLine();
  199.                 try {
  200.                     Playlist playlist = Player.readPlaylist(entry, entry);
  201.                     player.addPlaylist(playlist);
  202.                     playlist.start();
  203.  
  204.                 } catch (FileNotFoundException ex) {
  205.                     System.out.print("There are no playlist with such name!");
  206.                     return;
  207.                 }
  208.             }
  209.         });
  210.  
  211.         menu.addEntry(new MenuEntry("Stop") {
  212.             @Override
  213.             public void run() {
  214.                 System.out.println("Enter the number: 1.Stop all playlists" + " " + "2.Stop one playlist" + " "
  215.                         + "3.Cancel and return" + "\n");
  216.  
  217.                 if (sc.hasNextLine()) {
  218.                     try {
  219.                         int input = sc.nextInt();
  220.                         do {
  221.                             switch (input) {
  222.                             case 1:
  223.                                 input = 1;
  224.                                 player.finish();
  225.                                 return;
  226.  
  227.                             case 2:
  228.                                 input = 2; {
  229.                                 System.out.println("Enter the number:");
  230.                                 Scanner sc = new Scanner(System.in);
  231.                                 int number = sc.nextInt();
  232.                                 player.stopPlayList(number);
  233.                                 return;
  234.                                
  235.                             }
  236.                             case 3:
  237.                                 input = 3; {
  238.                                 return;
  239.                             }
  240.                             default:
  241.                                 System.out.print("Wrong input!");
  242.                             }
  243.  
  244.                         } while (input == -1);
  245.                     } catch (InputMismatchException ex) {
  246.                         System.out.print("Wrong input!");
  247.                     }
  248.  
  249.                 }
  250.             }
  251.         });
  252.  
  253.         menu.run();
  254.         sc.close();
  255.     }
  256. }
  257.  
  258.  
  259. one:
  260. Artifact303#Beyond Lightspeed#922
  261. Artifact303#Delirium#824
  262. Artifact303#Mysterious Fantasy#917
  263. Artifact303#Tropical Sunset#833
  264. Artifact303#Close Encounter#919
  265. Artifact303#For A Better World#802
  266. Artifact303#Trancemission#929
  267. Artifact303#They Will Communicate#832
  268. Artifact303#Magnetic Fields#823
  269.  
  270. two:
  271. Onuka#Misto#922
  272. Onuka#Sunday#824
  273. Onuka#Be#917
  274. Onuka#Go#833
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement