Clicky

 package Ass3; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JComboBox; import javax.swing.JFormattedTextField;   public class Converter extends javax.swing.JFrame {      /** Creates new form Converter */      String currentPattern;     private HashMap<String, Double> HashMap1;     private ArrayList<String> List;     private ArrayList<String> currencies  = new ArrayList<String>();;;     private Converter cc;     DecimalFormat df = new DecimalFormat("#####.00");      /** Creates new form Converter */      public Converter() throws Exception{     initComponents();  HashMap1= new HashMap<String, Double>(); List = new ArrayList<String>(); try{  URL ecb = new URL("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"); BufferedReader in = new BufferedReader(new InputStreamReader(ecb.openStream())); FileWriter fw = new FileWriter("Rates.xml"); String inputLine;   while ((inputLine = in.readLine()) != null){ System.out.println(inputLine); fw.write(inputLine);  String buff = inputLine;  }  in.close(); fw.close(); } catch(Exception e){     e.printStackTrace(); System.exit(1); }          /** Creates new form CurrencyConverterGUI */         String[] patternExamples = {"EUR", "USD"};         List = new ArrayList<String>();          XMLParser xp = new XMLParser();         xp.Init();        xp.parseAll();         HashMap1 = xp.getHashMap();         currencies.addAll(HashMap1.keySet());         for(String ss: currencies)System.out.println(ss);          CurrencyBox1.removeAll();          CurrencyBox2.removeAll();          for(String s: currencies){               CurrencyBox1.addItem(s);                 CurrencyBox2.addItem(s);          }                CurrencyBox1.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 CurrencyBox1ActionPerformed(evt);             }         });                CurrencyBox2.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 CurrencyBox2ActionPerformed(evt);             }         });          CurrencyBox1.setEditable(false);              CurrencyBox2.setEditable(false);    }       private void populateHashMap() {         Scanner SC = null;         try {              SC = new Scanner(new FileReader(new File("Rates.txt")));         } catch (IOException e) {             System.err.println("File Error when reading rates.txt file");             System.exit(1);         }         while (SC.hasNextLine()) {             String[] S = SC.nextLine().split(" ");             currencies.add(S[0]);          }     }           public Object[] getCurrencies() { 		return List.toArray(); 	}  	public double convert(String FC, String TC, double amt) { 	double result = amt / HashMap1.get(FC) * HashMap1.get(TC); 		return result;                  }     @SuppressWarnings("unchecked")     // <editor-fold defaultstate="collapsed" desc="Generated Code">     private void initComponents() {          CurrencyBox1 = new javax.swing.JComboBox();         CurrencyText1 = new javax.swing.JTextField();         CurrencyBox2 = new javax.swing.JComboBox();         CurrencyText2 = new javax.swing.JTextField();         ResetButton = new javax.swing.JButton();          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);          CurrencyBox1.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 CurrencyBox1ActionPerformed(evt);             }         });          CurrencyText1.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 CurrencyText1ActionPerformed(evt);             }         });          CurrencyBox2.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 CurrencyBox2ActionPerformed(evt);             }         });          CurrencyText2.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 CurrencyText2ActionPerformed(evt);             }         });          ResetButton.setText("Reset");         ResetButton.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 ResetButtonActionPerformed(evt);             }         });          org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());         getContentPane().setLayout(layout);         layout.setHorizontalGroup(             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)             .add(layout.createSequentialGroup()                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                     .add(layout.createSequentialGroup()                         .addContainerGap()                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                             .add(CurrencyBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                             .add(CurrencyBox1, 0, 77, Short.MAX_VALUE))                         .add(18, 18, 18)                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                             .add(CurrencyText1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)                             .add(CurrencyText2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)))                     .add(layout.createSequentialGroup()                         .add(72, 72, 72)                         .add(ResetButton)))                 .addContainerGap())         );         layout.setVerticalGroup(             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)             .add(layout.createSequentialGroup()                 .addContainerGap(35, Short.MAX_VALUE)                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)                     .add(CurrencyBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                     .add(CurrencyText1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)                     .add(CurrencyBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                     .add(CurrencyText2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                 .add(ResetButton))         );          pack();     }// </editor-fold>      private void CurrencyBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                                         String from = (String) CurrencyBox2.getSelectedItem();           String to =(String)  CurrencyBox1.getSelectedItem();           String s=  this.CurrencyText2.getText();                  if(s.trim().length() == 0){                        return;                           }     }                                                  private void CurrencyBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                                       String from = (String) CurrencyBox1.getSelectedItem();         String to = (String) CurrencyBox2.getSelectedItem();          String s=  CurrencyText1.getText();                if(s.trim().length() == 0){                         return;                           }     }                                                  private void CurrencyText1ActionPerformed(java.awt.event.ActionEvent evt) {                                                       // TODO add your handling code here:     }                                                   private void CurrencyText2ActionPerformed(java.awt.event.ActionEvent evt) {                                                       // TODO add your handling code here:     }                                                   private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     CurrencyText1.setText("");          CurrencyText2.setText("");        // TODO add your handling code here:     }                                                 /**      * @param args the command line arguments      */     public static void main(String args[]) {         /* Set the Nimbus look and feel */         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html           */         try {             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {                 if ("Nimbus".equals(info.getName())) {                     javax.swing.UIManager.setLookAndFeel(info.getClassName());                     break;                 }             }         } catch (ClassNotFoundException ex) {             java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);         } catch (InstantiationException ex) {             java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);         } catch (IllegalAccessException ex) {             java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);         } catch (javax.swing.UnsupportedLookAndFeelException ex) {             java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);         }         //</editor-fold>          /* Create and display the form */         java.awt.EventQueue.invokeLater(new Runnable() {              @Override             public void run() {                 try {                     new Converter().setVisible(true);                 } catch (Exception ex) {                     Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);                 }             }         });     }     // Variables declaration - do not modify     private javax.swing.JComboBox CurrencyBox1;     private javax.swing.JComboBox CurrencyBox2;     private javax.swing.JTextField CurrencyText1;     private javax.swing.JTextField CurrencyText2;     private javax.swing.JButton ResetButton;     // End of variables declaration }                             
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 

Select allOpen in new window



 /*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */  package Ass;  import javax.xml.stream.*; import javax.xml.stream.events.*; import javax.xml.namespace.*; import java.util.*; import java.io.*;  public class XMLParser {     private XMLInputFactory xif;     private XMLEventReader parser;     private FileReader infile;     private static HashMap<String, Double> m = new HashMap<String, Double>();     private String curr;     private String rat;        public void Init(){         xif = XMLInputFactory.newInstance();         try{             infile = new FileReader("rates.xml");             parser = xif.createXMLEventReader(infile);         }catch(Exception e){             System.err.println(e);         }     }      public HashMap<String, Double> getHashMap() {         return m;     }      public void parseAll(){         try{             while(parser.hasNext()){                 XMLEvent e = parser.nextEvent();                 if(e.isStartElement()){                     StartElement se = e.asStartElement();                     String tag = se.getName().getLocalPart();                     System.out.println("Start Tag Name = " + tag);                     if(tag.equals("Cube")){                         curr = null;                         rat = null;                     }                      Iterator<Attribute> it = se.getAttributes();                     while(it!=null && it.hasNext()){                         Attribute att = it.next();                         String AttName = att.getName().toString();                         String AttVal = att.getValue();                         System.out.println("Attribute Name = " + AttName);                         System.out.println("Attribute Value = " + AttVal);                         if(AttName.equals("rate")) rat = AttVal;                         else    if(AttName.equals("currency")) curr = AttVal;                        }                 }                  if(e.isCharacters()){                     String TagData=e.asCharacters().getData();                     System.out.println("Tag Data = " + TagData);                 }                  if(e.isEndElement()){                     EndElement ee = e.asEndElement();                     String tag = ee.getName().getLocalPart();                     System.out.println("End Tag Name = " + tag);                     if(tag.equals("Cube")){                          if(curr != null && rat != null){                             m.put(curr, new Double(rat));                         }                     }                 }             }         }catch(XMLStreamException e){             System.err.println(e);         }          try{             infile.close();         }catch(IOException ioe){             System.err.println(ioe);         }     }      public void parseProduct(String s){         Init();         try{             while(parser.hasNext()){                 XMLEvent e = parser.nextEvent();                 if(e.isStartElement()){                     StartElement se = e.asStartElement();                     if(se.getName().getLocalPart().equals("Product")){                         Attribute IdAtt = se.getAttributeByName(new QName("Id"));                         if(IdAtt.getValue().equals(s)){                             System.out.println("Product Id: " + s);                             while(parser.hasNext()){                                 e=parser.nextEvent();                                 if(e.isStartElement()){                                     se = e.asStartElement();                                     String tag = se.getName().getLocalPart();                                     System.out.println("Start Tag Name = " + tag);                                      Iterator<Attribute> it = se.getAttributes();                                     while(it!=null && it.hasNext()){                                         Attribute att = it.next();                                         String AttName = att.getName().toString();                                         String AttVal = att.getValue();                                         System.out.println("Attribute Name = " + AttName);                                         System.out.println("Attribute Value = " + AttVal);                                     }                                 }                                  if(e.isCharacters()){                                     String TagData=e.asCharacters().getData();                                     System.out.println("Tag Data = " + TagData);                                 }                                  if(e.isEndElement()){                                     EndElement ee = e.asEndElement();                                     String tag = ee.getName().getLocalPart();                                     if(tag.equals("Product")) return;                                     System.out.println("End Tag Name = " + tag);                                 }                              }                          }                     }                 }             }         }catch(XMLStreamException e){             System.err.println(e);         }     }                             
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 

Select allOpen in new window

asked 12/09/2011 11:25

JAVVA's gravatar image

JAVVA ♦♦


2 Answers:
the first code is called Converter.java
the second code is code XMLParser.java
link

answered

JAVVA's gravatar image

JAVVA

Thanks I did it
link

answered 2011-12-09 at 19:26:39

JAVVA's gravatar image

JAVVA

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×21
×2
×141
×1
×67

Asked: 12/09/2011 11:25

Seen: 304 times

Last updated: 12/12/2011 01:20