Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- public class PlayList {
- private Song[] list;
- public PlayList() {
- list = new Song[1];
- list[0] = new Song();
- }
- public PlayList(int x) {
- list = new Song[x];
- }
- public void setList(int x) {
- list = new Song[x];
- }
- public Song[] getList() {
- return list;
- }
- public void addSong(int x, Song s) {
- int length = list.length;
- if (x < length) {
- list[x] = s;
- } else {
- Song[] newList = new Song[length + 1];
- for (int i = 0; i < length; i++) {
- newList[i] = list[i];
- }
- newList[length] = s;
- list = newList;
- }
- }
- public Song getSong(int x) {
- return list[x];
- }
- public int numSongs() {
- int n = 0;
- for (int i = 0; i < list.length; i++) {
- if (list[i] != null) { n++; }
- }
- return n;
- }
- public int totalLength() {
- int t = 0;
- for (int i = 0; i < list.length; i++) {
- if (list[i] != null) {
- t += list[i].getLength();
- }
- }
- return t;
- }
- public void removeArtist(String ar) {
- int ct = 0;
- for (int i = 0; i < list.length; i++) {
- if (list[i] != null) {
- if (list[i].getArtist().equals(ar)) {
- ct++;
- }
- }
- }
- Song[] newList = new Song[list.length - ct];
- int j = 0;
- for (int i = 0; i < list.length; i++) {
- if (list[i] != null) {
- if (!list[i].getArtist().equals(ar)) {
- newList[j] = list[i];
- j++;
- }
- }
- }
- list = newList;
- }
- public void removeLength(int length) {
- int ct = 0;
- for (int i = 0; i < list.length; i++) {
- if (list[i] != null) {
- if (list[i].getLength() > length) {
- ct++;
- }
- }
- }
- Song[] newList = new Song[list.length - ct];
- int j = 0;
- for (int i = 0; i < list.length; i++) {
- if (list[i] != null) {
- if (list[i].getLength() <= length) {
- newList[j] = list[i];
- j++;
- }
- }
- }
- list = newList;
- }
- public void shuffle() {
- Random rnd = new Random();
- for (int i = list.length - 1; i > 0; i--) {
- int index = rnd.nextInt(i+1);
- Song a = list[index];
- list[index] = list[i];
- list[i] = a;
- }
- }
- public boolean equals(Object obj) {
- PlayList pl = (PlayList)obj;
- return Arrays.equals(list, pl.getList());
- }
- public String toString() {
- return Arrays.toString(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement