This program should display the list of checks and deposits in a TextArea on an applet. See the example below to see how to do this. Use the same data as before. Change the main class into an applet and the main method into the init method of the applet. You will not have to change the method in the ListClass that reads the data into the array, but you will have to change the methods that display the data.
Instead of displaying the data using System.out, you should display it in the TextArea. That means that the TextArea should be a parameter to the display methods. These are the displayList, and displayTransaction. (Note there are three displayTransaction methods that have to be changed.) These do not have to be completely rewritten. All you have to do is change the way that the data is displayed. (Replace System.out.println () with area.append (…'\n').)
Write meaningful comments for each class and method and use identifiers when naming objects that appropriately describe what the object is or does.
import java.io.*;
import java.text.*;
import java.awt.*;
import java.applet.Applet;
/* Orders is an applet that displays a list of orders in a TextArea
followed by the total bill for the orders. */
public class Orders extends Applet
{
private Panel panel;
private Label areaLabel;
private TextArea area;
private OrderList list;
/* The init method puts the panel, label and text
area on the screen and then reads and displays the orders. */
public void init ()
{
// The panel,
label and text area are added to the applet first.
panel = new
Panel ();
panel.setBackground
(Color.cyan);
areaLabel =
new Label ("Order");
area = new TextArea
(10, 30);
panel.add (areaLabel);
panel.add (area);
add (panel);
/* The list of orders is
created, the orders read in and displayed, and then the total cost is computed
and displayed. */
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;
// The orders are read in from the file and stored in the array.
public void readOrders ()
{
try
{
BufferedReader
orderFile = new BufferedReader (new InputStreamReader (new FileInputStream
("orderFile.txt")));
String
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. The end of line character is attached to the end of the line
so that the text area will show the following data on a new 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.
It also contains method to read and display an order. */
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;}
// An order is read from the file, orderFile,
and stored in the order object.
public void readOrder (BufferedReader orderFile)
throws IOException
{
id = orderFile.readLine
();
price = Double.parseDouble
(orderFile.readLine ());
quantity = Integer.parseInt
(orderFile.readLine ());
} // method readOrder
/* Each order is displayed in the text area on a
line. At the end of the line, the new line character is attached
so that the next order will appear on the following line. */
public void displayOrder (TextArea area)
{
area.append
(id + " ");
area.append
(productName);
area.append
(" " + price);
area.append
(" " + quantity + '\n');
} // method displayOrder
} // class Order