/*
 * File: CurrencyConverter.java
 * ----------------------------
 * This program converts monetary entries from one currency to another.
 */

import acm.program.*;
import acm.util.*;
import java.util.*;


/** This class implements a simple currency converter. */
public class CurrencyConverter extends ConsoleProgram {

	public void run() {
		currencyTable = new CurrencyConversionTable(CURRENCY_FILE);
		while (true) {
			try {
				String line = readLine("Conversion: ").toUpperCase();
				if (line.length() == 0) break;
				StringTokenizer tokenizer = new StringTokenizer(line);
				double fromValue = Double.parseDouble(tokenizer.nextToken());
				String fromCode = tokenizer.nextToken();
				if (!tokenizer.nextToken().equals("TO")) {
					throw new ErrorException("Missing \"to\" in conversion");
				}
				String toCode = tokenizer.nextToken();
				double fromRate = currencyTable.getRate(fromCode);
				double toRate = currencyTable.getRate(toCode);
				String fromName = currencyTable.getName(fromCode);
				String toName = currencyTable.getName(toCode);
				double toValue = fromValue * fromRate / toRate;
				print(fromValue + " " + fromName + " (" + fromCode + ") = ");
				println(toValue + " " + toName + " (" + toCode + ")");
			} catch (ErrorException ex) {
				println(ex.getMessage());
			}
		}
	}
	
/* Data file name */

	private static final String CURRENCY_FILE = "CurrencyData.txt";

/* Private instance variables */

	private CurrencyConversionTable currencyTable;
}
