1 32 33 package com.knowgate.math; 34 35 import java.math.BigInteger ; 36 import java.math.BigDecimal ; 37 38 import java.text.DecimalFormat ; 39 import java.text.FieldPosition ; 40 import java.text.NumberFormat ; 41 42 import java.util.Currency ; 43 44 import com.knowgate.misc.Gadgets; 45 import java.util.Locale ; 46 47 import com.knowgate.misc.Gadgets; 48 49 55 public class Money extends BigDecimal { 56 57 private static final DecimalFormat FMT2 = new DecimalFormat ("#0.00"); 58 private static final FieldPosition FRAC = new FieldPosition (NumberFormat.FRACTION_FIELD); 59 60 private CurrencyCode oCurrCode; 61 62 64 private Money(String sVal) throws UnsupportedOperationException { 65 super(sVal); 66 throw new UnsupportedOperationException ("Money(String) is not an allowed constructor"); 67 } 68 69 71 public Money(Money oVal) { 72 super(((BigDecimal ) oVal).toString()); 73 oCurrCode = oVal.oCurrCode; 74 } 75 76 78 84 public Money(String sVal, CurrencyCode oCur) throws NumberFormatException { 85 super(sVal); 86 oCurrCode = oCur; 87 } 88 89 91 98 public Money(String sVal, String sCur) 99 throws NumberFormatException , IllegalArgumentException { 100 super(sVal); 101 oCurrCode = CurrencyCode.currencyCodeFor(sCur); 102 if (null==oCurrCode) throw new IllegalArgumentException ("Money() "+sCur+" is not a legal currency alphanumeric code"); 103 oCurrCode = CurrencyCode.currencyCodeFor(sCur); 104 } 105 106 108 113 public Money(double dVal, CurrencyCode oCur) { 114 super(dVal); 115 oCurrCode = oCur; 116 } 117 118 120 126 public Money(double dVal, String sCur) throws IllegalArgumentException { 127 super(dVal); 128 oCurrCode = CurrencyCode.currencyCodeFor(sCur); 129 } 130 131 133 public Money(BigDecimal oVal, CurrencyCode oCur) { 134 super(oVal.toString()); 135 oCurrCode = oCur; 136 } 137 138 140 public Money(BigDecimal oVal, String sCur) { 141 super(oVal.toString()); 142 oCurrCode = CurrencyCode.currencyCodeFor(sCur); 143 } 144 145 147 public Money(BigInteger oVal, CurrencyCode oCur) { 148 super(oVal); 149 oCurrCode = oCur; 150 } 151 152 154 public Money(BigInteger oVal, String sCur) { 155 super(oVal); 156 oCurrCode = CurrencyCode.currencyCodeFor(sCur); 157 } 158 159 161 public CurrencyCode currencyCode() { 162 return oCurrCode; 163 } 164 165 167 public static boolean isMoney (String sVal) { 168 if (sVal==null) return false; 169 if (sVal.length()==0) return false; 170 String sAmount = sVal.toUpperCase(); 171 int iDot = sAmount.indexOf('.'); 172 int iCom = sAmount.indexOf(','); 173 if (iDot!=0 && iCom!=0) { 174 if (iDot>iCom) { 175 Gadgets.removeChar(sAmount,','); 176 } else { 177 Gadgets.removeChar(sAmount,'.'); 178 } 179 } sAmount = sAmount.replace(',','.'); 181 sAmount = Gadgets.removeChars(sAmount, "�$����#�& ABCDEFGHIJKLMNOPQRSZUVWXYZ"); 182 boolean bMatch = false; 183 try { 184 bMatch = Gadgets.matches(sAmount, "([0-9]+)|([0-9]+.[0-9]+)"); 185 } catch (org.apache.oro.text.regex.MalformedPatternException neverthrown) {} 186 return bMatch; 187 } 189 191 public static Money parse(String sVal) 192 throws NullPointerException ,IllegalArgumentException ,NumberFormatException { 193 int iDot, iCom; 194 CurrencyCode oCur = null; 195 String sAmount; 196 197 if (null==sVal) throw new NullPointerException ("Money.parse() argument cannot be null"); 198 if (sVal.length()==0) throw new IllegalArgumentException ("Money.parse() argument cannot be an empty string"); 199 200 sAmount = sVal.toUpperCase(); 201 if (sAmount.indexOf("EUR")>=0 || sAmount.indexOf("�")>=0 || sAmount.indexOf("€")>=0) 202 oCur = CurrencyCode.EUR; 203 else if (sAmount.indexOf("USD")>=0 || sAmount.indexOf("$")>=0) 204 oCur = CurrencyCode.USD; 205 else if (sAmount.indexOf("GBP")>=0 || sAmount.indexOf("�")>=0) 206 oCur = CurrencyCode.GBP; 207 else if (sAmount.indexOf("JPY")>=0 || sAmount.indexOf("YEN")>=0 || sAmount.indexOf("�")>=0) 208 oCur = CurrencyCode.JPY; 209 else if (sAmount.indexOf("CNY")>=0 || sAmount.indexOf("YUAN")>=0) 210 oCur = CurrencyCode.CNY; 211 212 iDot = sAmount.indexOf('.'); 213 iCom = sAmount.indexOf(','); 214 215 if (iDot!=0 && iCom!=0) { 216 if (iDot>iCom) { 217 Gadgets.removeChar(sAmount,','); 218 } else { 219 Gadgets.removeChar(sAmount,'.'); 220 } 221 } 223 sAmount = sAmount.replace(',','.'); 224 sAmount = Gadgets.removeChars(sAmount, "�$����#�& ABCDEFGHIJKLMNOPQRSZUVWXYZ"); 225 226 return new Money(sAmount, oCur); 227 } 229 231 235 public Money round2 () { 236 StringBuffer oBuffer = new StringBuffer (); 237 FMT2.format(doubleValue(), oBuffer, FRAC); 238 return new Money (oBuffer.toString(), oCurrCode); 239 } 240 241 243 253 public Money convertTo (CurrencyCode oTarget, BigDecimal oRatio) 254 throws NullPointerException { 255 256 Money oNewVal; 257 258 if (oTarget==null) throw new NullPointerException ("Money.convertTo() target currency cannot be null"); 259 260 if (oCurrCode!=null) { 261 if (oCurrCode.equals(oTarget)) 262 oNewVal = this; 263 else 264 oNewVal = new Money(multiply(oRatio), oTarget); 265 } else { 266 oNewVal = new Money(multiply(oRatio), oTarget); 267 } 268 return oNewVal; 269 } 271 273 279 280 public String format (String sLanguage, String sCountry) { 281 282 Locale oLoc; 283 if (null==sCountry) sCountry = oCurrCode.countryCode(); 284 285 if (null!=sLanguage && null!=sCountry) 286 oLoc = new Locale (sLanguage,sCountry); 287 else if (null!=sLanguage) 288 oLoc = new Locale (sLanguage); 289 else 290 oLoc = Locale.getDefault(); 291 NumberFormat oFmtC = NumberFormat.getCurrencyInstance(oLoc); 292 oFmtC.setCurrency(currencyCode().currency()); 293 return oFmtC.format(doubleValue()); 294 } 296 298 public String toString () { 299 if (oCurrCode==null) 300 return super.toString(); 301 else 302 return super.toString()+" "+oCurrCode.alphaCode(); 303 } 304 305 307 } 308
| Popular Tags
|