Example using a TextArea on an applet to display a list.
import java.io.*;
import java.text.*;
import java.awt.*;
import java.applet.Applet;
// Orders is an applet that displays a list of orders followed by the total cost of the orders.
public class Orders 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 Orders
// OrderList reads the orders into an array and then displays them in a TextArea.
class OrderList
{
final int maxSize = 10;
private Order []list = new Order [maxSize];
private int listSize = 0;
public void readOrders ()
{
String name;
try
{
BufferedReader orderFile = new BufferedReader
(new InputStreamReader (new FileInputStream ("orderFile.txt")));
name = orderFile.readLine ();
while ((name != null) & (listSize < maxSize))
{
Order order = new Order (name);
order.readOrder (orderFile);
list [listSize] = order;
listSize ++;
name = orderFile.readLine ();
}
orderFile.close ();
}catch (IOException e) {System.out.println ("File Error");}
} // method readOrders
// The orders are displayed in a TextArea one to a line.
public void displayOrders (TextArea area)
{
area.append (" ID Name Price Quantity" + '\n');
for (int count = 0; count < listSize; count ++)
list [count].displayOrder (area);
} // method displayOrders
// The total cost is displayed at the end of the list of orders.
public void displayTotalCost (TextArea area)
{
double total = 0;
for (int count = 0; count < listSize; count ++)
total += list[count].getPrice () * list[count].getQuantity ();
area.append ("Total Cost = " + NumberFormat.getCurrencyInstance ().format (total) + '\n');
} // method totalCost
} // class OrderList
// An order consists of its name, id, price and the quantity ordered.
class Order
{
private String productName, id;
private double price;
private int quantity;
Order (String name){productName = name;} // constructor
public double getPrice () {return price;}
public int getQuantity () { return quantity;}
public void readOrder (BufferedReader orderFile) throws IOException
{
id = orderFile.readLine ();
price = Double.parseDouble (orderFile.readLine ());
quantity = Integer.parseInt (orderFile.readLine ());
} // method readOrder
public void displayOrder (TextArea area)
{
area.append (id + " ");
area.append (productName);
area.append (" " + price);
area.append (" " + quantity + '\n');
} // method displayOrder
} // class Order