//********************************************************************
// Magazine.java Author: Lewis/Loftus
//
// Represents a single magazine.
//********************************************************************
public class Magazine
{
private String title;
//-----------------------------------------------------------------
// Sets up the new magazine with its title.
//-----------------------------------------------------------------
public Magazine (String newTitle)
{
title = newTitle;
}
//-----------------------------------------------------------------
// Returns this magazine as a string.
//-----------------------------------------------------------------
public String toString ()
{
return title;
}
}
//*******************************************************************
// MagazineList.java Author: Lewis/Loftus
//
// Represents a collection of magazines.
//*******************************************************************
public class MagazineList
{
private MagazineNode list;
//----------------------------------------------------------------
// Sets up an initially empty list of magazines.
//----------------------------------------------------------------
public MagazineList()
{
list = null;
}
//----------------------------------------------------------------
// Creates a new MagazineNode object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
public void add (Magazine mag)
{
MagazineNode node = new MagazineNode (mag);
MagazineNode current;
if (list == null)
list = node;
else
{
current = list;
while (current.next != null)
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;
}
}
}
//*******************************************************************
// MagazineRack.java Author: Lewis/Loftus
//
// Driver to exercise the MagazineList collection.
//*******************************************************************
public class MagazineRack
{
//----------------------------------------------------------------
// Creates a MagazineList object, adds several magazines to the
// list, then prints it.
//----------------------------------------------------------------
public static void main (String[] args)
{
MagazineList rack = new MagazineList();
rack.add (new Magazine("Time"));
rack.add (new Magazine("Woodworking Today"));
rack.add (new Magazine("Communications of the ACM"));
rack.add (new Magazine("House and Garden"));
rack.add (new Magazine("GQ"));
System.out.println (rack);
}
}
Question: Modify the magazine rack program (from the lecture notes) by adding delete and insert operations into the magazine rack class. The insert method should be based on a compareTo() method in the Magazine class that determines if one magazine title comes before another alphabetically. In the main method, exercise various insertion and deletion operations. Print the magazine rack when complete. Use the code below for your main method:
//*******************************************************************
// MagazineRack.java Author: Lewis/Loftus
//
// Driver to exercise the MagazineList collection.
//*******************************************************************
public class MagazineRack
{
//----------------------------------------------------------------
// Creates a MagazineList object, adds several magazines to the
// list, then prints it.
//----------------------------------------------------------------
public static void main (String[] args)
{
MagazineList rack = new MagazineList();
rack.insert (new Magazine("F Title") );
rack.insert (new Magazine("D Title"));
rack.insert (new Magazine("G Title"));
rack.insert (new Magazine("A Title"));
rack.insert (new Magazine("E Title"));
rack.insert (new Magazine("H Title"));
System.out.println( "After inserts:\n" + rack );
rack.delete (new Magazine("A Title"));
rack.delete (new Magazine("H Title"));
rack.delete (new Magazine("G Title"));
rack.delete (new Magazine("E Title"));
System.out.println( "After deletes:\n" + rack );
rack.insert (new Magazine("A Title"));
rack.insert (new Magazine("E Title"));
rack.insert (new Magazine("H Title"));
rack.insert (new Magazine("G Title"));
System.out.println( "After 2nd inserts:\n" + rack );
}
}