Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.InterruptedException;
- 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 * 100);
- }
- }
- import java.util.ArrayList;
- public class Playlist extends Thread {
- String playlistName;
- ArrayList<Song> songs = new ArrayList<Song>();
- Playlist(String playlistName, ArrayList<Song> songs) {
- this.playlistName = playlistName;
- this.songs = songs;
- this.setDaemon(true);
- }
- public void playListplay() {
- for (Song s : this.songs) {
- try {
- s.play();
- } catch (InterruptedException ex) {
- System.out.print("Plylist " + playlistName + " has been stopped!" + "\n");
- return;
- }
- }
- }
- public void run() {
- this.playListplay();
- }
- }
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Player {
- ArrayList<Playlist> activeplaylists;
- Scanner scanner = null;
- public Player() {
- this.activeplaylists = new ArrayList<Playlist>();
- }
- public void addPlaylist(Playlist playlist) {
- this.activeplaylists.add(playlist);
- }
- public void stopPlayList(int index) {
- this.activeplaylists.get(index - 1).interrupt();
- }
- public void finish() {
- for (Playlist s : activeplaylists) {
- try {
- s.interrupt();
- s.join();
- } 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);
- Song s = new Song(words[0], words[1], lengthOfTrack);
- songs.add(s);
- }
- scanner.close();
- Playlist playlist = new Playlist(name, songs);
- return playlist;
- }
- }
- public abstract class MenuEntry {
- private String title;
- public MenuEntry(String title) {
- this.title = title;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public abstract void run();
- }
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Menu {
- private static final String MENU_PATTERN = "%s - %s\n";
- private ArrayList<MenuEntry> entries = new ArrayList<MenuEntry>();
- private boolean isExit = false;
- public Menu() {
- entries.add(new MenuEntry("Exit") {
- @Override
- public void run() {
- isExit = true;
- }
- });
- }
- public void run() {
- Scanner sc = new Scanner(System.in);
- while (!isExit) {
- printMenu();
- try {
- String line = sc.nextLine();
- int choice = Integer.parseInt(line);
- MenuEntry entry = entries.get(choice - 1);
- entry.run();
- } catch (NumberFormatException e) {
- System.out.print("Wrong input!");
- } catch (IndexOutOfBoundsException e2) {
- System.out.print("Wrong input!");
- }
- }
- sc.close();
- }
- public Menu addEntry(MenuEntry entry) {
- int index = entries.size() - 1;
- entries.add(index, entry);
- return this;
- }
- private void printMenu() {
- StringBuffer buffer = new StringBuffer();
- buffer.append("\nMenu:\n");
- for (int i = 0; i < entries.size(); i++) {
- MenuEntry entry = entries.get(i);
- String entryFormatted = String.format(MENU_PATTERN, (i + 1), entry.getTitle());
- buffer.append(entryFormatted);
- }
- System.out.print(buffer.toString());
- }
- }
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import java.util.InputMismatchException;
- 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.addPlaylist(playlist);
- playlist.start();
- } catch (FileNotFoundException ex) {
- System.out.print("There are no playlist with such name!");
- return;
- }
- }
- });
- 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");
- if (sc.hasNextLine()) {
- try {
- int input = sc.nextInt();
- do {
- switch (input) {
- case 1:
- input = 1;
- player.finish();
- return;
- case 2:
- input = 2; {
- System.out.println("Enter the number:");
- Scanner sc = new Scanner(System.in);
- int number = sc.nextInt();
- player.stopPlayList(number);
- return;
- }
- case 3:
- input = 3; {
- return;
- }
- default:
- System.out.print("Wrong input!");
- }
- } while (input == -1);
- } catch (InputMismatchException ex) {
- System.out.print("Wrong input!");
- }
- }
- }
- });
- menu.run();
- sc.close();
- }
- }
- one:
- Artifact303#Beyond Lightspeed#922
- Artifact303#Delirium#824
- Artifact303#Mysterious Fantasy#917
- Artifact303#Tropical Sunset#833
- Artifact303#Close Encounter#919
- Artifact303#For A Better World#802
- Artifact303#Trancemission#929
- Artifact303#They Will Communicate#832
- Artifact303#Magnetic Fields#823
- two:
- Onuka#Misto#922
- Onuka#Sunday#824
- Onuka#Be#917
- Onuka#Go#833
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement