Niksi

НП MP3

Dec 26th, 2019
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4.  
  5. public class PatternTest {
  6. public static void main(String args[]) {
  7. List<Song> listSongs = new ArrayList<Song>();
  8. listSongs.add(new Song("first-title", "first-artist"));
  9. listSongs.add(new Song("second-title", "second-artist"));
  10. listSongs.add(new Song("third-title", "third-artist"));
  11. listSongs.add(new Song("fourth-title", "fourth-artist"));
  12. listSongs.add(new Song("fifth-title", "fifth-artist"));
  13. MP3Player player = new MP3Player(listSongs);
  14.  
  15.  
  16. System.out.println(player.toString());
  17. System.out.println("First test");
  18.  
  19.  
  20. player.pressPlay();
  21. player.printCurrentSong();
  22. player.pressPlay();
  23. player.printCurrentSong();
  24.  
  25. player.pressPlay();
  26. player.printCurrentSong();
  27. player.pressStop();
  28. player.printCurrentSong();
  29.  
  30. player.pressPlay();
  31. player.printCurrentSong();
  32. player.pressFWD();
  33. player.printCurrentSong();
  34.  
  35. player.pressPlay();
  36. player.printCurrentSong();
  37. player.pressREW();
  38. player.printCurrentSong();
  39.  
  40.  
  41. System.out.println(player.toString());
  42. System.out.println("Second test");
  43.  
  44.  
  45. player.pressStop();
  46. player.printCurrentSong();
  47. player.pressStop();
  48. player.printCurrentSong();
  49.  
  50. player.pressStop();
  51. player.printCurrentSong();
  52. player.pressPlay();
  53. player.printCurrentSong();
  54.  
  55. player.pressStop();
  56. player.printCurrentSong();
  57. player.pressFWD();
  58. player.printCurrentSong();
  59.  
  60. player.pressStop();
  61. player.printCurrentSong();
  62. player.pressREW();
  63. player.printCurrentSong();
  64.  
  65.  
  66. System.out.println(player.toString());
  67. System.out.println("Third test");
  68.  
  69.  
  70. player.pressFWD();
  71. player.printCurrentSong();
  72. player.pressFWD();
  73. player.printCurrentSong();
  74.  
  75. player.pressFWD();
  76. player.printCurrentSong();
  77. player.pressPlay();
  78. player.printCurrentSong();
  79.  
  80. player.pressFWD();
  81. player.printCurrentSong();
  82. player.pressStop();
  83. player.printCurrentSong();
  84.  
  85. player.pressFWD();
  86. player.printCurrentSong();
  87. player.pressREW();
  88. player.printCurrentSong();
  89.  
  90.  
  91. System.out.println(player.toString());
  92. }
  93. }
  94.  
  95. //Vasiot kod ovde
  96.  
  97. class Song{
  98. String title;
  99. String artist;
  100.  
  101. public Song(String title, String artist) {
  102. super();
  103. this.title = title;
  104. this.artist = artist;
  105. }
  106.  
  107. public String getTitle() {
  108. return title;
  109. }
  110.  
  111. public String getArtist() {
  112. return artist;
  113. }
  114.  
  115. @Override
  116. public String toString() {
  117. return String.format("Song{title=%s, artist=%s}", getTitle(),getArtist());
  118. }
  119.  
  120.  
  121.  
  122. }
  123.  
  124. class MP3Player{
  125. List<Song> list;
  126. int i=0;
  127. int pom=10;
  128. boolean stop=true;
  129. boolean play=false;
  130. public MP3Player(List<Song> list) {
  131. super();
  132. this.list = list;
  133. }
  134.  
  135. public void pressPlay() {
  136. if(play) {
  137. System.out.println("Song is already playing");
  138. return;
  139. }else {
  140. System.out.println(String.format("Song %d is playing", i));
  141. play=true;
  142. stop=false;
  143. pom=10;
  144. }
  145. }
  146.  
  147. public void pressStop() {
  148. if(stop&&i==0 && pom==-1) {
  149. System.out.println("Songs are already stopped");
  150. return ;
  151. }
  152. if(stop) {
  153. System.out.println("Songs are stopped");
  154. i=0;
  155. pom=-1;
  156. }else {
  157. stop=true;
  158. play=false;
  159. System.out.println(String.format("Song %d is paused", i));
  160. }
  161. }
  162.  
  163. public void pressREW() {
  164. System.out.println("Reward...");
  165. if(i-1<0) {
  166. i=list.size()-1;
  167. }else {
  168. i--;
  169. }
  170. play=false;
  171. stop=true;
  172. pom=10;
  173. }
  174. public void pressFWD() {
  175. System.out.println("Forward...");
  176. if(i+1==list.size()) {
  177. i=0;
  178. }else {
  179. i++;
  180. }
  181. play=false;
  182. stop=true;
  183. pom=10;
  184. }
  185.  
  186. public void printCurrentSong() {
  187. System.out.println(list.get(i).toString());
  188. }
  189.  
  190. public String songsToString() {
  191. return list.stream().map(Song::toString).collect(Collectors.joining(", "));
  192. }
  193.  
  194. @Override
  195. public String toString() {
  196. return "MP3Player{currentSong = " + i + ", songList = [" + songsToString() + "]}";
  197. }
  198.  
  199.  
  200. }
Add Comment
Please, Sign In to add comment