Question

You will be building a linked list. Make sure to keep track of both the head and tail nodes.

Ch 8 Program: Playlist (Java)


You will be building a linked list. Make sure to keep track of both the head and tail nodes.

(1) Create two files to submit.

SongEntry.java - Class declaration

Playlist.java - Contains main() method

Build the SongEntry class per the following specifications. Note: Some methods can initially be method stubs (empty methods), to be completed in later steps.

Private fields

String uniqueID - Initialized to "none" in default constructor

string songName - Initialized to "none" in default constructor

string artistName - Initialized to "none" in default constructor

int songLength - Initialized to 0 in default constructor

SongEntry nextNode - Initialized to null in default constructor

Default constructor (1 pt)

Parameterized constructor (1 pt)

Public member methods

void insertAfter(SongEntry currNode) (1 pt)

void setNext(SongEntry nextNode) - Mutator (1 pt)

String getID()- Accessor

String getSongName() - Accessor

String getArtistName() - Accessor

int getSongLength() - Accessor

SongEntry getNext() - Accessor

void printPlaylistSongs()

Ex. of printPlaylistSongs output:

Unique ID: S123
Song Name: Peg
Artist Name: Steely Dan
Song Length (in seconds): 237

(2) In main(), prompt the user for the title of the playlist. (1 pt)

Ex:

Enter playlist's title:
JAMZ


(3) Implement the printMenu() method. printMenu() takes the playlist title as a parameter and a Scanner object, outputs a menu of options to manipulate the playlist, and reads the user menu selection. Each option is represented by a single character. Build and output the menu within the method.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to execute the menu until the user enters q to Quit. (3 pts)

Ex:

JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:


(4) Implement "Output full playlist" menu option. If the list is empty, output: Playlist is empty (3 pts)

Ex:

JAMZ - OUTPUT FULL PLAYLIST
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length (in seconds): 237

2.
Unique ID: JJ234
Song Name: All For You
Artist Name: Janet Jackson
Song Length (in seconds): 391

3.
Unique ID: J345
Song Name: Canned Heat
Artist Name: Jamiroquai
Song Length (in seconds): 330

4.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length (in seconds): 197

5. 
Unique ID: SD567
Song Name: I Got The News
Artist Name: Steely Dan
Song Length (in seconds): 306


Ex (empty playlist):

JAMZ - OUTPUT FULL PLAYLIST
Playlist is empty


(5) Implement the "Add song" menu item. New additions are added to the end of the list. (2 pts)

Ex:

ADD SONG
Enter song's unique ID:
SD123
Enter song's name:
Peg
Enter artist's name:
Steely Dan
Enter song's length (in seconds):
237


(6) Implement the "Remove song" method. Prompt the user for the unique ID of the song to be removed.(4 pts)

Ex:

REMOVE SONG
Enter song's unique ID:
JJ234
"All For You" removed


(7) Implement the "Change position of song" menu option. Prompt the user for the current position of the song and the desired new position. Valid new positions are 1 - n (the number of nodes). If the user enters a new position that is less than 1, move the node to the position 1 (the head). If the user enters a new position greater than n, move the node to positionn (the tail). 6 cases will be tested:

Moving the head node (1 pt)

Moving the tail node (1 pt)

Moving a node to the head (1 pt)

Moving a node to the tail (1 pt)

Moving a node up the list (1 pt)

Moving a node down the list (1 pt)

Ex:

CHANGE POSITION OF SONG
Enter song's current position:
3
Enter new position for song:
2
"Canned Heat" moved to position 2


(8) Implement the "Output songs by specific artist" menu option. Prompt the user for the artist's name, and output the node's information, starting with the node's current position. (2 pt)

Ex:

OUTPUT SONGS BY SPECIFIC ARTIST
Enter artist's name:
Janet Jackson

2.
Unique ID: JJ234
Song Name: All For You
Artist Name: Janet Jackson
Song Length (in seconds): 391

4.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length (in seconds): 197


(9) Implement the "Output total time of playlist" menu option. Output the sum of the time of the playlist's songs (in seconds). (2 pts)

Ex:

OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)
Total time: 1461 seconds

My code:

SongEntry.java

public class SongEntry {
    private String uniqueID;
    private String songName;
    private String artistName;
    private int songLength;
    private SongEntry nextNode;

    // Default constructor.
    public SongEntry() {
        uniqueID = "none";
        songName = "none";
        artistName = "none";
        songLength = 0;
        nextNode = null;
    }

    public SongEntry(String uniqueID, String songName, String artistName, int songLength) {
        this.uniqueID = uniqueID;
        this.songName = songName;
        this.artistName = artistName;
        this.songLength = songLength;
    }

    public void insertAfter(SongEntry currNode) {
        if (currNode == null) {
            return;
        }
            currNode.setNext(nextNode);
            nextNode = currNode;
    }

    public void setNext(SongEntry nextNode) {

        this.nextNode = nextNode;
    }

    public SongEntry getNext() {

        return this.nextNode;
    }/**     * Mutator methods for uniqueID, songName, artistName, and songLength.     * These methods are used to set a value to the private fields I have initialized.     */    public void setUniqueID(String uniqueID) {

        this.uniqueID = uniqueID;
    }

    public void setSongName(String songName) {

        this.songName = songName;
    }

    public void setArtistName(String artistName) {

        this.artistName = artistName;
    }

    public void setSongLength(int songLength) {

        this.songLength = songLength;
    }

    // Accessor methods that are used to return the value of the private fields.
    public String getID() {

        return this.uniqueID;
    }

    public String getSongName() {

        return this.songName;
    }

    public String getArtistName() {

        return this.artistName;
    }

    public int getSongLength() {

        return this.songLength;
    }

    public void printPlaylistSongs() {
      System.out.println("Unique ID: " + getID());
      System.out.println("Song Name: " + getSongName());
      System.out.println("Artist Name: " + getArtistName());
      System.out.println("Song Length (in seconds): " + getSongLength());
      System.out.println("");
    }
}

Playlist.java

import java.util.Scanner;

public class Playlist {
    private SongEntry headNode;
    private SongEntry tailNode;
    private int x;

    public Playlist() {
        this.headNode = null;
        this.tailNode = null;
        this.x = 0;
    }

    boolean isEmpty() {

        return (headNode == null);
    }

    void outputPlaylist(String title) {
        SongEntry song = this.headNode;
        int i = 1;

        System.out.println(title + " - OUTPUT FULL PLAYLIST");

        while (song != null) {
            System.out.println(i + ".");
            song.printPlaylistSongs();
            song = song.getNext();
            i += 1;
        }
    }

    boolean songExists(String songID) {
        SongEntry song = this.headNode;

        while (song != null) {
            if (song.getID().equalsIgnoreCase(songID)) {
                return true;
            }
            song = song.getNext();
        }
        return false;
    }

    void addSong(String uniqueID, String songName, String artistName, int songLength) {

        SongEntry newSong = new SongEntry(uniqueID, songName, artistName, songLength);

        if (headNode == null) {
            headNode = newSong;
            tailNode = headNode;
            x += 1;

        } else {
            if (!songExists(uniqueID)) {
                tailNode.setNext(newSong);
                tailNode = newSong;
                x += 1;

            } else
                System.out.println("Song ID: " + uniqueID + " is already in the playlist.");
        }
    }

    String removeSong(String uniqueID) {
        SongEntry song = this.headNode;
        String title = null;

        if (headNode.getID().equalsIgnoreCase(uniqueID)) {
            title = headNode.getSongName();
            headNode = headNode.getNext();
            x -= 1;

        } else {

            while (song.getNext() != null) {

                if (song.getNext().getID().equalsIgnoreCase(uniqueID)) {
                    title = song.getNext().getSongName();
                    if (song.getNext().getNext() == null)
                        tailNode = song;

                    song.setNext(song.getNext().getNext());
                    x -= 1;
                    break;
                }
                song = song.getNext();
            }
        }

        return title;
    }

    String getSong(int index) {
        SongEntry song = this.headNode;
        String title = null;
        int i = 1;

        while (i != index) {
            song = song.getNext();
            i += 1;
        }
        return song.getSongName();
    }

    int changeSongPos(int currPos, int newPos) {
        int actualPos = -1;
        if (currPos != newPos) {
            int pos = 1;
            SongEntry song = this.headNode;
            SongEntry target = null;

            if (currPos == 1) {
                target = headNode;
                headNode = headNode.getNext();

            } else {
                while (pos != (currPos - 1)) {
                    song = song.getNext();
                    pos += 1;
                }

                target = song.getNext();
                song.setNext(target.getNext());

                if (currPos == x)
                    tailNode = song;
            }

            if (newPos = x) {
                tailNode.setNext(target);

                tailNode = target;
                target.setNext(null);

                actualPos = x;

            } else {
                song = this.headNode;
                pos = 1;

                while (pos != (newPos - 1)) {
                    song = song.getNext();
                    pos += 1;
                }

                target.setNext(song.getNext());
                song.setNext(target);

                actualPos = newPos;
            }
        }

        return actualPos;
    }

    void songByArtist(String artistName) {
        SongEntry song = this.headNode;

        while (song != null) {
            if (song.getArtistName().equalsIgnoreCase(artistName))
                song.printPlaylistSongs();
            song = song.getNext();
        }
    }

    int totalPlaylistTime() {
        int time = 0;
        SongEntry song = this.headNode;

        while (song != null) {
            time += song.getSongLength();
            song = song.getNext();
        }

        return time;
    }

    private static void printMenu(String title, Scanner scnr) {

        Playlist list = new Playlist();
        String menuOption;

        do {

            System.out.println(title + " PLAYLIST MENU" + "\na - Add song" + "\nd - Remove song"
                    + "\nc - Change position of song" + "\ns - Output songs by specific artist"
                    + "\nt - Output total time of playlist (in seconds)" + "\no - Output full playlist" + "\nq - Quit");
            System.out.println();
            System.out.println("Choose an option:");
            menuOption = scnr.nextLine();


            switch(menuOption) {

                case "q":
                case "Q":

                    break;

                case "a":
                case "A":

                    System.out.println("ADD SONG");
                    System.out.println("Enter song's unique ID:");
                    String songID = scnr.nextLine();

                    System.out.println("Enter song's name:");
                    String songName = scnr.nextLine();

                    System.out.println("Enter artist's name:");
                    String artistName = scnr.nextLine();

                    System.out.println("Enter song's length (in seconds):");
                    int songLength = scnr.nextInt();

                    list.addSong(songID, songName, artistName, songLength);
                    System.out.println();

                    break;

                case "d":
                case "D":

                    System.out.println("REMOVE SONG");
                    if (!list.isEmpty()) {
                        System.out.println("Enter song's unique ID:");
                        songID = scnr.nextLine();

                        String removedSong = list.removeSong(songID);
                        System.out.println((removedSong == null) ? ("Cannot find the song with id " + songID)
                                : ("\"" + removedSong + "\"" + " " + "removed."));
                    } else

                        System.out.println("Playlist is empty");
                        System.out.println();

                    break;

                case "c":
                case "C":

                    if (!list.isEmpty()) {
                        System.out.println("CHANGE POSITION OF SONG");

                        int currPos = -1;

                        do {

                            System.out.println("Enter song's current position:");
                            currPos = scnr.nextInt();

                            if ((currPos < 1) || (currPos > list.x))
                                System.out.println("Invalid current position. Please try again.");

                        } while ((currPos < 1 || currPos > list.x));

                        System.out.println("Enter new position for song:");
                        int newPos = scnr.nextInt();

                        newPos = (newPos < 1) ? 1 : ((newPos > list.x) ? list.x : newPos);
                        newPos = list.changeSongPos(currPos, newPos);

                        if (newPos != -1)
                            System.out.println("\"" + list.getSong(newPos) + "\"" + " moved to position " + newPos);

                        else
                            System.out.println("No change");

                    } else

                        System.out.println("Playlist is empty");
                        System.out.println();

                    break;

                case "s":
                case "S":

                    if (!list.isEmpty()) {
                        System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
                        System.out.println("Enter artist's name:\n");

                        artistName = scnr.nextLine();
                        list.songByArtist(artistName);

                    } else
                        System.out.println("\nPlaylist is empty");

                    break;

                case "t":
                case "T":

                    if (!list.isEmpty()) {
                        System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");
                        System.out.println("Total time: " + list.totalPlaylistTime() + " seconds\n");
                    } else {
                        System.out.println(title);
                        System.out.println("\nPlaylist is empty");
                    }
                    break;

                case "o":
                case "O":

                    if (!list.isEmpty()) {
                        list.outputPlaylist(title);
                    } else {
                        System.out.println(title + " - OUTPUT FULL PLAYLIST" + "\nPlaylist is empty");
                        System.out.println();
                    }
                    break;
            }

        } while (!menuOption.equalsIgnoreCase("q"));
    }

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        System.out.println("Enter playlist's title:");
        String title = scnr.nextLine();
        System.out.println();printMenu(title, scnr);

    }
}// end of Playlist.java.

This is an example of the output I'm getting when a user enters:

JAMZ

a

SD123

Peg

Steely Dan

237

o

q

ADD SONG

Enter song's unique ID:
Enter song's name:
Enter artist's name:
Enter song's length (in seconds):

JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:
JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:
JAMZ - OUTPUT FULL PLAYLIST
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length (in seconds): 237

JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:

What's bolded "JAMZ PLAYLIST MENU" is only suppose to print once.

It's meant to look like this:

ADD SONG
Enter song's unique ID:
Enter song's name:
Enter artist's name:
Enter song's length (in seconds):

JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:
JAMZ - OUTPUT FULL PLAYLIST
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length (in seconds): 237

JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option

Here's one more example:

CHANGE POSITION OF SONG
Enter song's current position:
Enter new position for song:
"Peg" moved to position 3

JAMZ PLAYLIST MENU  
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:
JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:
JAMZ - OUTPUT FULL PLAYLIST
1.
Unique ID: JJ234
Song Name: All For You
Artist Name: Janet Jackson
Song Length (in seconds): 391

2.
Unique ID: J345
Song Name: Canned Heat
Artist Name: Jamiroquai
Song Length (in seconds): 330

3.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length (in seconds): 237

4.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length (in seconds): 197

5.
Unique ID: SD567
Song Name: I Got The News
Artist Name: Steely Dan
Song Length (in seconds): 306

JAMZ PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit

Choose an option:

I can't figure out what I'm doing wrong here. Been working on this none stop for the last few days so I'm pretty exhausted and annoyed. Maybe I just need some rest and I'll figure it out.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Playlist.java


import java.util.Scanner;

public class Playlist {

    private String title;
    private SongEntry head;
    private static String stringPlaylistEmpty = "Playlist is empty";

    private void changePosition(Scanner scan) {

        int inputCurrPosition;
        int inputNewPosition;
        SongEntry se;
        SongEntry currPrevSe;
        SongEntry currNextSe;
        SongEntry newPrevSe;
        SongEntry newNextSe;

        System.out.println("\nCHANGE POSITION OF SONG");

        System.out.println("Enter song's current position:");
        inputCurrPosition = scan.nextInt();
        scan.nextLine();//clear the scanner
        if (inputCurrPosition > this.getLength()) {
            System.out.println("Error: invalid position. Playlist " + this.getTitle()
                    + " consists of " + this.getLength() + " song entries.");
            return;
        }

        System.out.println("Enter new position for song:");
        inputNewPosition = scan.nextInt();
        scan.nextLine();//clear the scanner
        if (inputNewPosition > this.getLength()) {
            inputNewPosition = this.getLength();
        }

        //If the user enters a new position that is less than 1, move the node to the position 1 (the head).
//        if (inputNewPosition < 1 && inputCurrPosition > 1/*if the node is already the head, no need to do anything*/) {
        if (
            //nothing needs to be done if head node is to changed to head position
            // or tail node is to be changed to tail position
            // or current position is the same as new position
                !((inputNewPosition < 2 && inputCurrPosition == 1)
                        || (inputNewPosition >= this.getLength()) && inputCurrPosition == this.getLength())
                        || (inputCurrPosition != inputNewPosition)
                ) {
            se = getEntryAtIndex(inputCurrPosition);
            currPrevSe = (getEntryAtIndex(inputCurrPosition - 1) == null) ? null : getEntryAtIndex(inputCurrPosition - 1);
            currNextSe = (getEntryAtIndex(inputCurrPosition + 1) == null) ? null : getEntryAtIndex(inputCurrPosition + 1);

            //take node out cleanly
            if (inputCurrPosition == 1) {
                this.setHead(currNextSe);
            } else if (inputCurrPosition == this.getLength()) {
                currPrevSe.setNext(null);
            } else {
                currPrevSe.setNext(currNextSe);
            }

            newPrevSe = (getEntryAtIndex(inputNewPosition - 1) == null) ? null : getEntryAtIndex(inputNewPosition - 1);
            newNextSe = (getEntryAtIndex(inputNewPosition) == null) ? null : getEntryAtIndex(inputNewPosition);

            //add node at new position
            if (inputNewPosition == 1) {
                this.setHead(se);
                se.setNext(newNextSe);
            } else if (inputNewPosition == this.getLength() + 1) {
                se.setNext(null);
                newPrevSe.setNext(se);
            } else {
                newPrevSe.setNext(se);
                se.setNext(newNextSe);
            }
        }
    }

    private SongEntry getEntryAtIndex(int inputCurrPosition) {
        if (inputCurrPosition == 0) return null;

        SongEntry se = this.getHead();
        for (int i = 0; i < inputCurrPosition - 1; ++i) {
            se = se.getNext();
        }
        return se;
    }


    private void removeSong(Scanner scan) {

        String inputUid;
        SongEntry prevSE;

        System.out.println("\nREMOVE SONG");
        if (this.getHead() == null) {
            System.out.println(stringPlaylistEmpty);
        } else {
            System.out.println("Enter song's unique ID:");
            inputUid = scan.nextLine();

            //removing HEAD is a little special
            if (inputUid.equals(this.getHead().getID())) {
                if (this.getHead().getNext() == null) {
                    this.setHead(null);
                } else {
                    this.setHead(this.getHead().getNext());
                }
            } else {
                prevSE = getEntryB4Match(inputUid);
                if (prevSE == null) {
                    System.out.println("Entry does not exist.");
                } else /*entry exists*/ if (prevSE.getNext().getNext() != null) {
                    prevSE.setNext(prevSE.getNext().getNext());
                } else /*remove last entry*/ {
                    prevSE.setNext(null);
                }
            }
        }
    }

    private SongEntry getEntryB4Match(String inputUid) {
        SongEntry prevSE = this.getHead();
        while (prevSE.getNext() != null) {
            if (prevSE.getNext().getID().equals(inputUid)) {
                return prevSE;
            }
            prevSE = prevSE.getNext();
        }
        return null;
    }

    private void outputFullPlaylist() {

        int count = 0;
        SongEntry currSong = this.getHead();

        System.out.println(this.getTitle() + " - OUTPUT FULL PLAYLIST");

        if (this.getHead() == null) {
            System.out.println(stringPlaylistEmpty);
        } else {
            System.out.println(++count + ".");
            this.getHead().printPlaylistSongs();

            while (currSong.getNext() != null) {
                currSong = currSong.getNext();
                System.out.println(++count + ".");
                currSong.printPlaylistSongs();
            }
        }
    }

    private SongEntry getTail() {

        SongEntry tail;
        SongEntry currSE;

        if (this.getHead() == null) {
            tail = null;
        } else if (this.getHead().getNext() == null) {
            tail = this.getHead();
        } else {
            currSE = this.getHead().getNext();
            while (currSE.getNext() != null) {
                currSE = currSE.getNext();
            }
            tail = currSE;
        }
        return tail;
    }

    public Playlist(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public SongEntry getHead() {
        return head;
    }

    public void setHead(SongEntry head) {
        this.head = head;
    }

    public int getLength() {

        int length = 0;
        SongEntry currSE;

        if (this.getHead() != null) {
            length++;
            currSE = this.getHead();

            while (currSE.getNext() != null) {
                length++;
                currSE = currSE.getNext();
            }
        }

        return length;
    }

    private void outputArtistSongs(Scanner scan) {

        System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
        System.out.println("Enter artist's name:");
        String artist = scan.nextLine().replaceAll("\\s", "").toLowerCase();

        int count = 1;
        SongEntry currSong;


        if (this.getHead() == null) {
            System.out.println(stringPlaylistEmpty);
        } else {
            currSong = this.getHead();
//            System.out.println(++count + ".");
            if (currSong.getArtistName().replaceAll("\\s", "").toLowerCase().equals(artist)) {
                System.out.println(++count + ".");
                currSong.printPlaylistSongs();
            }

            while (currSong.getNext() != null) {
                currSong = currSong.getNext();
                if (currSong.getArtistName().replaceAll("\\s", "").toLowerCase().equals(artist)) {
                    System.out.println(++count + ".");
                    currSong.printPlaylistSongs();
                }
            }
        }
    }

    private void addSong(Scanner scan) {
        String inputUid;
        String inputSongTitle;
        String inputArtistName;
        int inputLength;

        System.out.println("\nADD SONG");

        //uncomment for deliverable
        System.out.println("Enter song's unique ID:");
        inputUid = scan.nextLine();
        System.out.println("Enter song's name:");
        inputSongTitle = scan.nextLine();
        System.out.println("Enter artist's name:");
        inputArtistName = scan.nextLine();
        System.out.println("Enter song's length (in seconds):");
        inputLength = scan.nextInt();
        scan.nextLine();//clear the scanner

        SongEntry se = new SongEntry(inputUid, inputSongTitle, inputArtistName, inputLength, null);

        if (this.getHead() == null) {
            this.setHead(se);
        } else {
            se.insertAfter(this.getTail());
        }
        //END OF uncomment for deliverable


        //FOR TESTING ONLY - autopopulate songs
//        for (int i = 1; i <= 7; ++i) {
//            SongEntry se2 = new SongEntry(Integer.toString(i), Integer.toString(i), Integer.toString(i), i, null);
//
//            if (this.getHead() == null) {
//                this.setHead(se2);
//            } else {
//                se2.insertAfter(this.getTail());
//            }
//        }
        //end of FOR TESTING ONLY
    }

    private void processOption(String inputOption, Scanner scan) {

        switch (inputOption) {
            case "a":
                addSong(scan);
                break;
            case "d":
                removeSong(scan);
                break;
            case "c":
                changePosition(scan);
                break;
            case "s":
                outputArtistSongs(scan);
                break;
            case "t":
                outputTotalTime();
                break;
            case "o":
                outputFullPlaylist();
                break;
            //only for testing; remove for deliverable
            case "gl":
                System.out.println("length = " + this.getLength());
                break;
            //END OF only for testing; remove for deliverable
            case "q":
                break;
            default:
                System.out.println("Invalid option entered. Please enter valid menu option.");

        }
    }

    private void outputTotalTime() {
        System.out.printf("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)\n" +
                "Total time: %s seconds\n", this.getTotalTime());
    }

    private int getTotalTime() {
        SongEntry se;
        int time = 0;
        if (this.getHead() == null) {
            return time;
        }
        se = this.getHead();
        time += se.getSongLength();
        while (se.getNext() != null) {
            se = se.getNext();
            time += se.getSongLength();
        }
        return time;
    }


    private void printMenu() {
        System.out.println(this.getTitle().toUpperCase() + " PLAYLIST MENU\n" +
                "a - Add song\n" +
                "d - Remove song\n" +
                "c - Change position of song\n" +
                "s - Output songs by specific artist\n" +
                "t - Output total time of playlist (in seconds)\n" +
                "o - Output full playlist\n" +
                "q - Quit\n" +
                "\n" +
                "Choose an option:");
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input_title;
        String inputOption;

        System.out.println("Enter playlist's title:");
        input_title = scan.nextLine().toUpperCase();

        Playlist pl = new Playlist(input_title);

        do {
            System.out.println();
            pl.printMenu();
            inputOption = scan.next().toLowerCase();
            scan.nextLine();//clear the scanner
            pl.processOption(inputOption, scan);
        } while (!inputOption.equals("q"));
    }
}

SongEntry.java

public class SongEntry {

    private String uniqueID;
    private String songName;
    private String artistName;
    private int songLength;
    private SongEntry nextNode;

    public SongEntry() {
        this.uniqueID = "none";
        this.songName = "none";
        this.artistName = "none";
        this.songLength = 0;
        this.nextNode = null;
    }

    public SongEntry(String uniqueID, String songName, String artistName, int songLength, SongEntry nextNode) {
        this.uniqueID = uniqueID;
        this.songName = songName;
        this.artistName = artistName;
        this.songLength = songLength;
        this.nextNode = nextNode;
    }

    //    Public member methods
    public void insertAfter(SongEntry currNode) {
        currNode.setNext(this);
    }

    public void setNext(SongEntry nextNode) {
        this.nextNode = nextNode;
    }

    public String getID() {
        return uniqueID;
    }

    public String getSongName() {
        return songName;
    }

    public String getArtistName() {
        return artistName;
    }

    public int getSongLength() {
        return songLength;
    }

    public SongEntry getNext() {
        return nextNode;
    }

    public void printPlaylistSongs() {
        System.out.println("Unique ID: " + uniqueID);
        System.out.println("Song Name: " + songName);
        System.out.println("Artist Name: " + artistName);
        System.out.println("Song Length (in seconds): " + songLength + "\n");
    }

}


Add a comment
Know the answer?
Add Answer to:
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work....

    Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work. You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. Playlist.h - Class declaration Playlist.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1...

  • THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...

    THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact...

    Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact Disc.java (see Code Listing 7.2) and Classics.txt (see Code Listing 7.3) from the Student Files or as directed by your instructor. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by Compact Disc.java, the file you will be editing. 2. In Compact Disc.java, there are comments indicating where the missing code is to be placed. Declare...

  • Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player --...

    Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player -- sorting song lists. In this assignment, you are asked to design a graphic user interface (GUI) for this function. To start, create a Java project named CS235A4_YourName. Then, copy the class Singer and class Song from finished Lab 4 and paste into the created project (src folder). Define another class TestSongGUI to implement a GUI application of sorting songs. Your application must provide the...

  • 20.6 Lab: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes (1) Create three files to submit. ContactNode.h-Class declaration ContactNode.cpp- Class...

    20.6 Lab: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes (1) Create three files to submit. ContactNode.h-Class declaration ContactNode.cpp- Class definition main.cpp-main0 function (2) Build the ContactNode class per the following specifications Parameterized constructor. Parameters are name followed by phone number Public member functions InsertAfter0 (2 pts) GetName0 -Accessor(1 pt) GetPhoneNumber- Accessor (1 pt) GetNext0-Accessor (1 pt) PrintContactNode Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex....

  • Introduction Welcome to Rad.io, you've been hired to work on our music streaming app, think of...

    Introduction Welcome to Rad.io, you've been hired to work on our music streaming app, think of it as Spotify only more rad! You're in charge of handling our customer’s song list. When a user selects a playlist it will load into the list a number of songs. Users can skip to the next song, move to the previous, they can select a song to play next or select a song to add to the end of their list. Objective You...

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer)...

    Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer) (Please answer if it is correct and working) (Have been getting many wrong and spam answers lately) Introduction: In this project, we use the same file structure of Artist and Art to extend the concepts of array and linked list and have them applied to sorting. The input files of p1arts.txt and p1artists.txt have been slightly modified. They are named p7arts.txt and p7artists.txt. Assignments:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT