1 Replies - 113 Views - Last Post: 29 November 2017 - 02:41 AM Rate Topic: -----

#1 AnonMom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 17

Print Menu output incorrect

Posted 28 November 2017 - 09:28 PM

I am close to completing this assignment but I have a print menu that is showing up to many times in my output. Specifically when I output add a song option, output full playlist, or output by specific artist. I tried moving my menu outside of my do while loop but I am getting an error by doing that. Any advice on where I should start looking would be appreciated. I am building a playlist program using a linked list. I also want to print the current Position of song when I output song by specific artist. I am unable to find a code that works. I have used list.getSongAt(currPos)

public class Playlist {

   // variables
   private SongEntry headNode;
   private SongEntry tailNode;
   private int count;

   
   // Constructor  
   public Playlist() {
       this.headNode = null;
       this.tailNode = null;
       this.count = 0;
   }
   

   boolean isEmpty() {
       return (headNode == null);
   }

    //Displays all the songs in the playlist 
   void outputPlaylist(String title) {
       SongEntry song = this.headNode;
       int i = 1;

       System.out.println(title + " - OUTPUT FULL PLAYLIST");
       // Traverse the playlist
       while (song != null) {
           System.out.println(i + ".");
           song.printPlaylistSongs();
           song = song.getNext();
           i += 1;
       }
   }

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

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

   
   //Adds a song to the playlist   
   void addSong(String uniqueID, String songName, String artistName, int songLength) {
       // Create a new node
       SongEntry newSong = new SongEntry(uniqueID, songName, artistName, songLength);

       if (headNode == null) {
           headNode = newSong;
           tailNode = headNode;
           // Increment count
           count += 1;

       } else {
           if (!songExists(uniqueID)) {
               // Add new song after tail
               tailNode.setNext(newSong);

               // Make newSong as the tail
               tailNode = newSong;

               // Increment count
               count += 1;

           } else
               System.out.println("Song with ID " + uniqueID + " already exists.");
       }
   }

   
   //Removes a song with songID Returns the song name if it is removed   
   String removeSong(String uniqueID) {
       SongEntry song = this.headNode;
       String title = null;

       // Check if head or tail has songID
       if (headNode.getID().equalsIgnoreCase(uniqueID)) {
           title = headNode.getSongName();

           // Set head.next as new head
           headNode = headNode.getNext();

           // Decrease count
           count -= 1;

       } else {
           // Traverse the playlist
           while (song.getNext() != null) {
               if (song.getNext().getID().equalsIgnoreCase(uniqueID)) {
                   title = song.getNext().getSongName();

                   // Check if the target song was the last song
                   if (song.getNext().getNext() == null)
                       tailNode = song;

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

                   // Decrease count
                   count -= 1;
                   break;
               }
               song = song.getNext();
           }
       }

       return title;
   }

   
   //Returns the song name at index
   String getSongAt(int index) {
       SongEntry song = this.headNode;
       String title = null;
       int i = 1;

       // Traverse the playlist
       while (i != index) {
           song = song.getNext();
           i += 1;
       }

       return song.getSongName();
   }

   
   //Moves the song at position currPos to newPos 
   int changePosition(int currPos, int newPos) {
       int actualPos = -1;
       if (currPos != newPos) {
           // Goto the song at currPos - 1
           int pos = 1;
           SongEntry song = this.headNode;
           SongEntry target = null;
          
           if (currPos == 1) {   // Move the head
               // Set target as head
               target = headNode;
              
               // Set new head
               headNode = headNode.getNext();
              
           } else {
               // Traverse the playlist
               while (pos != (currPos - 1)) {
                   song = song.getNext();
                   pos += 1;
               }
  
               // Get the target song
               target = song.getNext();
  
               // Set song's next as target's next
               song.setNext(target.getNext());
              
               // Check if currPos is last node
               if (currPos == count)
                   tailNode = song;
           }

           if (newPos <= 1) {
               // Move song at currPos to the head
               // Set target's next as head's next
               target.setNext(headNode);

               // Set target as head
               headNode = target;

               actualPos = 1;

           } else if (newPos >= count) {
               // Move song at currPos to the tail
               // Set tail's next as taget
               tailNode.setNext(target);

               // Set target as tail
               tailNode = target;
               target.setNext(null);

               actualPos = count;

           } else {
               // Goto the song at newPos - 1
               song = this.headNode;
               pos = 1;
              
               // Traverse the playlist
               while (pos != (newPos - 1)) {
                   song = song.getNext();
                   pos += 1;
               }

               // Set target's next as song.next.next
               target.setNext(song.getNext());

               // Set song's next as target
               song.setNext(target);
              
               actualPos = newPos;
           }
       }

       return actualPos;
   }

  
   //Displays all songs by artist  
   void songByArtist(String artistName) {
       SongEntry song = this.headNode;

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

   /**
   * Returns the total playlist time
   *
   * @return
   */
   int totalPlaylistTime() {
       int time = 0;
       SongEntry song = this.headNode;

       // Traverse the playlist
       while (song != null) {
           time += song.getSongLength();
           song = song.getNext();
       }

       return time;
   }

   /**
    Clears the keyboard buffer
   
   static void clearBuffer(Scanner in) {
       in.nextLine();
   }
    */
   /**
   * Displays the menu to the user
   *
   * @param title
   *            - playlist's title
   * @param in
   *            - Scanner to get user input
   */
   static void printMenu(String title, Scanner in) {
       // Create playlist object
       Playlist list = new Playlist();
       String choice;

       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:");
           choice = in.nextLine();
         //  clearBuffer(in);
        
           switch (choice) {
           case "q": // Quit
           case "Q":
               break;

           case "a": // Add song
           case "A":
               System.out.println("ADD SONG");
               System.out.println("Enter song's unique ID:");
               String songID = in.nextLine();

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

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

               System.out.println("Enter song's length (in seconds):");
               int songLength = in.nextInt();
             //  clearBuffer(in);
               list.addSong(songID, songName, artistName, songLength);
               System.out.println();
               break;

           case "d": // Remove song
           case "D":
               System.out.println("REMOVE SONG");
               // Check if list is null
               if (!list.isEmpty()) {
                   System.out.println("Enter song's unique ID:");
                   songID = in.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": // Change position of song
           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 = in.nextInt();
                //       clearBuffer(in);

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

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

                   // Check newPos range
                   newPos = (newPos < 1) ? 1 : ((newPos > list.count) ? list.count : newPos);

                   newPos = list.changePosition(currPos, newPos);
                   if (newPos != -1)
                       System.out.println("\"" + list.getSongAt(newPos) + "\"" + " moved to position " + newPos);
                   else
                       System.out.println("No change");
               } else
                   System.out.println("Playlist is empty");
                   System.out.println();
               break;

           case "s": // Output songs by specific artist
           case "S":
               if (!list.isEmpty()) {
                   System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
                   System.out.println("Enter artist's name:\n");
               
                   artistName = in.nextLine();
                   //System.out.print(currNode);
                   //list.getNext(count);
                   list.songByArtist(artistName);
               } else
                   System.out.println("\nPlaylist is empty");
                   //System.out.println();
               break;

           case "t": // Output total time of playlist (in seconds)
           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");
                   //System.out.println();
               }
               break;

           case "o": // Output full playlist
           case "O":
               if (!list.isEmpty()) {
                   list.outputPlaylist(title);
               } else {
                   System.out.println(title + " - OUTPUT FULL PLAYLIST" + "\nPlaylist is empty");
                   System.out.println();
               }
               break;
           
           //default: // Invalid character
            //   System.out.println("\nInvalid choice!");
           }

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


   public static void main(String[] args) {

       // Get playlist title
       try ( // Scanner to get user input
               Scanner in = new Scanner(System.in)) {
           // Get playlist title
           System.out.println("Enter playlist's title:");
           String title = in.nextLine();
           System.out.println();
           // Display menu
           printMenu(title, in);
           // Close scanner
       }
   }
}
    
    



public class SongEntry {
     // Instance variables
   private String uniqueID;
   private String songName;
   private String artistName;
   private int songLength;
   private SongEntry nextNode;
  
   /**
   * Default constructor
   */
   public SongEntry() {
       this("none", "none", "none", 0);
   }

   /**
   * Parameterized constructor
   * @param uniqueID
   * @param songName
   * @param artistName
   * @param songLength
    
   */
   public SongEntry(String uniqueID, String songName, String artistName, int songLength) {
       this.uniqueID = uniqueID;
       this.songName = songName;
       this.artistName = artistName;
       this.songLength = songLength;
      
   }
  
   /**
   * Inserts a song after the currNode
   * @param currNode - song to be inserted
   */
   public void insertAfter(SongEntry currNode) {
       if (currNode == null)
       {
           return;
       }
           currNode.setNext(nextNode);
           nextNode = currNode;
       }
   
  
   /**
   * Sets the next song
   * @param nextNode - next song
   */
   public void setNext(SongEntry nextNode) {
       this.nextNode = nextNode;
   }
  
   /**
   * Returns the id of the song
   * @return
   */
   public String getID() {
       return this.uniqueID;
   }
  
   /**
   * Returns the song name
     * @return 
   */
   public String getSongName() {
       return this.songName;
   }
  
   /**
   * Returns the artist of the song
     * @return 
   */
   public String getArtistName() {
       return this.artistName;
   }
  
   /**
   * Returns the song length
     * @return 
   */
   public int getSongLength() {
       return this.songLength;
   }
  
   /**
   * Gets the next song
     * @return 
   */
   public SongEntry getNext() {
       return this.nextNode;
   }
  
  
   //Prints the songs in the playlist  
   public void printPlaylistSongs() {
       //System.out.println();
       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();
   }
}
    




Is This A Good Question/Topic? 0
  • +

Replies To: Print Menu output incorrect

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3487
  • View blog
  • Posts: 15,766
  • Joined: 20-September 08

Re: Print Menu output incorrect

Posted 29 November 2017 - 02:41 AM

static void reset(Scanner in) {
   if (in.hasNextLine()) {
       in.nextLine();
   }
}


Try calling reset immediately before every break
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1