import java.io.*;
import java.text.*;
import java.awt.event.*;
import java.awt.*;
// Main class that instantiates the frame.
public class OrderFrame
{
public static void main (String [] args)
{
OrderWindow
frame = new OrderWindow ();
frame.addWindowListener
(new WindowCloser ());
frame.show ();
} // main method
} // class ShowFrames
// class to activate the closing icon on the frame.
class WindowCloser extends WindowAdapter
{
public void windowClosing (WindowEvent e){System.exit
(0);}
} // class WindowCloser
// class that creates a frame with a panel, text area, and three buttons.
It also instantiates the list.
class OrderWindow extends Frame
{
private Panel panel;
private Label areaLabel;
private TextArea area;
private OrderList list;
private Button readFile, display, save;
OrderWindow ()
{
super ("Orders");
setSize (300,
300);
panel = new
Panel ();
panel.setBackground
(Color.cyan);
areaLabel =
new Label ("Order");
area = new TextArea
(10, 30);
panel.add (areaLabel);
panel.add (area);
readFile = new
Button ("Read Order");
readFile.addActionListener
(new ReadListener ());
panel.add (readFile);
display = new
Button ("Display Order");
display.addActionListener
(new DisplayListener ());
panel.add (display);
save = new Button
("Save Order");
save.addActionListener
(new SaveListener ());
panel.add (save);
add (panel);
list = new OrderList
();
} // constructor
// Inner class that listens to the display
button.
class DisplayListener implements ActionListener
{
public void
actionPerformed (ActionEvent e) {list.displayOrders (area);}
} // class DisplayListener
// Inner class that listens to the read button.
class ReadListener implements ActionListener
{
public void
actionPerformed (ActionEvent e) {list.readOrders ();}
} // class ReadListener
// Inner class that listens to the save button.
class SaveListener implements ActionListener
{
public void
actionPerformed (ActionEvent e) {list.saveOrders ();}
} // class SaveListener
} // class OrderFrame
/* class that creates an manages a linked list of orders. Orders
are added to the end of the list. The list can be displayed in a
text area and saved to a second file. */
class OrderList
{
private Order listHead, listRear;
final int maxSize = 10;
private int listSize;
OrderList ()
{
listHead = null;
listRear = null;
} // constructor
// Method that reads orders from a text file
and adds them to the end of a linked list.
public void readOrders ()
{
String title;
try
{
BufferedReader orderFile = new BufferedReader (new InputStreamReader (new
FileInputStream ("orderFile.txt")));
title = orderFile.readLine ();
while ((title != null) && (listSize < maxSize))
{
Order order = new Order (title);
order.readOrder (orderFile);
addToEnd (order);
title = orderFile.readLine ();
}
orderFile.close ();
}catch (IOException
e) {System.out.println ("File Error");}
} // method readOrders
// private method to add orders to the end
of a list.
private void addToEnd (Order newOrder)
{
if (listHead
== null)
{
listHead = newOrder;
listRear = newOrder;
}
else
{
listRear.setNext (newOrder);
listRear = newOrder;
}
} // method addToEnd
// Method that displays the list of orders
in a text area and then displays the total cost.
public void displayOrders (TextArea area)
{
Order tempOrder
= listHead;
while (tempOrder
!= null)
{
tempOrder.displayOrder (area);
tempOrder = tempOrder.getNext ();
}
area.append
("The total cost is " + NumberFormat.getCurrencyInstance ().format (calculateTotal
()) + '\n');
} // method displayOrders
// private method that calculates the total
cost.
private double calculateTotal ()
{
double total
= 0;
Order tempOrder
= listHead;
while (tempOrder
!= null)
{
double price = tempOrder.getPrice ();
int quantity = tempOrder.getQuantity ();
total += price * quantity;
tempOrder = tempOrder.getNext ();
}
return total;
} // method totalCost
// Method that saves the list of orders, followed
by the total cost, to a second file.
public void saveOrders ()
{
try
{
PrintStream outfile = new PrintStream (new FileOutputStream ("orderFile2.txt"));
Order tempOrder = listHead;
while (tempOrder != null)
{
tempOrder.outputOrder (outfile);
tempOrder = tempOrder.getNext ();
}
outfile.println ("The total cost is " + NumberFormat.getCurrencyInstance
().format (calculateTotal ()));
outfile.close ();
} catch (FileNotFoundException
e) {System.out.println ("No File");}
} // method saveOrders
} // class OrderList
// class that defines an order with a next field for attaching an instance
to the end of the list.
class Order
{
private String name, id;
private double price;
private int quantity;
private Order next;
Order (String name)
{
this.name =
name;
next = null;
} // constructor
protected Order getNext () { return next;}
protected void setNext (Order newOrder) {
next = newOrder;}
public double getPrice () {return price;}
public int getQuantity () { return quantity;}
// Method to read a single order from the file.
public void readOrder (BufferedReader orderFile)
throws IOException
{
id = orderFile.readLine
();
price = Double.parseDouble
(orderFile.readLine ());
quantity = Integer.parseInt
(orderFile.readLine ());
} // method readOrder
// Method to display an order in the text
area.
public void displayOrder (TextArea area)
{
area.append
("Product Name: " + name + '\n');
area.append
("Product ID: " + id + '\n');
area.append
("Price: " + price + '\n');
area.append
("Quantity: " + quantity + '\n' + '\n');
} // method displayOrder
// Method to print the order to an output
file.
public void outputOrder (PrintStream outfile)
{
outfile.println
("Product Name: " + name);
outfile.println
("Product ID: " + id);
outfile.println
("Price: " + price);
outfile.println
("Quantity: " + quantity);
outfile.println
();
} // method outputOrder
} // class Order