Computer Science 241
Assignment 5
Due: March 21, 2002

Modify assignment 4 so that it reads data into a linked list rather than an array.  Even if the data is not in order, the linked list should be.  Use one field as an ID field and use that to order the list.  Refer to the Linked List Example on the web page.  You may either use a separate node class and have your book or borrower class extend it, or you may simply add a next field to your class and put the linked list methods into your list class.  You will no longer need to keep track of the list size, although you may.  Make sure that you replace all references to the array by statements that refer to the linked list.

The example below might be helpful.

/* An example of an applet that reads from a file, stores the information in a linked list, and then displays it in a TextArea on the applet. */

import java.io.*;
import java.text.*;
import java.awt.*;
import java.applet.Applet;

/* The applet creates the TextArea and the list.  It then reads in the list and displays it. */
public class OrdersLinked extends Applet
{
     private Panel panel;
     private Label areaLabel;
     private TextArea area;
     private OrderList list;

     public void init ()
     {
          panel = new Panel ();
          panel.setBackground (Color.cyan);
          areaLabel = new Label ("Order");
          area = new TextArea (10, 30);
          panel.add (areaLabel); panel.add (area);
          add (panel);
          list = new OrderList ();
          list.readOrders ();
          list.displayOrders (area);
          list.displayTotalCost (area);
     } // method init
} // class OrderFrame

/* The list class reads the data from the file and adds it to the end of a linked list.  It then displays the data and the total cost of the order. */
class OrderList
{
     private Order listHead = null, listRear = null;

     public void readOrders ()
     {
           try
          {
               BufferedReader orderFile = new BufferedReader (new InputStreamReader (new FileInputStream ("orderFile.txt")));
               String  name = orderFile.readLine ();
               while (name != null)
               {
                    Order order = new Order (name);
                    order.readOrder (orderFile);
                    addToEnd (order);
                    name = orderFile.readLine ();
               }
               orderFile.close ();
          }catch (IOException e) {System.out.println ("File Error");}
     } // method readOrders
 
    // Inserts a new id into the list in order by id.
     public void insertInList (Order newOrder)
     {
          if (listHead == null) listHead = newOrder;
          else
          {
               Order current = listHead, previous = listHead;
               while ((current != null) && (current.getId ().compareTo (newOrder.getId ()) < 0))
               {
                    previous = current;
                    current = current.getNext ();
               }
               if (current == listHead)
               {
                    newOrder.setNext (listHead);
                    listHead = newOrder;
               }
               else
               {
                    previous.setNext (newOrder);
                    newOrder.setNext (current);
               }
          }
     } // method insertInList

     /* A temporary node is used to go through the list and display the orders in the TextArea. */
     public void displayOrders (TextArea area)
     {
          Order tempNode = listHead;
          area.append ("Order List " + '\n' + '\n');
          while (tempNode != null)
          {
               tempNode.displayOrder (area);
               tempNode = tempNode.getNext ();
          }
     } // method displayOrders

      /* The total cost is computed by traveling through the list and adding in the price times the quantity of each order. */
     public void displayTotalCost (TextArea area)
     {
          Order tempNode = listHead;
          double total = 0;
          while (tempNode != null)
          {
               total += tempNode.getPrice () * tempNode.getQuantity ();
               tempNode = tempNode.getNext ();
          }
          area.append ("Total Cost = " + NumberFormat.getCurrencyInstance ().format (total) + '\n');
     } // method totalCost
} // class OrderList

/* The Order class contains both the data for each order as well as a pointer, next, to the next order in the list. */
class Order
{
     private String productName, id;
     private double price;
     private int quantity;
     private Order next = null;

     Order (String name) {productName = name;} // constructor

     protected Order getNext () { return next;}
     protected void setNext (Order newOrder) {next = newOrder;}

     protected double getPrice () {return price;}
     protected int getQuantity () {return quantity;}

     /* readOrder reads the data from the file and stores it in the order class. */
     public void readOrder (BufferedReader orderFile) throws IOException
     {
          id = orderFile.readLine ();
          price = Double.parseDouble (orderFile.readLine ());
          quantity = Integer.parseInt (orderFile.readLine ());
     } // method readOrder

     /* displayOrder appends each field in the class to the TextArea. */
     public void displayOrder (TextArea area)
     {
          area.append ("ID: " + id + '\n');
          area.append ("Name: " + productName + '\n');
          area.append ("Price: " + price + '\n');
          area.append ("Quantity: " + quantity + '\n' + '\n');
     } // method displayOrder
} // class Order