www.pudn.com > StockManager.rar > RMSStockStore.java


 
import javax.microedition.rms.*; 
import java.io.DataOutputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.ByteArrayInputStream; 
import java.io.DataInputStream; 
import java.io.EOFException; 
import java.util.*; 
 
/** 
 * A class used for storing and showing stock values. 
 */ 
 public class RMSStockStore 
    implements RecordFilter, RecordComparator, StockDatabase 
{ 
    /* 
     * The RecordStore used for storing the stock values. 
     */ 
    private RecordStore recordStore = null; 
 
    /* 
     * The symbol name to use when filtering. 
     */ 
    public static String symbolFilter = null; 
 
    /* 
     * Part of the RecordFilter interface. 
     */ 
    public boolean matches(byte[] candidate) 
    throws IllegalArgumentException 
    { 
        // If no filter set, nothing can match it. 
        if (this.symbolFilter == null) { 
            return false; 
        } 
 
        ByteArrayInputStream bais = new ByteArrayInputStream(candidate); 
        DataInputStream inputStream = new DataInputStream(bais); 
        String name = null; 
 
        try { 
 
            Stock stock = StockStorage.readStock(inputStream); 
            int shares = stock.getNumShares(); 
            name = stock.getSymbol(); 
        } 
        catch (EOFException eofe) { 
            System.out.println(eofe); 
            eofe.printStackTrace(); 
        } 
        catch (IOException eofe) { 
            System.out.println(eofe); 
            eofe.printStackTrace(); 
        } 
        return (this.symbolFilter.equals(name)); 
    } 
 
    /* 
     * Part of the RecordComparator interface. 
     */ 
    public int compare(byte[] rec1, byte[] rec2) 
    { 
    // Construct DataInputStreams for extracting the stocks from 
    // the records. 
    ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1); 
    DataInputStream inputStream1 = new DataInputStream(bais1); 
    ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2); 
    DataInputStream inputStream2 = new DataInputStream(bais2); 
 
    int compareValue = 0; 
 
    try { 
        // Extract the stocks. 
        Stock stock1 = StockStorage.readStock(inputStream1); 
        Stock stock2 = StockStorage.readStock(inputStream2); 
 
        String symbol1 = stock1.getSymbol(); 
        String symbol2 = stock2.getSymbol(); 
 
        compareValue = symbol1.compareTo(symbol2); 
    } 
    catch (EOFException eofe) { 
        System.out.println(eofe); 
        eofe.printStackTrace(); 
    } 
    catch (IOException eofe) { 
        System.out.println(eofe); 
        eofe.printStackTrace(); 
    } 
 
    // Sort by symbol name 
    if (compareValue < 0) { 
        return RecordComparator.PRECEDES; 
    } 
    else if (compareValue > 0) { 
        return RecordComparator.FOLLOWS; 
    } 
    else { 
        return RecordComparator.EQUIVALENT; 
    } 
    } 
 
    /** 
     * The constructor opens the underlying record store, 
     * creating it if necessary. 
     */ 
    public RMSStockStore() 
    { 
    // 
    // Create a new record store for this example 
    // 
    try { 
        String fileName = "shares"; 
        recordStore = RecordStore.openRecordStore(fileName, true); 
    } 
    catch (RecordStoreException rse) { 
        System.out.println(rse); 
        rse.printStackTrace(); 
    } 
    } 
 
    /** 
     * Add a new stock to the storage. 
     * 
     * @param shares the number of shares to store. 
     * @param symbol the name of the stock 
     * @param price the price of the stock 
     */ 
    private void addStock(int shares, String symbolName, int price) 
    { 
        // 
        // Each share is stored in a separate record, formatted as defined in the StockStorage class 
        // 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        DataOutputStream outputStream = new DataOutputStream(baos); 
        try { 
            Stock outputStock = new Stock( symbolName, shares, price); 
 
            StockStorage.writeStock( outputStock, outputStream); 
 
        } 
        catch (IOException ioe) { 
            System.out.println(ioe); 
            ioe.printStackTrace(); 
        } 
 
        // Extract the byte array 
        byte[] b = baos.toByteArray(); 
 
        // Add it to the record store 
        try { 
            recordStore.addRecord(b, 0, b.length); 
        } 
        catch (RecordStoreException rse) { 
            System.out.println(rse); 
            rse.printStackTrace(); 
        } 
    } 
 
    /** 
     * A helper method for the printStocks methods. 
     */ 
    private Vector getStockHelper(RecordEnumeration re) 
    { 
        Vector stockVector = new Vector(); 
 
        try { 
            while(re.hasNextElement()) 
            { 
                int id = re.nextRecordId(); 
                ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id)); 
                DataInputStream inputStream = new DataInputStream(bais); 
                try { 
                    Stock stock = StockStorage.readStock(inputStream); 
 
                    stockVector.addElement( stock); 
                } 
                catch (EOFException eofe) { 
                    System.out.println(eofe); 
                    eofe.printStackTrace(); 
                } 
            } 
        } 
        catch (RecordStoreException rse) { 
            System.out.println(rse); 
            rse.printStackTrace(); 
        } 
        catch (IOException ioe) { 
            System.out.println(ioe); 
            ioe.printStackTrace(); 
        } 
 
        return stockVector; 
    } 
 
 
    /** 
     * This method prints all of the symbol for a given symbol, 
     * sorted by game symbol. 
     */ 
    public Stock getStock(String symbol) 
    { 
        this.symbolFilter = symbol; 
        Stock stock = null; 
 
        try { 
            // Enumerate the records using the comparator and filter 
            // implemented above to sort by symbol. 
            RecordEnumeration re = recordStore.enumerateRecords(this, this, 
                                    true); 
            Vector v = getStockHelper(re); 
 
            if ( v.size() > 0) 
            { 
                stock = (Stock) v.elementAt(0); 
            } 
        } 
        catch (RecordStoreException rse) { 
            System.out.println(rse); 
            rse.printStackTrace(); 
        } 
 
        return stock; 
    } 
 
    public void buyStock(String symbol, String shares, int price) throws StockException 
    { 
        int numShares = 0; 
 
        try 
        { 
            numShares = Integer.parseInt( shares); 
        } 
        catch( NumberFormatException nfe) 
        { 
            throw new StockException("Number of shares value is invalid", nfe); 
        } 
 
        if ( numShares <= 0 || price <= 0 || symbol.length() == 0) 
        { 
            throw new StockException("Invalid Data passed to RMS", null); 
        } 
 
        // check if there are existing shares 
        Stock stock = getStock( symbol); 
 
        if ( stock == null) 
        { 
            // add the new shares 
            addStock( numShares, symbol, price); 
        } 
        else 
        { 
            sellStock( stock, stock.getNumShares() ); 
 
            addStock( numShares + stock.getNumShares(), symbol, price); 
        } 
    } 
 
 
    public void sellStock(Stock stock, int numShares) throws StockException 
    { 
        // delete entry from the database 
        this.symbolFilter = stock.getSymbol(); 
 
        Stock retrievedStock = null; 
 
        try { 
            RecordEnumeration re = recordStore.enumerateRecords(this, this, true); 
 
            while(re.hasNextElement()) 
            { 
                int id = re.nextRecordId(); 
                ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id)); 
                DataInputStream inputStream = new DataInputStream(bais); 
                try { 
                    retrievedStock = StockStorage.readStock(inputStream); 
 
                    int diff = retrievedStock.getNumShares() - stock.getNumShares(); 
 
                    if ( diff > 0) 
                    { 
                        addStock( diff, stock.getSymbol(), retrievedStock.getPrice() ); 
                    } 
 
                    recordStore.deleteRecord( id); 
                } 
                catch (EOFException eofe) { 
                    System.out.println(eofe); 
                    eofe.printStackTrace(); 
                } 
            } 
        } 
        catch (RecordStoreException rse) { 
            System.out.println(rse); 
            rse.printStackTrace(); 
        } 
        catch (IOException ioe) { 
            System.out.println(ioe); 
            ioe.printStackTrace(); 
        } 
 
    } 
 
    public Vector getStocks() throws StockException 
    { 
        Vector stockVector = new Vector(); 
 
        try { 
            // Enumerate the records using the comparator implemented 
            // above to sort by game symbol. 
            RecordEnumeration re = recordStore.enumerateRecords(null, this, 
                                    true); 
            stockVector = getStockHelper(re); 
        } 
        catch (RecordStoreException rse) { 
            System.out.println(rse); 
            rse.printStackTrace(); 
        } 
 
        if ( stockVector.size() == 0) 
            throw new StockException("No Stocks found in portfolio.", null); 
 
        return stockVector; 
    } 
}