Hey guys,
i have a linkedlist called CDArtist and one called CDTitle. i have all the functions and classes written and working except one which i have no idea how to. I need to be able to delete a CD by the name of the title or artist but this has to be done in both. (one is sorted by title, one by artist.) to do this i need a search method which takes a string but i cannot seem to come up with a working one? any help would be apreciated.

public class SortedList extends ListReferenceBased implements SortedListInterface{
    public SortedList(){
    }
    
    public void insertArtist(MyComparable newItem){
        int newPosition=locateIndexArtist(newItem);
        add(newPosition, newItem);
    }
    
    public void insertTitle(MyComparable newItem){
        int newPosition=locateIndexTitle(newItem);
        add(newPosition, newItem);
    }

    public void removeArtist(MyComparable Item) throws ListException{
        int position=locateIndexArtist(Item);
        if((Item.compareTo1(retrieveSort(position))==0)){
            remove(position);
        }
        else{
            throw new ListException("Remove Failed");
        }
    }
    public void removeTitle(MyComparable Item) throws ListException{
        int position=locateIndexTitle(Item);
        if((Item.compareTo2(retrieveSort(position))==0)){
            remove(position);
        }
        else{
            throw new ListException("Remove Failed");
        }
    }
    
    public int locateIndexArtist(MyComparable Item){
        int index1=1;
        while ((index1<=size()) && (Item.compareTo1(retrieveSort(index1))>0)){
            ++index1;
        }
        return index1;
    }
    
    public int locateIndexTitle(MyComparable Item){
        int index2=1;
        while ((index2<=size()) && (Item.compareTo2(retrieveSort(index2))>0)){
            ++index2;
        }
        return index2;
    }
    public MyComparable retrieveSort(int index){
        return (MyComparable)retrieve(index);
    }
    
}
import java.util.*;



public class SMOA2 extends SortedList{
    
    public static void Welcome(){
        System.out.print("Welcome to SMOA2. \n" +
                "\tTo insert a CD please enter \"1\"\n" +
                "\tTo delete a CD please enter \"2\"\n" +
                "\tTo modify CD information please enter\"3\"\n" +
                "\tTo display CD by artist or group name enter \"4\"\n" +
                "\tTo display CD by CD title enter \"5\"\n" +
                "\tTo display information regarding a specific CD enter \"6\"\n" +
                "\tTo display the number of CD's in the library enter \"7\"\n" +
                "\tTo exit the program please enter \"8\"\n" +
                "\tTo see this information again enter\"help\"\n" +
                "\tPlease enter your choice: ");
        
    } //closes Welcome

    public static MusicCD getInfo(String Title, String Artist, String Genre, String Release){
        MusicCD Info;
        Info=new MusicCD(Title, Artist, Genre, Release);
        return Info;
    }
    public static void main(String[] args) {
        String Title;
        String Artist;
        String Genre;
        String Release;
        String Command;
        String DelBy;
        String DelCmd;
        SortedList CDArtist= new SortedList();
        SortedList CDTitle= new SortedList();
        Scanner scan=new Scanner(System.in);
        Welcome();
        Command=(scan.next()).toLowerCase();
        while(!Command.equals("8")){
            if (Command.equals("help")){
                Welcome();
                Command=scan.next();
                continue;
            } //closes "help" loop
            else if (Command.equals("1")){
                System.out.print("Please enter the title: ");
                Title=scan.next().toLowerCase();
                System.out.print("Please enter the Artist: ");
                Artist=scan.next().toLowerCase();
                System.out.print("Please enter the Genre: ");
                Genre=scan.next().toLowerCase();
                System.out.print("Please enter the release year: ");
                Release=scan.next().toLowerCase();
                CDArtist.insertArtist(new MusicCD(Title, Artist, Genre, Release));
                CDTitle.insertTitle(new MusicCD(Title, Artist, Genre, Release));
                System.out.print("Please enter What you wish to do: ");
                Command=scan.next();
                
            } // closes "1" loop
            else if(Command.equals("2")){

            }//closes "2" loop
            else if(Command.equals("3")){
                
            }//closes "3" loop
            else if(Command.equals("4")){
                for (int x=CDArtist.size(); x>=1; x--){
                    System.out.print(((MusicCD)CDArtist.retrieve(x)).GetArtist()+ "\n");
                }
                System.out.print("What would you like to do next: ");
                Command=scan.next();
                
            } //closes "4" loop
            else if(Command.equals("5")){
                for(int check=CDTitle.size();check>=1;check--){
                    System.out.print(((MusicCD)CDTitle.retrieve(check)).GetTitle()+"\n");
                }
                System.out.print("What would you like to do next: ");
                Command=scan.next();
            } //closes "5" loop
            else if(Command.equals("6")){
                
            }//closes "6" loop
            else if(Command.equals("7")){
                System.out.print("You have "+CDArtist.size()+" cds.\n");
                System.out.print("What would you like to do next: ");
                Command=scan.next();
            } //closes "7" loop
            else{
                System.out.print("The Command you have  entered is not valid.\n" +
                        "Please enter a valid command, Type \"help\" to see command list again: ");
                Command=scan.next();    
            }
        }//closes While
        
    }//closes Main

}//closes Class
class MusicCD implements MyComparable{
        String CdTitle;
        String CdArtist;
        String CdGenre;
        String CdRelease;
            
        public MusicCD(String Title, String Artist, String Genre, String Release){
            this.CdTitle=Title;
            this.CdArtist=Artist;
            this.CdGenre=Genre;
            this.CdRelease=Release;
        }
         public int compareTo1(MyComparable music)
         {
             String firstArtist = ((MusicCD)music).GetArtist();
             String thisArtist = this.GetArtist(); 
             return firstArtist.compareTo(thisArtist); 
         }
         
         public int compareTo2(MyComparable music){
             String firstTitle = ((MusicCD)music).GetTitle();
             String thisTitle = this.GetTitle();
             return firstTitle.compareTo(thisTitle);
         }
         

        public String GetTitle(){return CdTitle;}
        public String GetArtist(){return CdArtist;}
        public String GetGenre(){return CdGenre;}
        public String GetRelease(){return CdRelease;}
        
        

        
    }
public class ListReferenceBased implements ListInterface{
    private Node head;
    private int numOfItems;
    
    public ListReferenceBased(){
        numOfItems=0;
        head=null;
    }
    
    public boolean isEmpty(){
        return numOfItems==0;
    }
    
    public int size(){
        return numOfItems;
    }
    
    private Node Find(int index){
        Node curr=head;
        for(int x=1; x<index; x++){
            curr=curr.getNext();
        }
        return curr;
    }
    
    public Object retrieve(int index)throws ListException{
        if(index>=1 && index<=numOfItems){
            Node curr=Find(index);
            Object Data=curr.getItem();
            return Data;
        }
        else{
            throw new ListException("List Index out of bounds on Retrieve");
        }
    }
    
    public void add(int index, Object item) throws ListException{
        if(index>=1 && index<=numOfItems+1){
            if(index==1){
                Node newNode= new Node(item, head);
                head=newNode;
            }
            else{
                Node prev=Find(index-1);
                Node newNode=new Node(item, prev.getNext());
                prev.setNext(newNode);
            }
            numOfItems++;
        }
        else{
            throw new ListException("List indext out of bounds for ADD");
        }
    }
    
    public void remove(int index) throws ListException{
        if(index>=1&& index<=numOfItems){
            if(index==1){
                head=head.getNext();
            }
            else{
                Node prev=Find(index-1);
                Node curr=prev.getNext();
            }
            numOfItems--;
        }
        else{
            throw new ListException("List index out of bounds on remove");
        }
    }
    
    public void removeAll(){
        head=null;
        numOfItems=0;
    }
}

thats some of the clases i have.
thanks in advance

Perhaps it may be too late into your assignment to consider this, but do you really need to keep two separate lists that only vary by sort order. As you are finding with your current task, having multiple copies of the data means that all operations on that data must be kept in sync. If you later want to be able to sort by genre, you are forced to add another list and more methods to support that list. You can see how such a design becomes more difficult to deal with as it grows.

I think you would be better off keeping a single list and providing methods to resort it as needed.

Is there a requirement that you implement your own linked list and not use a built in collection such as ArrayList? Collections.sort() provides an easy mechanism for sorting based upon any Comparator that you wish to use. Also the collection classes that are part of the Java Collections Framework are already fully featured and tested. It looks like your current remove(int index) still has a bug where it does not update the "next" reference after deletion here

Node curr=prev.getNext();

I know that is a lot of potential change to throw at you after you have gotten this far, but you would have an easier time managing a single ArrayList collection in the long run.

there is a requirement that we have to implement our own linklist we cannot use the java classes. i know that would be easier and i considered it.

i tried to go the single method but i got lost/a headache when i tried to have functions to resort it as neded so i decided jsut to have to. the main idea behind this is to see how the datastructures work.

there is also more clases/interface/exceptions that i have made but didnt think woud be necessary let me know if you need them

if you get a headache trying to think up a class to combine the two fields and find a way to sort on that, you're bound to fail your course so you'd better give up now, why put in the effort knowing you're going to fail anyway?

i didnt give up cause i didnt want to do it i gave up cause thats not what she wants you to get out of the course

Well, if you are sticking to the current list implementation, you just need a search method that locates the item. Then call remove(item) from the list. You do not need separate methods to removeArtist() and removeTitle() - just a search to find it so you can call remove. All you need to do to find the item is loop the linked list until you have a field match. If the user can supply either a title or artist and you have no way to know which they supplied then your search method will check against the values of both fields and return any item that matches.

If you are still confused by that, just let me know which part isn't making sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.