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.util; 018 019 import java.util.Collection; 020 import java.util.Iterator; 021 import java.math.BigDecimal; 022 import org.apache.geronimo.samples.daytrader.*; 023 024 public class FinancialUtils { 025 //TODO -- FinancialUtils should have parts reimplemented as JSPTaglibs 026 027 public final static int ROUND = BigDecimal.ROUND_HALF_UP; 028 public final static int SCALE = 2; 029 public final static BigDecimal ZERO = (new BigDecimal(0.00)).setScale(SCALE); 030 public final static BigDecimal ONE = (new BigDecimal(1.00)).setScale(SCALE); 031 public final static BigDecimal HUNDRED = (new BigDecimal(100.00)).setScale(SCALE); 032 033 public static BigDecimal computeGain(BigDecimal currentBalance, 034 BigDecimal openBalance) 035 { 036 return currentBalance.subtract(openBalance).setScale(SCALE); 037 } 038 039 public static BigDecimal computeGainPercent(BigDecimal currentBalance, 040 BigDecimal openBalance) 041 { 042 if (openBalance.doubleValue() == 0.0) return ZERO; 043 BigDecimal gainPercent = 044 currentBalance.divide(openBalance, ROUND).subtract(ONE).multiply(HUNDRED); 045 return gainPercent; 046 } 047 048 public static BigDecimal computeHoldingsTotal(Collection holdingDataBeans) { 049 BigDecimal holdingsTotal = new BigDecimal(0.0).setScale(SCALE); 050 if (holdingDataBeans == null) 051 return holdingsTotal; 052 Iterator it = holdingDataBeans.iterator(); 053 while (it.hasNext()) { 054 HoldingDataBean holdingData = (HoldingDataBean) it.next(); 055 BigDecimal total = 056 holdingData.getPurchasePrice().multiply(new BigDecimal(holdingData.getQuantity())); 057 holdingsTotal = holdingsTotal.add(total); 058 } 059 return holdingsTotal.setScale(SCALE); 060 } 061 062 public static String printGainHTML(BigDecimal gain) { 063 String htmlString, arrow; 064 if (gain.doubleValue() < 0.0) { 065 htmlString = "<FONT color=\"#ff0000\">"; 066 arrow = "arrowdown.gif"; 067 } else { 068 htmlString = "<FONT color=\"#009900\">"; 069 arrow = "arrowup.gif"; 070 } 071 072 htmlString += gain.setScale(SCALE, ROUND) + "</FONT><IMG src=\"images/" + arrow + "\" width=\"10\" height=\"10\" border=\"0\"></IMG>"; 073 return htmlString; 074 } 075 076 public static String printChangeHTML(double change) { 077 String htmlString, arrow; 078 if (change < 0.0) { 079 htmlString = "<FONT color=\"#ff0000\">"; 080 arrow = "arrowdown.gif"; 081 } else { 082 htmlString = "<FONT color=\"#009900\">"; 083 arrow = "arrowup.gif"; 084 } 085 086 087 htmlString += change + "</FONT><IMG src=\"images/" + arrow + "\" width=\"10\" height=\"10\" border=\"0\"></IMG>"; 088 return htmlString; 089 } 090 091 public static String printGainPercentHTML(BigDecimal gain) { 092 String htmlString, arrow; 093 if (gain.doubleValue() < 0.0) { 094 htmlString = "(<B><FONT color=\"#ff0000\">"; 095 arrow = "arrowdown.gif"; 096 } else { 097 htmlString = "(<B><FONT color=\"#009900\">+"; 098 arrow = "arrowup.gif"; 099 } 100 101 htmlString += gain.setScale(SCALE, ROUND); 102 htmlString += "%</FONT></B>)<IMG src=\"images/" + arrow + "\" width=\"10\" height=\"10\" border=\"0\"></IMG>"; 103 return htmlString; 104 } 105 106 public static String printQuoteLink(String symbol) 107 { 108 String htmlString; 109 return "<A href=\"app?action=quotes&symbols="+ symbol+"\">" + symbol + "</A>"; 110 } 111 112 113 }