001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.daytrader;
018    
019    import java.io.Serializable;
020    import java.math.BigDecimal;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Date;
024    import java.util.Iterator;
025    
026    import org.apache.geronimo.samples.daytrader.util.FinancialUtils;
027    import org.apache.geronimo.samples.daytrader.util.Log;
028    
029    public class MarketSummaryDataBean implements Serializable
030    {
031    
032            private BigDecimal      TSIA;                   /* Trade Stock Index Average */
033            private BigDecimal      openTSIA;               /* Trade Stock Index Average at the open */     
034            private double          volume;                 /* volume of shares traded */
035            private Collection      topGainers;             /* Collection of top gaining stocks */
036            private Collection      topLosers;              /* Collection of top losing stocks */   
037            //FUTURE private Collection     topVolume;              /* Collection of top stocks by volume */                
038            private Date                    summaryDate;   /* Date this summary was taken */
039            
040            //cache the gainPercent once computed for this bean
041            private BigDecimal  gainPercent=null;
042    
043            public MarketSummaryDataBean(){ }
044            public MarketSummaryDataBean(BigDecimal TSIA,
045                                                            BigDecimal  openTSIA,
046                                                            double          volume,
047                                                            Collection      topGainers,
048                                                            Collection      topLosers//, Collection topVolume
049                                                            )
050            {
051                    setTSIA(TSIA);
052                    setOpenTSIA(openTSIA);
053                    setVolume(volume);
054                    setTopGainers(topGainers);
055                    setTopLosers(topLosers);
056                    setSummaryDate(new java.sql.Date(System.currentTimeMillis()));
057                    gainPercent = FinancialUtils.computeGainPercent(getTSIA(), getOpenTSIA());
058                    
059            }
060            
061            public static MarketSummaryDataBean getRandomInstance() {
062                    Collection gain = new ArrayList();
063                    Collection lose = new ArrayList();
064                    
065                    for (int ii = 0; ii < 5; ii++) {
066                            QuoteDataBean quote1 = QuoteDataBean.getRandomInstance();
067                            QuoteDataBean quote2 = QuoteDataBean.getRandomInstance();
068                            
069                            gain.add(quote1);
070                            lose.add(quote2);
071                    }
072                    
073                    return new MarketSummaryDataBean(
074                            TradeConfig.rndBigDecimal(1000000.0f),
075                            TradeConfig.rndBigDecimal(1000000.0f),
076                            TradeConfig.rndQuantity(),
077                            gain,
078                            lose
079                    );
080            }
081    
082            public String toString()
083            {
084                    String ret = "\n\tMarket Summary at: " + getSummaryDate()
085                            + "\n\t\t        TSIA:" + getTSIA()
086                            + "\n\t\t    openTSIA:" + getOpenTSIA()
087                            + "\n\t\t        gain:" + getGainPercent()
088                            + "\n\t\t      volume:" + getVolume()
089                            ;
090    
091                    if ( (getTopGainers()==null) || (getTopLosers()==null) )
092                            return ret;
093                    ret += "\n\t\t   Current Top Gainers:";
094                    Iterator it = getTopGainers().iterator();
095                    while ( it.hasNext() ) 
096                    {
097                            QuoteDataBean quoteData = (QuoteDataBean) it.next();
098                            ret += ( "\n\t\t\t"  + quoteData.toString() );
099                    }
100                    ret += "\n\t\t   Current Top Losers:";
101                    it = getTopLosers().iterator();
102                    while ( it.hasNext() ) 
103                    {
104                            QuoteDataBean quoteData = (QuoteDataBean) it.next();
105                            ret += ( "\n\t\t\t"  + quoteData.toString() );
106                    }
107                    return ret;             
108            }
109            public String toHTML()
110            {
111                    String ret = "<BR>Market Summary at: " + getSummaryDate()
112                            + "<LI>        TSIA:" + getTSIA() + "</LI>"
113                            + "<LI>    openTSIA:" + getOpenTSIA() + "</LI>"
114                            + "<LI>      volume:" + getVolume() + "</LI>"
115                            ;
116                    if ( (getTopGainers()==null) || (getTopLosers()==null) )
117                            return ret;
118                    ret += "<BR> Current Top Gainers:";
119                    Iterator it = getTopGainers().iterator();
120                    while ( it.hasNext() ) 
121                    {
122                            QuoteDataBean quoteData = (QuoteDataBean) it.next();
123                            ret += ( "<LI>"  + quoteData.toString()  + "</LI>" );
124                    }
125                    ret += "<BR>   Current Top Losers:";
126                    it = getTopLosers().iterator();
127                    while ( it.hasNext() ) 
128                    {
129                            QuoteDataBean quoteData = (QuoteDataBean) it.next();
130                            ret += ( "<LI>"  + quoteData.toString()  + "</LI>" );
131                    }
132                    return ret;
133            }
134            public void print()
135            {
136                    Log.log( this.toString() );
137            }       
138            
139            public BigDecimal getGainPercent()
140            {
141                    if ( gainPercent == null )
142                            gainPercent = FinancialUtils.computeGainPercent(getTSIA(), getOpenTSIA());
143                    return gainPercent;
144            }
145    
146    
147            /**
148             * Gets the tSIA
149             * @return Returns a BigDecimal
150             */
151            public BigDecimal getTSIA() {
152                    return TSIA;
153            }
154            /**
155             * Sets the tSIA
156             * @param tSIA The tSIA to set
157             */
158            public void setTSIA(BigDecimal tSIA) {
159                    TSIA = tSIA;
160            }
161    
162            /**
163             * Gets the openTSIA
164             * @return Returns a BigDecimal
165             */
166            public BigDecimal getOpenTSIA() {
167                    return openTSIA;
168            }
169            /**
170             * Sets the openTSIA
171             * @param openTSIA The openTSIA to set
172             */
173            public void setOpenTSIA(BigDecimal openTSIA) {
174                    this.openTSIA = openTSIA;
175            }
176    
177            /**
178             * Gets the volume
179             * @return Returns a BigDecimal
180             */
181            public double getVolume() {
182                    return volume;
183            }
184            /**
185             * Sets the volume
186             * @param volume The volume to set
187             */
188            public void setVolume(double volume) {
189                    this.volume = volume;
190            }
191    
192            /**
193             * Gets the topGainers
194             * @return Returns a Collection
195             */
196            public Collection getTopGainers() {
197                    return topGainers;
198            }
199            /**
200             * Sets the topGainers
201             * @param topGainers The topGainers to set
202             */
203            public void setTopGainers(Collection topGainers) {
204                    this.topGainers = topGainers;
205            }
206    
207            /**
208             * Gets the topLosers
209             * @return Returns a Collection
210             */
211            public Collection getTopLosers() {
212                    return topLosers;
213            }
214            /**
215             * Sets the topLosers
216             * @param topLosers The topLosers to set
217             */
218            public void setTopLosers(Collection topLosers) {
219                    this.topLosers = topLosers;
220            }
221    
222            /**
223             * Gets the summaryDate
224             * @return Returns a Date
225             */
226            public Date getSummaryDate() {
227                    return summaryDate;
228            }
229            /**
230             * Sets the summaryDate
231             * @param summaryDate The summaryDate to set
232             */
233            public void setSummaryDate(Date summaryDate) {
234                    this.summaryDate = summaryDate;
235            }
236    
237    }