/* * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) * Published By SunSoft Press/Prentice-Hall * Copyright (C) 1996 Sun Microsystems Inc. * All Rights Reserved. ISBN 0-13-596891-7 * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * @version 1.01 07 Sep 1996 * @author Cay Horstmann */ import java.awt.*; import java.applet.*; import java.util.*; import java.net.*; import corejava.*; public class PriceListTest extends Applet { public void init() { setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new FlowLayout()); name = new Choice(); try { URL url = new URL(getDocumentBase(), "prices.dat"); prices.load(url.openStream()); } catch(Exception e) {} Enumeration e = prices.propertyNames(); while (e.hasMoreElements()) name.addItem(((String)e.nextElement()) .replace('+', ' ')); quantity = new IntTextField(1, 0, 100, 4); p.add(name); p.add(quantity); p.add(new Button("Add")); p.add(new Button("Done")); add("North", p); add("Center", canvas = new PurchaseOrderCanvas()); canvas.resize(250, 150); canvas.redraw(a); } public boolean action(Event evt, Object arg) { if (arg.equals("Add")) { if (quantity.isValid()) { String itemName = name.getSelectedItem(); a.addElement(new Item(itemName, quantity.getValue(), Format.atof((String)prices.get(itemName .replace(' ', '+'))))); } } else if (arg.equals("Done")) { a.addElement(new Item("State Tax", 1, 0.00)); a.addElement(new Item("Shipping", 1, 5.00)); a.trimToSize(); } else return super.action(evt, arg); canvas.redraw(a); return true; } private Vector a = new Vector(); private Choice name; private IntTextField quantity; private PurchaseOrderCanvas canvas; private int m = 1; private Properties prices = new Properties(); } class Item { Item(String n, int q, double u) { name = n; quantity = q; unitPrice = u; } public String toString() { return new Format("%-20s").form(name) + new Format("%6d").form(quantity) + new Format("%8.2f").form(unitPrice); } private String name; private int quantity; private double unitPrice; } class PurchaseOrderCanvas extends Canvas { public void redraw(Vector new_a) { a = new_a; repaint(); } public void paint(Graphics g) { Font f = new Font("Courier", Font.PLAIN, 12); g.setFont(f); FontMetrics fm = g.getFontMetrics(f); int height = fm.getHeight(); int x = 80; int y = 0; int i = 0; y += height; g.drawString("Your Order: ", x, y); for (i = 0; i < a.size(); i++) { y += height; g.drawString(a.elementAt(i).toString(), x, y); } } private Vector a; }