Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class PlayList {
  5. private Song[] list;
  6.  
  7. public PlayList() {
  8. list = new Song[1];
  9. list[0] = new Song();
  10. }
  11.  
  12. public PlayList(int x) {
  13. list = new Song[x];
  14. }
  15.  
  16. public void setList(int x) {
  17. list = new Song[x];
  18. }
  19.  
  20. public Song[] getList() {
  21. return list;
  22. }
  23.  
  24. public void addSong(int x, Song s) {
  25. int length = list.length;
  26. if (x < length) {
  27. list[x] = s;
  28. } else {
  29. Song[] newList = new Song[length + 1];
  30. for (int i = 0; i < length; i++) {
  31. newList[i] = list[i];
  32. }
  33. newList[length] = s;
  34. list = newList;
  35. }
  36. }
  37.  
  38. public Song getSong(int x) {
  39. return list[x];
  40. }
  41.  
  42. public int numSongs() {
  43. int n = 0;
  44. for (int i = 0; i < list.length; i++) {
  45. if (list[i] != null) { n++; }
  46. }
  47. return n;
  48. }
  49.  
  50. public int totalLength() {
  51. int t = 0;
  52. for (int i = 0; i < list.length; i++) {
  53. if (list[i] != null) {
  54. t += list[i].getLength();
  55. }
  56. }
  57. return t;
  58. }
  59.  
  60. public void removeArtist(String ar) {
  61. int ct = 0;
  62. for (int i = 0; i < list.length; i++) {
  63. if (list[i] != null) {
  64. if (list[i].getArtist().equals(ar)) {
  65. ct++;
  66. }
  67. }
  68. }
  69. Song[] newList = new Song[list.length - ct];
  70. int j = 0;
  71. for (int i = 0; i < list.length; i++) {
  72. if (list[i] != null) {
  73. if (!list[i].getArtist().equals(ar)) {
  74. newList[j] = list[i];
  75. j++;
  76. }
  77. }
  78. }
  79. list = newList;
  80. }
  81.  
  82. public void removeLength(int length) {
  83. int ct = 0;
  84. for (int i = 0; i < list.length; i++) {
  85. if (list[i] != null) {
  86. if (list[i].getLength() > length) {
  87. ct++;
  88. }
  89. }
  90. }
  91. Song[] newList = new Song[list.length - ct];
  92. int j = 0;
  93. for (int i = 0; i < list.length; i++) {
  94. if (list[i] != null) {
  95. if (list[i].getLength() <= length) {
  96. newList[j] = list[i];
  97. j++;
  98. }
  99. }
  100. }
  101. list = newList;
  102. }
  103.  
  104. public void shuffle() {
  105. Random rnd = new Random();
  106. for (int i = list.length - 1; i > 0; i--) {
  107. int index = rnd.nextInt(i+1);
  108. Song a = list[index];
  109. list[index] = list[i];
  110. list[i] = a;
  111. }
  112. }
  113.  
  114. public boolean equals(Object obj) {
  115. PlayList pl = (PlayList)obj;
  116. return Arrays.equals(list, pl.getList());
  117. }
  118.  
  119. public String toString() {
  120. return Arrays.toString(list);
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement