//Author: ^-^ Veerle ^-^ import java.io.*; import java.util.Vector; import java.text.DecimalFormat; public class currencyConverter2W //Class for writing the currencies and conversion rates to a txt file { private boolean isError = false; private String strError = ""; private DecimalFormat decFormat = new DecimalFormat("#0.00"); public currencyConverter2W () { } public void createDefault () //Used if the file can't be found, creates a default file { Vector vecCurrencies = new Vector(); //Vector is an array that can grow vecCurrencies.addElement("Pound"); vecCurrencies.addElement("Euro"); vecCurrencies.addElement("Dollar"); float[][] conversionMatrix = new float[3][3]; conversionMatrix[0][0] = 1.00f; //Default conversion rates (probably wrong now) conversionMatrix[0][1] = 1.45f; conversionMatrix[0][2] = 1.74f; conversionMatrix[1][0] = 0.69f; conversionMatrix[1][1] = 1.00f; conversionMatrix[1][2] = 1.21f; conversionMatrix[2][0] = 0.57f; conversionMatrix[2][1] = 0.83f; conversionMatrix[2][2] = 1.00f; write(vecCurrencies, conversionMatrix); } private void addCurrentLine (String strBuffer, BufferedWriter writeFile) //Writes a line to the txt file { try //Attempts to execute the following code, if an error occurs it is caught { //Write the text, from index 0 (start) to the length of the current text writeFile.write(strBuffer, 0, strBuffer.length()); writeFile.newLine(); //Creates a new line } catch (IOException e) { isError = true; strError = "Error writing to the file"; } } public void write (Vector vecCurrencies, float[][] conversionMatrix) //Method called when data is to be written { isError = false; try { BufferedWriter writeFile = new BufferedWriter(new FileWriter("currencyConverter2Instructions.txt")); String strBuffer = ""; for (int i = 0; i < 6; i++) //Repeat 6 times { switch (i) //Takes the variable i, and executes one of the following lines depending on the state of it { //Creates the header stuff case 0: strBuffer = "/Lines starting with / are comments, # is start of a section, ~ is end of a section"; break; case 1: strBuffer = "/( is start of a field, ) is end of a field"; break; case 2: strBuffer = "/Do not edit this file!!!!!!!!!!!!!!!! The programme can make all modifications itself"; break; case 3: //Line 3 or 4 both share the same case 4: strBuffer = "/"; break; case 5: strBuffer = "#supported currencies"; break; } addCurrentLine(strBuffer, writeFile); //Write the current line to the txt file } for (int i = 0; i < vecCurrencies.size(); i++) //Repeat for how many currencies { strBuffer = vecCurrencies.elementAt(i).toString(); //Get each element in turn and convert it into a string addCurrentLine(strBuffer, writeFile); } for (int i = 0; i < 4; i++) //Repeat 4 times { switch (i) { case 0: strBuffer = "~end of supported currencies"; break; case 1: case 2: strBuffer = "/"; break; case 3: strBuffer = "#conversions"; break; } addCurrentLine(strBuffer, writeFile); } for (int row = 0; row < vecCurrencies.size(); row++) //write the conversion rates { for (int column = 0; column < vecCurrencies.size(); column++) { //Outputs in the format sourceCurrency to desCurrency (conversionRate) strBuffer = "" + vecCurrencies.elementAt(row) + " to " + vecCurrencies.elementAt(column) + " (" + decFormat.format(conversionMatrix[row][column]) + ")"; addCurrentLine(strBuffer, writeFile); } } strBuffer = "~end of conversion"; addCurrentLine(strBuffer, writeFile); writeFile.close(); //Closes the file } catch (IOException e) { isError = true; strError = "Error opening file to write to"; } } public boolean isError () { return isError; } public String getErrorMsg () { return strError; } }