package data.ooimpl;

import java.text.NumberFormat;
import java.text.ParseException;

import java.util.Locale;

import data.*;

/**
  * Pure Java implementation of the {@link Currency} interface.
  *
  * @author Steffen Zschaler
  * @version 2.0 19/08/1999
  * @since v2.0
  */
public class CurrencyImpl extends CatalogImpl implements Currency {

  /**
    * Tool used to format and parse currency values.
    *
    * @serial
    */
  private NumberFormat m_nfFormatter;

  /**
    * Create a new, initially empty CurrencyImpl for the given locale.
    *
    * @param sName the name of the currency to create.
    * @param l the locale that determines how currency values will be formatted.
    */
  public CurrencyImpl (String sName, Locale l) {
    super (sName);

    m_nfFormatter = NumberFormat.getCurrencyInstance (l);
  }

  /**
    * Create a new CurrencyImpl with a default locale of {@link Locale#GERMANY} and fill it.
    *
    * <p>The currency will already contain all denominations that are available in the DEM currency.
    * The list of denominations to be added is taken from {@link #s_asDenominations} and
    * {@link #s_anDenominations}.</p>
    *
    * @param sName the name of the new currency.
    */
  public CurrencyImpl (String sName) {
    this (sName, Locale.GERMANY);
    
    // changed by Thomas Medack 06.05.2001
    add (new CurrencyItemImpl (PFENNIG_STCK_1, m_anDenominations[0]), null);
    add (new CurrencyItemImpl (PFENNIG_STCK_2, m_anDenominations[1]), null);
    add (new CurrencyItemImpl (PFENNIG_STCK_5, m_anDenominations[2]), null);
    add (new CurrencyItemImpl (PFENNIG_STCK_10, m_anDenominations[3]), null);
    add (new CurrencyItemImpl (PFENNIG_STCK_50, m_anDenominations[4]), null);
    add (new CurrencyItemImpl (DM_STCK_1, m_anDenominations[5]), null);
    add (new CurrencyItemImpl (DM_STCK_2, m_anDenominations[6]), null);
    add (new CurrencyItemImpl (DM_STCK_5, m_anDenominations[7]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_5, m_anDenominations[8]), null);
    add (new CurrencyItemImpl (DM_STCK_10, m_anDenominations[9]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_10, m_anDenominations[10]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_20, m_anDenominations[11]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_50, m_anDenominations[12]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_100, m_anDenominations[13]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_200, m_anDenominations[14]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_500, m_anDenominations[15]), null);
    add (new CurrencyItemImpl (DM_SCHEIN_1000, m_anDenominations[16]), null);
  }

  /**
    * Return a String representation of the given NumberValue assuming it is a value given in the smallest unit
    * of this currency.
    *
    * @param nv the value to be rendered.
    *
    * @return the formatted String representation
    *
    * @override Never
    */
  public String toString (NumberValue nv) {
    int j = 1;
    for (int i = 1; i <= m_nfFormatter.getMinimumFractionDigits(); i++) {
      j *= 10;
    }

    return m_nfFormatter.format (nv.getValue().doubleValue() / j);
  }

  /**
    * Try to parse the given String as a currency value in the currency's associated format.
    *
    * @param s the text to be parsed.
    *
    * @return the value that was identified in the text, given in the smallest unit of this currency.
    *
    * @override Never
    *
    * @exception ParseException if the given String could not be interpreted as a currency value.
    */
  public NumberValue parse (String s)
    throws ParseException {
    int j = 1;
    for (int i = 1; i <= m_nfFormatter.getMinimumFractionDigits(); i++) {
      j *= 10;
    }

    // 10/27/2000-sz9: Fixed to use round instead a simple cast to int.
    return new IntegerValue ((int) 
                               java.lang.Math.round (
                                 m_nfFormatter.parse (s).doubleValue() * j));
  }
  
  // Fields added by Thomas Medack 06.05.2001

  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>1-Pfennig-Stueck</p>
    */
  public static String PFENNIG_STCK_1 = "1-Pfennig-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>2-Pfennig-Stueck</p>
    */
  public static String PFENNIG_STCK_2 = "2-Pfennig-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>5-Pfennig-Stueck</p>
    */
  public static String PFENNIG_STCK_5 = "5-Pfennig-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>10-Pfennig-Stueck</p>
    */
  public static String PFENNIG_STCK_10 = "10-Pfennig-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>50-Pfennig-Stueck</p>
    */
  public static String PFENNIG_STCK_50 = "50-Pfennig-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>1-Mark-Stueck</p>
    */
  public static String DM_STCK_1 = "1-DM-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>2-Mark-Stueck</p>
    */
  public static String DM_STCK_2 = "2-DM-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>5-Mark-Stueck</p>
    */
  public static String DM_STCK_5 = "5-DM-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>5-Mark-Schein</p>
    */
  public static String DM_SCHEIN_5 = "5-DM-Schein";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>10-Mark-Stueck</p>
    */
  public static String DM_STCK_10 = "10-DM-Stueck";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>10-Mark-Schein</p>
    */
  public static String DM_SCHEIN_10 = "10-DM-Schein";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>20-Mark-Schein</p>
    */
  public static String DM_SCHEIN_20 = "20-DM-Schein";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>50-Mark-Schein</p>
    */
  public static String DM_SCHEIN_50 = "50-DM-Schein";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>100-Mark-Schein</p>
    */
  public static String DM_SCHEIN_100 = "100-DM-Schein";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>200-Mark-Schein</p>
    */
  public static String DM_SCHEIN_200 = "200-DM-Schein";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>500-Mark-Schein</p>
    */
  public static String DM_SCHEIN_500 = "500-DM-Schein";
  /**
    * Field string represents the mainkey for an added <code>CurrencyItemImpl</code>.
    * <p>1000-Mark-Schein</p>
    */
  public static String DM_SCHEIN_1000 = "1000-DM-Schein";
  
  // changed by Thomas Medack 06.05.2001
  private int[] m_anDenominations = {
    1,
    2,
    5,
    10,
    50,
    100,
    200,
    500,
    500,
    1000,
    1000,
    2000,
    5000,
    10000,
    20000,
    50000,
    100000
  };  
}