/**
 *
 * Class MultiConverter does conversions of 
 * currencies in British pounds, Canadian dollars,  
 * Euros and Australian dollars from US dollars
 *
 * @author  Daniel Burgner
 * @version 1.0
 * @class   CS5730
 * @date    02.15.2002
 */

import java.awt.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.applet.Applet;

public abstract class MultiConverter extends Currency {
	
	/* initMultiConverter is an abstract function - to be used
		by the CurrencyConverter class */
	abstract public void initMultiConverter();

	/* nNumCurrencies holds the number of currencies */
	protected int nNumCurrencies = 0;

	/* cConversions holds an array of Conversions */
	protected Conversion cConversions[] = new Conversion[MAX_CURRENCIES];

	/* strCurrFullName holds the value of currency's full name */
	protected String strCurrFullName;

	/* d holds the dimensions of the applet */
	protected Dimension d;

	/* dfFormat is the format of the output */
	protected DecimalFormat dfFormat = new DecimalFormat("#,##0.00");

	/**
	 *
	 * init calls Currency's init function, gets the dimensions and
	 * calls the initMultiConverter function
	 * @param  none
	 * @return void
	 *
	 */ 
	public void init() {
		super.init();
		d = getSize();
		initMultiConverter();
	}

	/**
	 *
	 * addConversion adds the conversions to cConversions
	 * @param  c - a Conversion to be added to cConversions
	 * @return void
	 *
	 */
	final public void addConversion(Conversion c) {

		/* if nNumCurrencies < MAX_CURRENCIES and c != null,
			add the conversion and increment. */
		if (nNumCurrencies < MAX_CURRENCIES && c != null) {
			cConversions[nNumCurrencies++] = c;
		}
	}

	/**
	 *
	 * paint initializes the applet's background color to white,
	 * colors the entire applet to the background colors and  
	 * calls the ShowConversions and DrawBarChart method.
	 * @param  g - Graphics
	 * @return void
	 *
	 */
	public void paint(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0, 0, d.width, d.height);
		ShowConversions(g);
		DrawBarChart(g);
	}

	/**
	 *
	 * ShowConversions prints the conversions and the amount of
	 * money in original currency to the applet.
	 * @param  g - Graphics
	 * @return void
	 *
	 */
	protected void ShowConversions(Graphics g) {

		String strAmount;		// string value of amount

		/* set the color to white */
		g.setColor(Color.black);

		/* set the font to default font in Currency */
		g.setFont(font);

		/* prints to applet the original currency and amount */
		g.drawString("$" + dfFormat.format(dAmount) + " converts to:", 20, 20);

		/* prints to applet the result and the new currency 
			for 0 to nNumCurrencies */
		for (int nN = 0; nN < nNumCurrencies; nN++) {

			/* if strCurr == certain currency code, set the
				full name to corresponding currency */
			if (strCurr[nN].compareTo("AUD") == 0) {
				strCurrFullName = "Australian Dollars";
			} else if (strCurr[nN].compareTo("CAD") == 0) {
				strCurrFullName = "Canadian Dollars";
			} else if (strCurr[nN].compareTo("EUR") == 0) {
				strCurrFullName = "Euros";
			} else if (strCurr[nN].compareTo("GBP") == 0) {
				strCurrFullName = "British Pounds";
			}
			strAmount = dfFormat.format(cConversions[nN].result(dAmount));
	 		g.drawString(strAmount + " " + strCurrFullName, 20, 40 + 20 * nN);
		}
	}

	/**
	 *
	 * DrawBarChart draws the bar chart.
	 * @param  g - Graphics
	 * @return void
	 *
	 */
	protected void DrawBarChart(Graphics g) {

		int nTopBound = 120;			  // top boundary
		int nBottomBound = d.height - 20;	  // bottom boundary
		int nLeftBound = 50;			  // left boundary
		int nRightBound = d.width - 50;		  // right boundary
		int nVertical = nBottomBound - nTopBound; // vertical area
		int nTextPlacement = nBottomBound + 5;	  // places vert text
		int nLinePlacement = nBottomBound;	  // places lines
		int nLines;				  // number of lines
		int nLineStep;				  // line spacing
		int nN;
		int nHeight;				  // height of bar
		double dI = 0.0;
		double dStep = 0.5;			  // step value
		double dMax = 0;			  // max result
		double dValue;				  // ind result
		String strAmount;			  // string value of amount
	
		/* colArray is an array of colors differentiating the currencies
			from each other */
		Color colArray[] = {Color.blue, Color.red, Color.green,
				    Color.orange};

		/* gets the maximum result of cConversions[nN].result(nAmount) */
		for (nN = 0; nN < nNumCurrencies; nN++) {
			if (nN == 0) {
				dValue = cConversions[nN].result(dAmount);
				strAmount = dfFormat.format(dValue);
				dMax = Double.valueOf(strAmount).doubleValue();
			} else {
				dValue = cConversions[nN].result(dAmount);
				strAmount = dfFormat.format(dValue);
				dMax = Math.max(dMax, Double.valueOf(strAmount).doubleValue());
			}
		}

		/* calculates the number of lines and line spacing */
		nLines = ((int)(dMax/.5)) + 2;
		nLineStep = nVertical / nLines;
		
		/* sets the Color to black and font size to default */
		g.setColor(Color.black);
		g.setFont(font);

		/* draws the lines and places step values alongside the lines 
			for nN = 0 to nLines */
		for (nN = 0; nN < nLines; nN++) {
			g.drawLine(nLeftBound, nLinePlacement, nRightBound, nLinePlacement);
			nLinePlacement -= nLineStep;
			g.drawString("" + dI, 10, nTextPlacement);
			nTextPlacement -= nLineStep; 
			dI += dStep;
		}

		/* draws the bar on the chart for nN = 0 to nNumCurrencies */
		for (nN = 0; nN < nNumCurrencies; nN++) {
			dValue = cConversions[nN].result(dAmount);
			nHeight = (int)(dValue * nLineStep * 2);
			g.setColor(colArray[nN]);
			g.drawString("" + strCurr[nN], 75 + nN * 50, 
					nBottomBound + 15);
			g.drawRect(75 + nN * 50, nBottomBound - nHeight, 
					25, nHeight);
			g.fillRect(75 + nN * 50, nBottomBound - nHeight, 
					25, nHeight);
		}
	} 
}
