Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- 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());
- }
- }
- //Vasiot kod ovde
- class Song{
- String title;
- String artist;
- public Song(String title, String artist) {
- super();
- this.title = title;
- this.artist = artist;
- }
- public String getTitle() {
- return title;
- }
- public String getArtist() {
- return artist;
- }
- @Override
- public String toString() {
- return String.format("Song{title=%s, artist=%s}", getTitle(),getArtist());
- }
- }
- class MP3Player{
- List<Song> list;
- int i=0;
- int pom=10;
- boolean stop=true;
- boolean play=false;
- public MP3Player(List<Song> list) {
- super();
- this.list = list;
- }
- public void pressPlay() {
- if(play) {
- System.out.println("Song is already playing");
- return;
- }else {
- System.out.println(String.format("Song %d is playing", i));
- play=true;
- stop=false;
- pom=10;
- }
- }
- public void pressStop() {
- if(stop&&i==0 && pom==-1) {
- System.out.println("Songs are already stopped");
- return ;
- }
- if(stop) {
- System.out.println("Songs are stopped");
- i=0;
- pom=-1;
- }else {
- stop=true;
- play=false;
- System.out.println(String.format("Song %d is paused", i));
- }
- }
- public void pressREW() {
- System.out.println("Reward...");
- if(i-1<0) {
- i=list.size()-1;
- }else {
- i--;
- }
- play=false;
- stop=true;
- pom=10;
- }
- public void pressFWD() {
- System.out.println("Forward...");
- if(i+1==list.size()) {
- i=0;
- }else {
- i++;
- }
- play=false;
- stop=true;
- pom=10;
- }
- public void printCurrentSong() {
- System.out.println(list.get(i).toString());
- }
- public String songsToString() {
- return list.stream().map(Song::toString).collect(Collectors.joining(", "));
- }
- @Override
- public String toString() {
- return "MP3Player{currentSong = " + i + ", songList = [" + songsToString() + "]}";
- }
- }
Add Comment
Please, Sign In to add comment