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