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