Computer Science Canada

My remove LinkedList method not working

Author:  gummyw0rm [ Fri Mar 15, 2019 10:03 pm ]
Post subject:  My remove LinkedList method not working

hi guys i cannot seem to figure/solve why my remove method is not working. any help would be greatful!!

hi guys i want to switch this code into an if/else statements but am having trouble...


import java.util.Scanner;

public class Playlist {
public static String title;
static Scanner scnr = new Scanner(System.in);

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
SongEntry song;

System.out.println("Enter playlist's title:\n");
title = sc.nextLine();

song = new SongEntry();

printMenu(song, sc);


}

public static void menu() {
System.out.println(title + " PLAYLIST MENU");
System.out.println("a - Add song");
System.out.println("d - Remove song");
System.out.println("c - Change position of song");
System.out.println("s - Output songs by specific artist");
System.out.println("t - Output total time of playlist (in seconds)");
System.out.println("o - Output full playlist");
System.out.println("q - Quit");
System.out.println("");
}

public static void printMenu(SongEntry songList, Scanner scnr)
{
String choice;
String uniqueID, songName, artistName;
int songLength;
boolean select = true;

SongEntry headObj;
SongEntry currentObj;
SongEntry lastObj;

headObj = new SongEntry();
lastObj = headObj;

while(true) {
menu();

do {
System.out.println("Choose an option:");
choice = scnr.next().trim();

switch(choice.charAt(0))
{
case 'q':
select = false;
return;
case 'a':
scnr.nextLine(); // remove newline character from keyboard
System.out.println("ADD SONG");
System.out.println("Enter song's unique ID:");
uniqueID = scnr.nextLine();
System.out.println("Enter song's name:");
songName = scnr.nextLine();
System.out.println("Enter artist's name:");
artistName = scnr.nextLine();
System.out.println("Enter song's length (in seconds):");
songLength = Integer.parseInt(scnr.nextLine());
System.out.println("");

SongEntry song = new SongEntry();
song.setUniqueID(uniqueID);
song.setSongName(songName);
song.setArtistName(artistName);
song.setSongLength(songLength);
song.setNext(song);

// Add the item to linked list
song.addSong(song);
lastObj.insertAfter(song);
lastObj = song;

menu();
break;
case 'd':
scnr.nextLine();
System.out.println("REMOVE SONG");
System.out.println("Enter song's unique ID:");
uniqueID = scnr.nextLine();

currentObj = headObj;
lastObj.deleteSong(currentObj, uniqueID);

menu();
break;
case 'c':
scnr.nextLine();
System.out.println("CHANGE POSITION OF SONG");
System.out.println("Enter song's current position:");
int position = scnr.nextInt();
System.out.println("Enter new position for song:");
int newPosition = scnr.nextInt();
lastObj.changeSongPosition(headObj, position, newPosition);

menu();
break;
case 's':
scnr.nextLine();
System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
System.out.println("Enter artist's name:");
String artistID = scnr.nextLine();
lastObj.findArtist(headObj, artistID);

menu();
break;
case 't':
scnr.nextLine();
System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");
lastObj.totalTime(headObj);

menu();
break;
case 'o':
int count = 0;
currentObj = headObj;
currentObj = currentObj.getNext();
System.out.println(title + " - OUTPUT FULL PLAYLIST");
while (currentObj != null) {
count++;

currentObj.printSongEntry(currentObj, count);
currentObj = currentObj.getNext();
}
if (count == 0) {
System.out.println("Playlist is empty\n");
}

menu();
break;
}

} while (!choice.equals('q'));
}

}

}



----this is my SongEntry class-----
public class SongEntry {

// Private fields
private String uniqueID;
private String songName;
private String artistName;
private int songLength;
private SongEntry headNode;
private SongEntry currNode;
private SongEntry nextNode;

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

// Parameterized constructor
public SongEntry(String uniqueID, String songName, String artistName, int songLength) {

this.uniqueID = uniqueID;
this.songName = songName;
this.artistName = artistName;
this.songLength = songLength;

}

public String getID() {
return uniqueID;
}

public String getSongName() {
return songName;
}

public String getArtistName() {
return artistName;
}

public int getSongLength() {
return songLength;
}

public SongEntry getNext() {
return this.nextNode;
}

public SongEntry getHead() {
return this.headNode;
}

public SongEntry getCurrNode() {
return this.currNode;
}

public void printSongEntry(SongEntry currObj, int count) {
System.out.println(count + ".");
System.out.println("Unique ID: " + this.uniqueID);
System.out.println("Song Name: " + this.songName);
System.out.println("Artist Name: " + this.artistName);
System.out.println("Song Length (in seconds): " + this.songLength);
System.out.println("");
return;
}

public void insertAfter(SongEntry currNode) {
SongEntry tmpNext;

tmpNext = this.nextNode;
this.nextNode = currNode;
currNode.nextNode = tmpNext;
return;
}

public void addSong(SongEntry songList) {

SongEntry headObj;
SongEntry lastObj;

headObj = new SongEntry();
lastObj = headObj;
lastObj.insertAfter(songList);
}

public SongEntry isEmpty() {
return headNode = null;
}

public int length(SongEntry head) {
if (head == null) {
return 0;
}

int count = 0;

SongEntry currNode = head;
while(currNode != null) {
count++;
currNode = currNode.getNext();
}
return count;
}

public int findPosition(SongEntry head, String songID) {
int position = -1;
int count = 0;
boolean find = false;
SongEntry currNode = head;
int length = length(head);
while (find != true) {
count++;
if (count == length - 1) {
find = true;
}
currNode = currNode.getNext();
if (currNode.getID().equals(songID)) {
position = count;
find = true;
}
}
return position;
}

public String deleteSong(SongEntry head, String songID) {

String songRemoved = "";
int position = findPosition(head, songID);

int length = length(head);
if(position > length || position < 1) {
System.out.println("Song not found");
}
else {

SongEntry prevNode = head;
int count = 1;
while(count < position ) {
prevNode = prevNode.nextNode;
count++;
}

SongEntry curNode = prevNode.nextNode;
songRemoved = curNode.getSongName();
prevNode.nextNode = curNode.nextNode;
curNode.nextNode = null;
System.out.println("\"" + songRemoved + "\"" + " removed.");
System.out.println("");
}
return songRemoved;

}

public SongEntry getIndex(SongEntry head, int currPos) {
if (currPos == 0) return null;

SongEntry entry = head;
for (int i = 0; i < currPos; ++i) {
entry = entry.getNext();
}
return entry;
}


public SongEntry changeSongPosition(SongEntry head, int currentPos, int newPos) {
SongEntry linkedList = head;
int length = length(head);
SongEntry currNode = getIndex(linkedList, currentPos);
String songName = currNode.getSongName();
SongEntry prevNode = getIndex(linkedList, newPos);
String prevSongName = prevNode.getSongName();
System.out.println("Current song is " + songName + " and previous song name is " + prevSongName);

if(newPos < 1)
newPos = 1;

if (head==null){
return head;
}

int count = 0;
while(count < newPos)
{
count++;
}

return head;
}
public String removeSong(SongEntry head, String songID) { //delete a song
String songRemoved = "";
int position = findPosition(head, songID);

SongEntry prevNode = head;
int count = 1;
while(count < position ) {
prevNode = prevNode.nextNode;
count++;
}

SongEntry curNode = prevNode.nextNode;
songRemoved = curNode.getSongName(); //get name of song
prevNode.nextNode = curNode.nextNode;

curNode.nextNode = null;
return songRemoved;
}

public void findArtist(SongEntry head, String artistName) {
int count = 0;
SongEntry currNode = head;
while(currNode != null) {
if(currNode.getArtistName().equals(artistName)) {
currNode.printSongEntry(currNode, count);
}
currNode = currNode.nextNode;
count++;
}
}

public void totalTime(SongEntry head) {
int time = 0;
SongEntry currentNode = head;
while(currentNode != null) {
currentNode = currentNode.getNext();
if (currentNode != null) {
time = time + currentNode.getSongLength();
}
}

System.out.println("Total time: " + time + " seconds");
System.out.println("");
}
public void setUniqueID(String setUniqueID) {
this.uniqueID = setUniqueID;
}

public void setSongName(String setSongName) {
this.songName = setSongName;
}

public void setArtistName(String setArtistName) {
this.artistName = setArtistName;
}

public void setSongLength(int setSongLgth) {
this.songLength = setSongLgth;
}
public void setNext(SongEntry nextLoc) {
this.nextNode = nextLoc;
}
}


: