-3

I am trying to create a program that can sort a provided list of "magazine titles" alphabetically, and refresh the sorted list when a new magazine is inserted.

Here is the specific assignment instructions: Modify the magazine rack program to add new magazines in alphabetical order (you'll have to modify Magazine to make it implement Comparable with a compareTo() method, etc.).

BONUS: Implement a delete method, as well, and demonstrate it in your driver program.

Here is what I have so far for my list:

public class MagazineList_jcm
{
private MagazineNode list;

//----------------------------------------------------------------
//  Sets up an initially empty list of magazines.
//----------------------------------------------------------------
public MagazineList_jcm()
{
    list = null;
}

//  public boolean delete(Magazine mag)
//   {
    //  MagazineNode current = list;
    //   MagazineNode previous = null;
    //look through list of magazines
    //first, check for a null list
    // if so, return false
    //if a current.mag with the same title as mag
    //delete it from the list

    //if no mag found, return false

//  }

//----------------------------------------------------------------
//  Creates a new MagazineNode object and adds it to the end of
//  the linked list.
//----------------------------------------------------------------
public void add(Magazine_jcm mag)
{
    MagazineNode node = new MagazineNode(mag);
    MagazineNode current;
    MagazineNode previous;
    boolean done = false;

    if (list == null)
        list = node;
    else
    {
        current = list;
        while (!done)   
        {
            int comp = current.magazine.compareTo(mag);
            if (comp == 0) 
                //- duplicate
            else if (comp > 0)
                //add before current
            if previous null
            //insert at front of list
            list = node;
             node.next = current;

            //ELSE, INSERT BETWEEN PREVIOUS AND CURRENT

        //  else if (comp <0)
                //add after....
            //if current.next null
            //add right after 
//  else 
        //move to the next node
            previous = current;
            current = current.next;


            current = current.next;
        }
        current.next = node;
    }
}

//----------------------------------------------------------------
//  Returns this list of magazines as a string.
//----------------------------------------------------------------
public String toString()
{
    String result = "";

    MagazineNode current = list;

    while (current != null)
    {
        result += current.magazine + "\n";
        current = current.next;
    }

    return result;
}

//*****************************************************************
//  An inner class that represents a node in the magazine list.
//  The public variables are accessed by the MagazineList class.
//*****************************************************************
private class MagazineNode
{
    public Magazine magazine;
    public MagazineNode next;

    //--------------------------------------------------------------
    //  Sets up the node
    //--------------------------------------------------------------
    public MagazineNode(Magazine mag)
    {
        magazine = mag;
        next = null;
    }
}
}

And here is my "magazine":

public class Magazine_jcm 
{
   private String title;

   //-----------------------------------------------------------------
   //  Sets up the new magazine with its title.
   //-----------------------------------------------------------------
   public Magazine_jcm(String newTitle)
   {    
      title = newTitle;
   }

   //-----------------------------------------------------------------
   //  Returns this magazine as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      return title;
   }


}

and here is my driver program:

public class MagazineRack_jcm
{
   //----------------------------------------------------------------
   //  Creates a MagazineList object, adds several magazines to the
   //  list, then prints it.
   //----------------------------------------------------------------
   public static void main(String[] args)
   {    



  MagazineList_jcm rack = new MagazineList_jcm();

  rack.add(new Magazine_jcm("Time"));
  rack.add(new Magazine_jcm("Woodworking Today"));
  rack.add(new Magazine_jcm("Communications of the ACM"));
  rack.add(new Magazine_jcm("House and Garden"));
  rack.add(new Magazine_jcm("GQ"));

  String newMag = JOptionPane.showInputDialog("Please enter a Magazine to add to the list:");
  rack.add(new Magazine_jcm(newMag));
  System.out.println(rack); 
   }
}
4
  • 2
    Please ask a specific question. Commented Jul 21, 2015 at 16:48
  • You should take a look at this docs.oracle.com/javase/tutorial/collections/interfaces/… Commented Jul 21, 2015 at 16:51
  • I am asking how to accomplish the task of inserting a new magazine object and sort it with the given magazine objects using something like a compareTo() method Commented Jul 21, 2015 at 16:51
  • 1
    I'm not asking you to do my assignment for me, I am asking for some pointers. I have a genuine love of programming and I want to know how to do this more for myself than I do for the assignment. Commented Jul 22, 2015 at 16:04

1 Answer 1

-1

This answer can be seen over in another question.

Just change the function to compare your magazine titles rather than ints

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.