Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/rakiura/mbot/CurrencyTable.java


1   
2   package org.rakiura.mbot;
3   
4   /**/
5   import java.util.*;
6   import java.io.*;
7   
8   /**
9    * General purpose object class supporting currency conversions.
10   * The data must be presented in text file by rows as:
11   * currency_3_letters_code  unit?  ?unit comments
12   * and one row must have ??? 1.0 1.0 which will indicate that this row is
13   * the base currency for a given table. E.g.
14   * USD 0.526990 1.89757 Dollars States United
15   * EUR 0.507634 1.96992 Euro
16   * the first float is 1baseunit/unit second unit/1baseunit
17   *
18   *@version version 0.3 $Revision: 1.1.1.1 $
19   *@author Mariusz Nowostawski 
20   */
21  public class CurrencyTable {
22  
23    private Hashtable table;
24    private String datapath;
25  
26    /**
27     *
28     *@param datapath WITH final \ or /
29     *@param current data filename
30     */
31    public CurrencyTable(String datapath, String current){
32      this.datapath = datapath;
33      table = new Hashtable(72);
34      readData(datapath+current);
35    }
36  
37    /**/
38    public void readData(String filename){
39      BufferedReader f = null;
40      StringTokenizer tt;
41      try{
42        f = new BufferedReader(new FileReader(filename));
43      }catch(FileNotFoundException e){
44        System.out.println("ERROR: Currency table - file not found");
45        return;
46      }
47  
48      String line = new String("#");
49      String tmp;
50      String code;
51      float perunit, unit;
52      try{
53        while( (line = f.readLine()) != null){
54          line = line.trim().toLowerCase();
55          if(line.startsWith("#") || line == "") ;//skipp it
56          else{
57            tt = new StringTokenizer(line);
58            if(tt.countTokens() > 2){//assuming correct data line
59              //first code
60              code = tt.nextToken();
61              //second per unit ratio
62              tmp = tt.nextToken();
63              perunit = toFloat(tmp);
64              //second unit per base ratio
65              tmp = tt.nextToken();
66              unit = toFloat(tmp);
67              tmp = "";
68              while(tt.hasMoreTokens()) tmp += " "+tt.nextToken();
69              table.put(code, new CurrencyEntry(perunit, unit, tmp));
70            }
71          }
72        }
73      }catch(IOException ex){ex.printStackTrace();}
74    }
75  
76    /**
77     *
78     *@param input currency three characters currency code
79     *@param output currency three characters currency code
80     *@return ratio x  in conversion  1 in to x out, 
81     * -1 if data is not valid or not available.
82     */
83    public float getRatio(String sin, String sout){
84      CurrencyEntry in = (CurrencyEntry)table.get(sin.toLowerCase());
85      CurrencyEntry out = (CurrencyEntry)table.get(sout.toLowerCase());
86      if(in == null || out == null) return -1;
87      float x = in.getUnits();
88      float y = out.getPerUnit();
89      if(x < 0 || y < 0) return -1;
90      return x*y;
91    }
92  
93    public String getInfo(String currency){
94      CurrencyEntry in = (CurrencyEntry)table.get(currency.toLowerCase());
95      if(in == null) return null;
96      return in.getInfo();
97    }
98  
99  
100   /**/
101   private float toFloat(String tmp){
102     float f;
103     try{
104       f = Float.valueOf(tmp).floatValue();
105     }catch(NumberFormatException numex){
106       System.out.println("ERROR: Currency table - float value not in proper format.");
107       return -1;
108     }
109     return f;
110   }
111 
112 }
113 
114 
115 /**
116  *
117  */
118 class CurrencyEntry {
119     
120     private float perUnit; //how much this units for single base unit
121     private float units; //how much base units for single this unit 
122     private String info; //currency description`
123     
124     /**/
125     public CurrencyEntry(float perunit, float units, String info) {
126       this.perUnit = perunit;
127       this.units = units;
128       this.info = info;
129     }
130     
131     public float getUnits(){return units;}
132     public float getPerUnit(){return perUnit;}
133     public String getInfo(){return info;}
134     
135 }
136 
137 
138 
139 /////////////// end of file ///////////////