Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Да се напише класа за MP3Player во која се чуваат листа со песни (List<Song>) и песната која моментално се слуша (е на ред да се пушти). MP3Player-от има четири копчиња Play, Stop, FWD и REW.
- Ако се притисне копчето Play се пушта моменталната песна (на екранот се испишува "Song i is playing", каде i е редниот број на моменталната песна, почнувајќи од 0).
- Ако се притисне копчето Stop:
- моменталната песна кој е пуштена се паузира (на екран се испишува "Song i is paused" каде i е моменталната песна која била пуштена).
- листата целосно се ресетира од почеток, ако моменталната песна веќе била паузирана (на екран се испишува "Songs are stopped").
- Ако се притисне копчето FWD песната се паузира и следната песна од листата станува моментална (да се земе во предвид кружното повторување на песните).
- Ако се притисне копчето REW песната се паузира и претходната песна од листата станува моментална (да се земе во предвид кружното повторување на песните).
- За секоја песна (Song) се чуваат насловот на песната (String) и изведувачот на песната (String).
- */
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- class Song {
- String title;
- String artist;
- public Song(String title, String artist) {
- super();
- this.title = title;
- this.artist = artist;
- }
- @Override
- public String toString() {
- return "Song{title=" + title + ", artist=" + artist + "}";
- }
- }
- class MP3Player {
- List<Song> songs;
- int current;
- boolean playing;
- boolean paused;
- public MP3Player(List<Song> songs) {
- this.songs = new ArrayList<>();
- songs.forEach(song -> this.songs.add(song));
- this.current = 0;
- this.playing = false;
- this.paused = false;
- }
- public void pressPlay() {
- System.out.printf("Song %s playing\n", playing ? "is already" : current + " is");
- playing = true;
- paused = false;
- }
- public void printCurrentSong() {
- System.out.println(songs.get(current));
- }
- public void pressStop() {
- if (playing) {
- playing = false;
- paused = true;
- System.out.println("Song " + current + " is paused");
- } else if (paused) {
- paused = false;
- current = 0;
- System.out.println("Songs are stopped");
- } else
- System.out.println("Songs are already stopped");
- }
- public void pressFWD() {
- current = (current + 1) % songs.size();
- playing = false;
- paused = true;
- System.out.println("Forward...");
- }
- public void pressREW() {
- current = (songs.size() + --current) % songs.size();
- playing = false;
- paused = true;
- System.out.println("Reward...");
- }
- public String songsToString() {
- return songs.stream().map(Song::toString).collect(Collectors.joining(", "));
- }
- @Override
- public String toString() {
- return "MP3Player{currentSong = " + current + ", songList = [" + songsToString() + "]}";
- }
- }
- public class PatternTest {
- public static void main(String args[]) {
- List<Song> listSongs = new ArrayList<Song>();
- listSongs.add(new Song("first-title", "first-artist"));
- listSongs.add(new Song("second-title", "second-artist"));
- listSongs.add(new Song("third-title", "third-artist"));
- listSongs.add(new Song("fourth-title", "fourth-artist"));
- listSongs.add(new Song("fifth-title", "fifth-artist"));
- MP3Player player = new MP3Player(listSongs);
- System.out.println(player.toString());
- System.out.println("First test");
- player.pressPlay();
- player.printCurrentSong();
- player.pressPlay();
- player.printCurrentSong();
- player.pressPlay();
- player.printCurrentSong();
- player.pressStop();
- player.printCurrentSong();
- player.pressPlay();
- player.printCurrentSong();
- player.pressFWD();
- player.printCurrentSong();
- player.pressPlay();
- player.printCurrentSong();
- player.pressREW();
- player.printCurrentSong();
- System.out.println(player.toString());
- System.out.println("Second test");
- player.pressStop();
- player.printCurrentSong();
- player.pressStop();
- player.printCurrentSong();
- player.pressStop();
- player.printCurrentSong();
- player.pressPlay();
- player.printCurrentSong();
- player.pressStop();
- player.printCurrentSong();
- player.pressFWD();
- player.printCurrentSong();
- player.pressStop();
- player.printCurrentSong();
- player.pressREW();
- player.printCurrentSong();
- System.out.println(player.toString());
- System.out.println("Third test");
- player.pressFWD();
- player.printCurrentSong();
- player.pressFWD();
- player.printCurrentSong();
- player.pressFWD();
- player.printCurrentSong();
- player.pressPlay();
- player.printCurrentSong();
- player.pressFWD();
- player.printCurrentSong();
- player.pressStop();
- player.printCurrentSong();
- player.pressFWD();
- player.printCurrentSong();
- player.pressREW();
- player.printCurrentSong();
- System.out.println(player.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment