001    package org.cocome.tradingsystem.systests.util;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    import java.util.Random;
006    
007    import org.cocome.tradingsystem.systests.interfaces.IProduct;
008    import org.cocome.tradingsystem.systests.interfaces.IStorePC;
009    
010    /**
011     * This class is used to generate (reproducably) random stock items.
012     * 
013     * @author Benjamin Hummel
014     * @author Christian Pfaller
015     * @author $Author: pfaller $
016     * @version $Rev: 60 $
017     * @levd.rating GREEN Rev: 60
018     */
019    public class StockGenerator {
020    
021            /** The random number generator used. */
022            private final Random rng;
023    
024            /** The storePC used for adding stock. */
025            private final IStorePC storePC;
026    
027            /** The product generator containing all products we may refer to. */
028            private final ProductGenerator productGenerator;
029    
030            /** A list of all stock items generated so far. */
031            private final List<GeneratedStockItem> generatedStock = new ArrayList<GeneratedStockItem>();
032    
033            /**
034             * Construct a new stock generator.
035             * 
036             * @param randomSeed
037             *            the random seed used for initializing the random number
038             *            generator.
039             * @param storePC
040             *            the store used for generating stock.
041             * @param productGenerator
042             *            the product generator containing all products we may refer to.
043             */
044            public StockGenerator(long randomSeed, IStorePC storePC,
045                            ProductGenerator productGenerator) {
046                    rng = new Random(randomSeed);
047                    this.storePC = storePC;
048                    this.productGenerator = productGenerator;
049            }
050    
051            /** Returns the number of stock items generated so far. */
052            public int getNumberOfStockItems() {
053                    return generatedStock.size();
054            }
055    
056            /**
057             * Returns the stock item generated when <code>i</code> stock items had
058             * been generated already.
059             */
060            public GeneratedStockItem getGeneratedStockItem(int i) {
061                    return generatedStock.get(i);
062            }
063    
064            /** Generate stock items for all known products. */
065            public void generateAll() throws Exception {
066                    for (int i = generatedStock.size(); i < productGenerator
067                                    .getNumberOfProducts(); ++i) {
068                            generate(i);
069                    }
070            }
071            
072    
073            /**
074             * Generate a single random stock item from the product of index
075             * <code>i</code> in the product generator.
076             */
077            private void generate(int productIndex) throws Exception {
078    
079                    int minAmount = 1 + rng.nextInt(50);
080                    int amount = minAmount + rng.nextInt(1000);
081                    int maxAmount = amount + rng.nextInt(1000);
082    
083                    generate(productIndex, minAmount, amount, maxAmount);
084            }
085            
086            /**
087             * Generate a single random stock item from the product of index
088             * <code>i</code> in the product generator with the given amounts in stock.
089             */
090            public void generate(int productIndex, int minAmount, int amount, int maxAmount) throws Exception {
091                    GeneratedProduct gp = productGenerator
092                                    .getGeneratedProduct(productIndex);
093    
094                    IProduct product = gp.getProduct();
095    
096                    // prices may at most triple
097                    int salesPrice = (int) (gp.getPurchasePrice() * (1. + 2. * rng
098                                    .nextDouble()));
099    
100    
101                    storePC.insertStockItem(product, salesPrice, amount, minAmount);
102                    generatedStock.add(new GeneratedStockItem(gp, salesPrice, amount,
103                                    minAmount, maxAmount));
104            }
105    }