001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2002 Johann Gyger <johann.gyger@switzerland.org>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.stocks;
024:
025: import java.text.NumberFormat;
026: import java.text.ParseException;
027:
028: import net.sf.jmoney.model2.Commodity;
029: import net.sf.jmoney.model2.IObjectKey;
030: import net.sf.jmoney.model2.IValues;
031: import net.sf.jmoney.model2.ListKey;
032:
033: public class Stock extends Commodity {
034:
035: private static NumberFormat numberFormat = NumberFormat
036: .getNumberInstance();
037: static {
038: numberFormat.setMaximumFractionDigits(0);
039: numberFormat.setMinimumFractionDigits(0);
040: }
041:
042: private String nominalValue;
043:
044: /**
045: * Constructor used by datastore plug-ins to create
046: * a stock object.
047: */
048: public Stock(IObjectKey objectKey, ListKey parentKey, String name,
049: String nominalValue, IValues extensionValues) {
050: super (objectKey, parentKey, name, extensionValues);
051:
052: this .nominalValue = nominalValue;
053: }
054:
055: /**
056: * Constructor used by datastore plug-ins to create
057: * a stock object.
058: */
059: public Stock(IObjectKey objectKey, ListKey parentKey) {
060: super (objectKey, parentKey);
061:
062: this .nominalValue = null;
063: }
064:
065: @Override
066: protected String getExtendablePropertySetId() {
067: return "net.sf.jmoney.stocks.stock";
068: }
069:
070: /**
071: * @return the nominal value. For example, "ORD 25P"
072: */
073: public String getNominalValue() {
074: return nominalValue;
075: }
076:
077: public void setNominalValue(String nominalValue) {
078: String oldNominalValue = this .nominalValue;
079: this .nominalValue = nominalValue;
080:
081: // Notify the change manager.
082: processPropertyChange(StockInfo.getNominalValueAccessor(),
083: oldNominalValue, nominalValue);
084: }
085:
086: @Override
087: public long parse(String amountString) {
088: Number amount;
089: try {
090: amount = numberFormat.parse(amountString);
091: } catch (ParseException pex) {
092: amount = new Double(0);
093: }
094: return Math.round(amount.doubleValue());
095: }
096:
097: @Override
098: public String format(long amount) {
099: double a = amount;
100: return numberFormat.format(a);
101: }
102:
103: /**
104: * @return The scale factor. Always 1 for stock.
105: */
106: // TODO: This property should be for currency only.
107: @Override
108: public short getScaleFactor() {
109: return 1;
110: }
111: }
|