kristina7

НП - Лаб 7-1 MP3 Player

Jan 9th, 2019
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. /*
  2. Да се напише класа за MP3Player во која се чуваат листа со песни (List<Song>) и песната која моментално се слуша (е на ред да се пушти). MP3Player-от има четири копчиња Play, Stop, FWD и REW.
  3.  
  4. Ако се притисне копчето Play се пушта моменталната песна (на екранот се испишува "Song i is playing", каде i е редниот број на моменталната песна, почнувајќи од 0).
  5. Ако се притисне копчето Stop:
  6. моменталната песна кој е пуштена се паузира (на екран се испишува "Song i is paused" каде i е моменталната песна која била пуштена).
  7. листата целосно се ресетира од почеток, ако моменталната песна веќе била паузирана (на екран се испишува "Songs are stopped").
  8. Ако се притисне копчето FWD песната се паузира и следната песна од листата станува моментална (да се земе во предвид кружното повторување на песните).
  9. Ако се притисне копчето REW песната се паузира и претходната песна од листата станува моментална (да се земе во предвид кружното повторување на песните).
  10. За секоја песна (Song) се чуваат насловот на песната (String) и изведувачот на песната (String).
  11. */
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.stream.Collectors;
  16.  
  17.  
  18. class Song {
  19.     String title;
  20.     String artist;
  21.  
  22.     public Song(String title, String artist) {
  23.         super();
  24.         this.title = title;
  25.         this.artist = artist;
  26.     }
  27.  
  28.     @Override
  29.     public String toString() {
  30.         return "Song{title=" + title + ", artist=" + artist + "}";
  31.     }
  32.  
  33. }
  34.  
  35. class MP3Player {
  36.     List<Song> songs;
  37.     int current;
  38.     boolean playing;
  39.     boolean paused;
  40.  
  41.     public MP3Player(List<Song> songs) {
  42.         this.songs = new ArrayList<>();
  43.         songs.forEach(song -> this.songs.add(song));
  44.         this.current = 0;
  45.         this.playing = false;
  46.         this.paused = false;
  47.     }
  48.  
  49.     public void pressPlay() {
  50.         System.out.printf("Song %s playing\n", playing ? "is already" : current + " is");
  51.         playing = true;
  52.         paused = false;
  53.     }
  54.  
  55.     public void printCurrentSong() {
  56.         System.out.println(songs.get(current));
  57.     }
  58.  
  59.     public void pressStop() {
  60.         if (playing) {
  61.             playing = false;
  62.             paused = true;
  63.             System.out.println("Song " + current + " is paused");
  64.         } else if (paused) {
  65.             paused = false;
  66.             current = 0;
  67.             System.out.println("Songs are stopped");
  68.         } else
  69.             System.out.println("Songs are already stopped");
  70.     }
  71.  
  72.     public void pressFWD() {
  73.         current = (current + 1) % songs.size();
  74.         playing = false;
  75.         paused = true;
  76.         System.out.println("Forward...");
  77.     }
  78.  
  79.     public void pressREW() {
  80.         current = (songs.size() + --current) % songs.size();
  81.         playing = false;
  82.         paused = true;
  83.         System.out.println("Reward...");
  84.     }
  85.  
  86.     public String songsToString() {
  87.         return songs.stream().map(Song::toString).collect(Collectors.joining(", "));
  88.     }
  89.  
  90.     @Override
  91.     public String toString() {
  92.         return "MP3Player{currentSong = " + current + ", songList = [" + songsToString() + "]}";
  93.     }
  94. }
  95.  
  96. public class PatternTest {
  97.     public static void main(String args[]) {
  98.         List<Song> listSongs = new ArrayList<Song>();
  99.         listSongs.add(new Song("first-title", "first-artist"));
  100.         listSongs.add(new Song("second-title", "second-artist"));
  101.         listSongs.add(new Song("third-title", "third-artist"));
  102.         listSongs.add(new Song("fourth-title", "fourth-artist"));
  103.         listSongs.add(new Song("fifth-title", "fifth-artist"));
  104.         MP3Player player = new MP3Player(listSongs);
  105.  
  106.         System.out.println(player.toString());
  107.         System.out.println("First test");
  108.  
  109.         player.pressPlay();
  110.         player.printCurrentSong();
  111.         player.pressPlay();
  112.         player.printCurrentSong();
  113.  
  114.         player.pressPlay();
  115.         player.printCurrentSong();
  116.         player.pressStop();
  117.         player.printCurrentSong();
  118.  
  119.         player.pressPlay();
  120.         player.printCurrentSong();
  121.         player.pressFWD();
  122.         player.printCurrentSong();
  123.  
  124.         player.pressPlay();
  125.         player.printCurrentSong();
  126.         player.pressREW();
  127.         player.printCurrentSong();
  128.  
  129.         System.out.println(player.toString());
  130.         System.out.println("Second test");
  131.  
  132.         player.pressStop();
  133.         player.printCurrentSong();
  134.         player.pressStop();
  135.         player.printCurrentSong();
  136.  
  137.         player.pressStop();
  138.         player.printCurrentSong();
  139.         player.pressPlay();
  140.         player.printCurrentSong();
  141.  
  142.         player.pressStop();
  143.         player.printCurrentSong();
  144.         player.pressFWD();
  145.         player.printCurrentSong();
  146.  
  147.         player.pressStop();
  148.         player.printCurrentSong();
  149.         player.pressREW();
  150.         player.printCurrentSong();
  151.  
  152.         System.out.println(player.toString());
  153.         System.out.println("Third test");
  154.  
  155.         player.pressFWD();
  156.         player.printCurrentSong();
  157.         player.pressFWD();
  158.         player.printCurrentSong();
  159.  
  160.         player.pressFWD();
  161.         player.printCurrentSong();
  162.         player.pressPlay();
  163.         player.printCurrentSong();
  164.  
  165.         player.pressFWD();
  166.         player.printCurrentSong();
  167.         player.pressStop();
  168.         player.printCurrentSong();
  169.  
  170.         player.pressFWD();
  171.         player.printCurrentSong();
  172.         player.pressREW();
  173.         player.printCurrentSong();
  174.  
  175.         System.out.println(player.toString());
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment