001    package data.ooimpl;
002    
003    import java.text.NumberFormat;
004    import java.text.ParseException;
005    
006    import java.util.Locale;
007    
008    import data.*;
009    
010    /**
011     * Pure Java implementation of the {@link Currency} interface.
012     *
013     * @author Steffen Zschaler
014     * @version 2.0 19/08/1999
015     * @since v2.0
016     */
017    public class CurrencyImpl extends CatalogImpl implements Currency {
018    
019        /**
020         * Tool used to format and parse currency values.
021         *
022         * @serial
023         */
024        private NumberFormat m_nfFormatter;
025    
026        /**
027         * Create a new, initially empty CurrencyImpl for the given locale.
028         *
029         * @param sName the name of the currency to create.
030         * @param l the locale that determines how currency values will be formatted.
031         */
032        public CurrencyImpl(String sName, Locale l) {
033            super(sName);
034    
035            m_nfFormatter = NumberFormat.getCurrencyInstance(l);
036        }
037    
038        /**
039         * Create a new CurrencyImpl with a default locale of {@link Locale#GERMANY} and fill it.
040         *
041         * <p>The currency will already contain all denominations that are available in the DEM currency.</p>
042         *
043         * @param sName the name of the new currency.
044         */
045        public CurrencyImpl(String sName) {
046            this(sName, Locale.GERMANY);
047    
048            // changed by Thomas Medack 06.05.2001
049            add(new CurrencyItemImpl(PFENNIG_STCK_1, m_anDenominations[0]), null);
050            add(new CurrencyItemImpl(PFENNIG_STCK_2, m_anDenominations[1]), null);
051            add(new CurrencyItemImpl(PFENNIG_STCK_5, m_anDenominations[2]), null);
052            add(new CurrencyItemImpl(PFENNIG_STCK_10, m_anDenominations[3]), null);
053            add(new CurrencyItemImpl(PFENNIG_STCK_50, m_anDenominations[4]), null);
054            add(new CurrencyItemImpl(DM_STCK_1, m_anDenominations[5]), null);
055            add(new CurrencyItemImpl(DM_STCK_2, m_anDenominations[6]), null);
056            add(new CurrencyItemImpl(DM_STCK_5, m_anDenominations[7]), null);
057            add(new CurrencyItemImpl(DM_SCHEIN_5, m_anDenominations[8]), null);
058            add(new CurrencyItemImpl(DM_STCK_10, m_anDenominations[9]), null);
059            add(new CurrencyItemImpl(DM_SCHEIN_10, m_anDenominations[10]), null);
060            add(new CurrencyItemImpl(DM_SCHEIN_20, m_anDenominations[11]), null);
061            add(new CurrencyItemImpl(DM_SCHEIN_50, m_anDenominations[12]), null);
062            add(new CurrencyItemImpl(DM_SCHEIN_100, m_anDenominations[13]), null);
063            add(new CurrencyItemImpl(DM_SCHEIN_200, m_anDenominations[14]), null);
064            add(new CurrencyItemImpl(DM_SCHEIN_500, m_anDenominations[15]), null);
065            add(new CurrencyItemImpl(DM_SCHEIN_1000, m_anDenominations[16]), null);
066        }
067    
068        /**
069         * Return a String representation of the given NumberValue assuming it is a value given in the smallest unit
070         * of this currency.
071         *
072         * @param nv the value to be rendered.
073         *
074         * @return the formatted String representation
075         *
076         * @override Never
077         */
078        public String toString(NumberValue nv) {
079            int j = 1;
080            for (int i = 1; i <= m_nfFormatter.getMinimumFractionDigits(); i++) {
081                j *= 10;
082            }
083    
084            return m_nfFormatter.format(nv.getValue().doubleValue() / j);
085        }
086    
087        /**
088         * Try to parse the given String as a currency value in the currency's associated format.
089         *
090         * @param s the text to be parsed.
091         *
092         * @return the value that was identified in the text, given in the smallest unit of this currency.
093         *
094         * @override Never
095         *
096         * @exception ParseException if the given String could not be interpreted as a currency value.
097         */
098        public NumberValue parse(String s) throws ParseException {
099            int j = 1;
100            for (int i = 1; i <= m_nfFormatter.getMinimumFractionDigits(); i++) {
101                j *= 10;
102            }
103    
104            // 10/27/2000-sz9: Fixed to use round instead a simple cast to int.
105            return new IntegerValue((int)java.lang.Math.round(m_nfFormatter.parse(s).doubleValue() * j));
106        }
107    
108        // Fields added by Thomas Medack 06.05.2001
109    
110        /**
111         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
112         * <p>1-Pfennig-Stueck</p>
113         */
114        public static String PFENNIG_STCK_1 = "1-Pfennig-Stueck";
115        /**
116         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
117         * <p>2-Pfennig-Stueck</p>
118         */
119        public static String PFENNIG_STCK_2 = "2-Pfennig-Stueck";
120        /**
121         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
122         * <p>5-Pfennig-Stueck</p>
123         */
124        public static String PFENNIG_STCK_5 = "5-Pfennig-Stueck";
125        /**
126         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
127         * <p>10-Pfennig-Stueck</p>
128         */
129        public static String PFENNIG_STCK_10 = "10-Pfennig-Stueck";
130        /**
131         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
132         * <p>50-Pfennig-Stueck</p>
133         */
134        public static String PFENNIG_STCK_50 = "50-Pfennig-Stueck";
135        /**
136         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
137         * <p>1-Mark-Stueck</p>
138         */
139        public static String DM_STCK_1 = "1-DM-Stueck";
140        /**
141         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
142         * <p>2-Mark-Stueck</p>
143         */
144        public static String DM_STCK_2 = "2-DM-Stueck";
145        /**
146         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
147         * <p>5-Mark-Stueck</p>
148         */
149        public static String DM_STCK_5 = "5-DM-Stueck";
150        /**
151         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
152         * <p>5-Mark-Schein</p>
153         */
154        public static String DM_SCHEIN_5 = "5-DM-Schein";
155        /**
156         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
157         * <p>10-Mark-Stueck</p>
158         */
159        public static String DM_STCK_10 = "10-DM-Stueck";
160        /**
161         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
162         * <p>10-Mark-Schein</p>
163         */
164        public static String DM_SCHEIN_10 = "10-DM-Schein";
165        /**
166         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
167         * <p>20-Mark-Schein</p>
168         */
169        public static String DM_SCHEIN_20 = "20-DM-Schein";
170        /**
171         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
172         * <p>50-Mark-Schein</p>
173         */
174        public static String DM_SCHEIN_50 = "50-DM-Schein";
175        /**
176         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
177         * <p>100-Mark-Schein</p>
178         */
179        public static String DM_SCHEIN_100 = "100-DM-Schein";
180        /**
181         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
182         * <p>200-Mark-Schein</p>
183         */
184        public static String DM_SCHEIN_200 = "200-DM-Schein";
185        /**
186         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
187         * <p>500-Mark-Schein</p>
188         */
189        public static String DM_SCHEIN_500 = "500-DM-Schein";
190        /**
191         * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
192         * <p>1000-Mark-Schein</p>
193         */
194        public static String DM_SCHEIN_1000 = "1000-DM-Schein";
195    
196        // changed by Thomas Medack 06.05.2001
197        private int[] m_anDenominations = {
198                1, 2, 5, 10, 50, 100, 200, 500, 500, 1000, 1000, 2000, 5000, 10000, 20000, 50000, 100000
199        };
200    }