Recipe 8.6 Program: MediaInvoicer


This sketch of an invoicing program demonstrates several of these concepts. MediaFactory is a class that takes a String (or int) and returns the corresponding Media enum constant:

/*  * MediaFactory - give out Media enumeration constants  * @verion $Id: ch08.xml,v 1.5 2004/05/04 20:11:57 ian Exp $  */ public class MediaFactory {     public static void main(String[] args) {                  System.out.println(MediaFactory.getMedia("Book"));     }     public static Media getMedia(String s) {         return Enum.valueOf(Media.class, s.toLowerCase( ));     }     public static Media getMedia(int n){         return Media.values( )[n];     } }

This program uses the valueOf( ) method inherited from java.lang.Enum by all user-defined enumerations. The MediaFactory is used in the main class's getInvoice( ) method to get the correct enum constant for a given String read from the invoices file, a sample of which looks like this:

# Lines beginning with # are comments, ignored by the program # I invoice# cust# # M    media item# quantity M Book 2074 1 M MUSIC_VINYL 107 1 M MUSIC_CD 5102 5 M book 2100 1

As you can see, the data entry people have been careless about case, but the MediaFactory class turns the strings into lowercase before looking them up. The main program pulls these lines apart with a StringTokenizer and parses the ints with Integer.parseInt( ) .

The main class uses two helper classes, Invoice and Item. An invoice can have one or more line items in it, so the Invoice object contains an array of Item objects.

Example 8-11 is the code for the main MediaInvoicer class and the helper classes.

Example 8-11. MediaInvoicer.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; /**  * MediaInvoicer - Simple applicatin of Media, MediaFactory &c.  *   * @author ian  * @version $Id: ch08.xml,v 1.5 2004/05/04 20:11:57 ian Exp $  */ public class MediaInvoicer {     public static void main(String[] args) throws IOException {         MediaInvoicer mi = new MediaInvoicer(System.in);         Invoice i = mi.getInvoice( );         i.print(System.out);     }     BufferedReader myFile;     public MediaInvoicer(InputStream is) {         myFile = new BufferedReader(new InputStreamReader(is));     }     public Invoice getInvoice( ) throws IOException {         String line;         List < Item > items = new ArrayList < Item > ( );         while ((line = myFile.readLine( )) != null) {             if (line.startsWith("#")) {                 continue;             }             StringTokenizer st = new StringTokenizer(line);             st.nextToken( );             Media m = MediaFactory.getMedia(st.nextToken( ));             int stock = Integer.parseInt(st.nextToken( ));             int qty = Integer.parseInt(st.nextToken( ));             Item tmp = new Item(m, stock, qty);             items.add(tmp);         }         return new Invoice(1, 3,             (Item[]) items.toArray(new Item[items.size( )]));     }     /** Inner class for line order item */     class Item {         Media product;         int stockNumber;         int quantity;         /**          * @param product          * @param stockNumber          * @param quantity          */         public Item(Media product, int stockNumber, int quantity) {             super( );             this.product = product;             this.stockNumber = stockNumber;             this.quantity = quantity;         }         public String toString( ) {             return "Item[" + product + " " + stockNumber + "]";         }     }     /** Inner class for one invoice */     class Invoice {         int orderNumber;         int custNumber;         Item[] items;         public Invoice(int orderNumber, int custNumber, Item[] items) {             super( );             this.orderNumber = orderNumber;             this.custNumber = custNumber;             this.items = items;         }         public void print(PrintStream ps) {             ps.println("*** Invoice ***");             ps.println("Customer: " + custNumber + ")");             ps.println("Our order number: " + orderNumber);             for (int i = 0; i < items.length; i++) {                 Item it = items[i];                 ps.println(it);             }         }     } }

Running the program with the sample file shown in Example 8-11 produces this output:

*** Invoice *** Customer number: 1 Our order number: 3 Item[book 2074] Item[music_vinyl 107] Item[music_cd 5102] Item[book 2100]

This demonstrates a bit of the ease of use of Java enumerations they print as themselves and the use of a List customized to hold Item objects the list named items in the getInvoice( ) method.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net