Modify assignment 5 so that instead of reading the data into an array, it is read into a linked list. Add each new check or deposit to the end of the list. You may follow the example below or create a separate class to handle the list. Use the same data, applet and TextArea as before. You should not have to change very much in your program for assignment 5.
/* 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
/* This method adds an order to the
end of the linked list. */
private void addToEnd (Order newOrder)
{
if (listHead
== null)
{
listHead = newOrder;
listRear = newOrder;
}
else
{
listRear.setNext (newOrder);
listRear = newOrder;
}
} // method addToEnd
/* 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