Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to use a custom made CurrecenyConverter class. Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
use a CurrecenyConverter class. 27-Jun-05
Category
Algorithm
Language
JBuilder All Versions
Views
139
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
Harrington, Mark
Reference URL:
			1   import java.net.*;
2   import java.awt.*;
3   import java.awt.event.*;
4   import java.io.*;
5   import java.net.*;
6   import java.util.*;
7   
8   public class CurrencyConverter extends Frame implements ActionListener, KeyListener
9   
10  /* The Idea behind this java programme is to run a standalone application to 
11  request currency converstions  you could possibly implement this in a 
12  database or using JDBC as an interface bewteen a access database as a  stage
13   further to enhance other applications with repect to entering into the 
14   European market */
15  
16  /* with addittional code you could also implement this as an applet and 
17   incorperate this into your webpage */
18  
19  /* Written By MD Harrington June 2005 email elite_electronics@ntlworld.com "
20   * This program is free software; you can redistribute it and/or modify
21   * it under the terms of the GNU General Public License as published by
22   * the Free Software Foundation; either version 2 of the License, or
23   * (at your option) any later version. IT is not however  for sale to the public 
24   * please contact elite_electronics@ntlworld.com should you have any further 
25   *queries */ 
26  
27  
28  {
29  
30  private HttpURLConnection  connection ; // handles all http requests returned
31                                         // info and so on
32  private URL url ; // for creating a url object to which we want to post data 
33  private TextField currency, curr1, curr2; // for entering amount to be 
34                                            //converted an displaying countries
35  private Button submit ; // for submitting the information to the cgi webserver
36  private java.awt.List country1, country2 ; // for listing the country 
37                                              //converstion requirements 
38  private GridBagLayout gb ; // for constructing Gridbaglayout for this GUI
39  private GridBagConstraints gbconstraints; // for setting the grid spacings
40                                            //  between components
41  private TextArea ta ; // for getting the returned results and displaying 
42                                              //them accordingly 
43  private BufferedReader in ; // for getting the returned data 
44  private PrintWriter out ; // for sending data to the cgi server 
45  private Panel p1 ,p2,p3,p4; // for adding our components to 
46  private ResourceBundle res ;
47  private Canvas spacer1, spacer2, spacer3 ,spacer4; // spacers for seperating 
48                                                // components  
49  private Label lb1,lb2,lb3,lb4,lb5,lb6 ; // labels to indicate components are 
50  private Enumeration enum ;
51  
52  
53  	// Start of with our basic constructor for our class 
54   // Rule all constructors have the same name as our class template 
55      public CurrencyConverter()
56  	{
57              // invoke Frames contsructor and set the title of our frame 
58  		super("Marks Currency Convertor"); 
59  		
60  		// add window listener for closing the window
61  		this.addWindowListener(new WindowAdapter()
62  				{
63  				public void windowClosing(WindowEvent e)
64  						{
65  							closeWin();
66  
67  				}});
68  		try
69  		{
70  			//create a URL for posting our data too
71  			url = new URL("http://www.xe.com/ucc/convert.cgi");
72  			System.out.println("Succesfull Connection obtained");
73  		}
74  		catch (MalformedURLException e )
75  		{
76  // if the URL changes we want to be able to catch the error 
77  // later we can change this to show an additional frame with this message 
78  		System.out.println("Unable to connect to specified URL");
79  		}
80  
81  		// get our rescourse list 
82  
83  
84  
85  	// begin our GUI Constructor 
86  
87  		createGUI();
88  		this.add(p1);
89  		this.setSize(600,300);
90  		this.setResizable(false); 
91  		this.show();
92  
93  	}// end our constructor definition 
94  
95  
96      /* define how we terminate our GUI using Close Window */ 
97  	
98  	public void closeWin()
99  
100 	{
101 	  hide();     // hide the window 
102       dispose(); // free rescources
103 	  System.exit(1); // terminate program
104 	} // end close window method 
105 
106 
107 
108 	
109 
110 	public void createGUI()
111 
112 	{
113 		
114 		p1 = new Panel(); // our panel to which we will add components 
115 		// set up the layout manager for the panel 
116 		p1.setLayout(gb= new GridBagLayout()); 
117 		// istantiate gridbagcontsraints 
118 		gbconstraints = new GridBagConstraints();
119 		
120 		//create a spacer 
121 
122 		spacer1 = new Canvas();
123 		spacer1.setBackground(Color.blue);
124 		spacer1.setSize(440,20);
125 		
126 		// set up our contraints values all objects 
127 
128 		gbconstraints.fill = GridBagConstraints.BOTH ;
129 
130 		// add this too our panel 
131 		this.addComponent(p1,spacer1,gb,gbconstraints,0,0,60,1);
132 
133 
134 		// create a label
135 
136 		lb1 = new Label("Enter amount");
137 		
138 
139 		// add component to panel 
140 		this.addComponent(p1,lb1,gb,gbconstraints,1,0,8,1);
141 
142         // create a second label
143 
144 		lb2 = new Label("From Currency",Label.CENTER);
145 		
146 
147 		// add component to panel 
148 		this.addComponent(p1,lb2,gb,gbconstraints,1,10,20,1);
149 		
150 
151 		// create label3
152 		lb3 = new Label("To Currency",Label.CENTER);
153 		
154 
155 		// add component to panel 
156 		this.addComponent(p1,lb3,gb,gbconstraints,1,32,20,1);
157 		
158 		
159 		// create textfield currency
160 		
161 		currency = new TextField(8);
162 		// add keylistener we want to be able to detect NAN 
163 		currency.addKeyListener(this); 
164 		
165 		
166 		// add component to panel 
167 		this.addComponent(p1,currency,gb,gbconstraints,2,0,8,1);
168         
169 
170 
171 
172 		//create a spacer 
173 		spacer2 = new Canvas();
174 		spacer2.setBackground(Color.white);
175 		spacer2.setSize(20,18);
176 
177 
178 		// add this too our panel 
179 		this.addComponent(p1,spacer2,gb,gbconstraints,2,8,2,1);
180 
181 
182 	// create two texboxes to display countries that we have selected 
183         curr1 = new TextField(20) ;
184 		
185 		this.addComponent(p1,curr1,gb,gbconstraints,2,10,20,1);
186 
187 
188 		//create a spacer 
189 		spacer3 = new Canvas();
190 		spacer3.setBackground(Color.white);
191 		spacer3.setSize(20,18);
192 
193 
194 		
195 
196 		// add this too our panel 
197 		this.addComponent(p1,spacer3,gb,gbconstraints,2,30,2,1);
198 
199 
200 
201 		curr2 = new TextField(22);
202         
203 		// add component to panel 
204 		this.addComponent(p1,curr2,gb,gbconstraints,2,32,20,1);
205 
206 
207 		// create 4th label
208 
209 		lb4 = new Label("Select From Currency",Label.CENTER);
210 		lb5 = new Label("Select  To  Currency",Label.CENTER);
211 		
212 		p2 = new Panel();
213 		p2.setLayout(new GridLayout(1,2,5,5));
214 		p2.add(lb4);
215 		p2.add(lb5);
216 
217 		// add component to panel 
218 		this.addComponent(p1,p2,gb,gbconstraints,3,0,60,1);
219 		// create spacer 
220 
221 		
222 // create new list country1 country2
223 
224 // we need to expicitly call constructor because we are using both java.util
225 //  pacakage ans java.awt
226 
227  // reason for this is because awt includes a List Object and so does java.util 
228 // both have the same name OH Dear Yes you might ask why is that cough cough 
229 
230 		country1 = new java.awt.List(5); 
231         country2 = new java.awt.List(5);
232 
233 		
234 		// get our rescource bundle 
235         res = ResourceBundle.getBundle("CurrencyConverstions");
236         // populate our list with desriptions 
237    Enumeration enum = res.getKeys(); 
238    // get an enumeration object of the bundle keys
239 			
240 while (enum.hasMoreElements())
241 {
242         try
243         {
244         String myKey = (String)enum.nextElement(); // cast to a string object 
245         country1.addItem(res.getString(myKey));	// get the associated match 		
246         country2.addItem(res.getString(myKey));
247         }
248         catch(MissingResourceException e)
249         {
250                 System.out.println("No such value for key Unable to complete");
251 
252         }
253 
254 
255 } // end while method 
256 
257          // add action listener to both list objects
258 
259 		country1.addActionListener(this);
260 		country2.addActionListener(this);
261 	    
262 		// create a new panel and add components country1 and country2 
263 		p3 = new Panel();
264 		p3.setLayout(new GridLayout(1,2,5,5));
265 		p3.add(country1);
266 		p3.add(country2);
267 
268 		this.addComponent(p1,p3,gb,gbconstraints,4,0,60,5);
269 
270 		// create a new panel and button component 
271 
272 		p4 = new Panel();
273 		p4.setLayout(new GridLayout(1,1,50,50));
274 
275 		submit = new Button("Convert Currency");
276 		submit.addActionListener(this);
277 		submit.setActionCommand("Submit Currency");
278 		submit.setBackground(Color.blue);
279 		submit.setForeground(Color.white);
280 		p4.add(submit);
281 		this.addComponent(p1,p4,gb,gbconstraints,9,0,60,1);
282 
283 	      
284 
285     } // end GUI constructor
286 
287 
288 
289 
290   // user defined as to how you whish to layout components 
291 
292 	public void addComponent(Panel p, Component c, GridBagLayout g, 
293  GridBagConstraints gc,int row , int column,int width,int height)
294 
295 	{
296 	
297 	 gc.gridx =column ;
298 	 gc.gridy = row ;
299 	 gc.gridwidth = width ;
300 	 gc.gridheight = height ;
301 
302 	 g.setConstraints(c,gc); // set up the values for layout
303 	 p1.add(c) ; // add component to the panel 
304 
305 
306 
307 	} // end addComponent method 
308 
309 
310    public void keyTyped(KeyEvent e)
311 			{
312 				
313 				if(e.getKeyChar() == KeyEvent.VK_ENTER)
314 				{
315 					// define method here 
316 					
317 				}
318 
319 			}// end keyevent 
320 	
321 
322 // not used 
323 
324 /* We have to declare these methods since we are implementing an abstract class */
325 
326 public void keyPressed(KeyEvent e){}
327 public void keyReleased(KeyEvent e){}
328 
329 /* Action event method to deal with user interactions */
330 
331 public void actionPerformed(ActionEvent e)
332 
333 {
334 
335 
336 
337 		if (e.getSource()== country2)
338 		{
339 
340 		curr2.setText(country2.getSelectedItem());
341 
342 		}
343 		
344 		if (e.getSource()== country1)
345 		{
346 		curr1.setText(country1.getSelectedItem());
347 
348 			
349 		}
350 
351 		if (e.getSource()== submit)
352 		{
353 			String To="";
354 			String From ="";
355 			String outString = "" ;
356 			String Amount ="&Amount=";
357 			String submit ="submit=Perform+Currency+Conversion";
358 			boolean match1 = false ;
359 			boolean match2 = false ;
360 			boolean isNumber = false ;
361             float amount =0.0f;
362 
363 			
364 			
365 
366 
367 			if(!(currency.getText().equals("")))
368 			{
369 			
370 			try{
371 			
372 				amount = float.parseFloat(currency.getText());
373 				System.out.println(amount);
374 				isNumber = true ;
375 					
376 					try
377 					{
378 					Amount += 	URLEncoder.encode(currency.getText(), "UTF-8");
379 					}
380 					catch(UnsupportedEncodingException f){}			
381 
382 
383 
384 
385 				}
386 				catch(NumberFormatException g)
387 				{
388 					amount = 0.0f;
389 					System.out.println("Exception caught setting amount to " + amount);
390 					currency.setText("0.0");
391 					isNumber = true ;			
392 					
393 					try
394 					{
395 					Amount += 	URLEncoder.encode(currency.getText(), "UTF-8");
396 					}
397 					catch(UnsupportedEncodingException f){}
398 					
399 				
400 				}
401 
402 
403 		    } // end if 
404 			else
405 			{
406 				
407 					amount = 0.0f;
408 					currency.setText("0.0");
409 					isNumber = true ;
410 			        System.out.println("No Input for amount setting amount to " + amount );
411 					try
412 					{
413 					Amount += 	URLEncoder.encode(currency.getText(), "UTF-8");
414 					}
415 					catch(UnsupportedEncodingException f){}
416 			}
417 
418 
419 			Enumeration enum1 = res.getKeys();
420 			
421 			
422 			while(enum1.hasMoreElements())
423 			{
424 				String myKey = (String)enum1.nextElement();
425 				
426 				if (res.getString(myKey).equals(curr2.getText()))
427 				{
428 				 match2= true ;
429 				
430 				//?Amount=1&From=EUR&To=USD&submit=Perform+Currency+Conversion
431 				System.out.println(myKey + " matches " + res.getString(myKey) + " = " + 
432 curr2.getText() );	
433 				
434 				try
435 					{
436 
437 					To ="To=" +	URLEncoder.encode(myKey, "UTF-8");
438 					
439 				    
440 					}
441 					catch(UnsupportedEncodingException f){}
442 				}// end if 
443 				
444 				
445 				
446 				
447 				if (res.getString(myKey).equals(curr1.getText()))
448 				{
449 				 
450 				 match1 = true ;
451 
452 				//?Amount=1&From=EUR&To=USD&submit=Perform+Currency+Conversion
453 				System.out.println(myKey + " matches " + res.getString(myKey) + " = " + 
454 curr1.getText() );	
455 				
456 				try
457 					{
458 
459 					From ="From=" +	URLEncoder.encode(myKey, "UTF-8");
460 					
461 				    
462 					}
463 					catch(UnsupportedEncodingException f){}
464 				}// end if 
465 			
466 			
467 			} // end while(enum1.hasMoreElements()) 
468 
469 			
470 			
471 			if((match1==true)&&(match2==true)&&(isNumber==true))
472 			
473 			
474 			{
475 
476 			System.out.println(Amount+"&"+From+"&"+To+"&"+submit);
477 
478 			
479 				try
480 				{
481 				connection = (HttpURLConnection) url.openConnection();
482 				connection.setRequestMethod("POST");
483 				connection.setDoOutput(true);
484 				
485 				
486 				out = new PrintWriter(connection.getOutputStream());
487 				out.println(Amount+"&"+From+"&"+To+"&"+submit);
488 				out.close();
489 
490 				in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
491 				String line;
492 						
493 						while ((line = in.readLine()) != null)
494 						{
495 						System.out.println(line);
496 						} // end while 
497 						in.close();
498 				
499 				}// end try block
500 				catch(IOException ioe){}
501 				
502 
503 
504 			}
505 			else
506 			{ System.out.println("Information Missing from Fields");} // end 
507 if((match1==true)&&(match2==true)&&(isNumber==true))
508 			
509 			
510 			
511 	 } // end if (e.getSource()== submit)
512 
513 
514 
515 		
516 
517 }
518 
519 
520 
521 	public static void main(String[] args) 
522 	{
523 		CurrencyConverter myObject = new CurrencyConverter();
524 	}
525 }


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC