Saturday, April 27, 2013

Currency Conversion program

This is a simple currency conversion program. The program allows you to convert from USD amount to Riel amount and vise versa and from EURO amount to USD amount and vise versa. In this program, you will learn to use  the JTabbedPane class, and JComboBox lass, and to write Java code to detect key typed.

CurrencyConversion source code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class  CurrencyConversion extends JFrame implements ActionListener{
            JTabbedPane tbpane;
            JPanel panelt1;
            JLabel lblus;
            JLabel lblkh;
            JLabel lblcbkh;
            JTextField txtus;
            JTextField txtkh;
            JComboBox cbratekh;
            JButton btuskh;
           
            JPanel panelt2;
            JLabel lbleuro;
            JLabel lbleurous;
            JLabel lblcbus;
            JTextField txteurous;
            JTextField txteuro;
            JComboBox cbrateus;
            JButton bteurous;   
           

            CurrencyConversion(){
                       
1                      setTitle("Currency Conversion Program");
2                      setDefaultCloseOperation(EXIT_ON_CLOSE);
3                      setSize(300,250);
4                      setResizable(false);
5                      Container cont=getContentPane();

                        //USD to Riel conversion interface
6                      lblus=new JLabel("USD:");
7                      lblkh=new JLabel("Riel:");
8                      lblcbkh=new JLabel("Select exchange rate:");
9                      btuskh=new JButton("OK");
10                    btuskh.addActionListener(this);               
11                    txtus=new JTextField(10);
12                    txtus.addKeyListener(new KeyList());
13                    txtkh=new JTextField(10);
14                    txtkh.addKeyListener(new KeyList());
15                    Object[] ratekh={3800,3900,4000,4100,4200};
16                    cbratekh=new JComboBox(ratekh);
17                    cbratekh.setEditable(true);
18                    cbratekh.getEditor().getEditorComponent().addKeyListener(new KeyList());
19                    panelt1=new JPanel();
20                    panelt1.add(lblcbkh);
21                    panelt1.add(cbratekh);
22                    panelt1.add(lblus);
23                    panelt1.add(txtus);
24                    panelt1.add(lblkh);
25                    panelt1.add(txtkh);
26                    panelt1.add(btuskh);
27                    panelt1.setLayout(new GridLayout(4,1));
                       
                        //EURO and USD conversion interface
                                                                       
28                    lbleuro=new JLabel("Euro:");
29                    lbleurous=new JLabel("USD:");
30                    lblcbus=new JLabel("Select exchange rate:");
31                    bteurous=new JButton("OK");
32                    bteurous.addActionListener(this);                       
33                    txteuro=new JTextField(10);
34                    txteuro.addKeyListener(new KeyList());
35                    txteurous=new JTextField(10);
36                    txteurous.addKeyListener(new KeyList());
37                    Object[] rateus={1.25,1.28,1.30,1.32,1.35,1.40};
38                    cbrateus=new JComboBox(rateus);
39                    cbrateus.setEditable(true);
40                    cbrateus.getEditor().getEditorComponent().addKeyListener(new KeyList());
41                    panelt2=new JPanel();
42                    panelt2.setLayout(new GridLayout(4,1));
43                    panelt2.add(lblcbus);
44                    panelt2.add(cbrateus);
45                    panelt2.add(lbleuro);
46                    panelt2.add(txteuro);
47                    panelt2.add(lbleurous);
48                    panelt2.add(txteurous);
49                    panelt2.add(bteurous);
                       
                        //Create JTabbedPane object and add panelt1 and panelt2
50                    JTabbedPane tbpane=new JTabbedPane();
51                    tbpane.add(panelt1, null, 0);
52                    tbpane.add(panelt2, null, 1);
                        //Set title to each tab
53                    tbpane.setTitleAt(0,"USD and Riel");
54                    tbpane.setTitleAt(1,"EURO and USD");

                        //Add tbpane JTabbedPane object to the container
55                    cont.add(tbpane);
56                    setVisible(true);                   
                                   
                       
            }

57 public void actionPerformed(ActionEvent e){
                        double rate;
                        double usdamount;
                        double khamount;
                        double euroamount;
                       
                        if(e.getSource()==btuskh){
                                    if(!checkBlank(txtus,txtkh, cbratekh))
                                                if(txtkh.getText().equals("")){ //USD to Riel conversion
                                                            rate=Double.parseDouble(cbratekh.getSelectedItem().toString());
                                                            usdamount=Double.parseDouble(txtus.getText());
                                                            khamount=usdToKH(rate,usdamount);
                                                            txtkh.setText(""+khamount);
                                                            }

                                                else if(txtus.getText().equals("")) { //Riel to USD conversion
                                                rate=Double.parseDouble(cbratekh.getSelectedItem().toString());
                                                khamount=Double.parseDouble(txtkh.getText());
                                                usdamount=khToUSD(rate,khamount);
                                                txtus.setText(""+usdamount);
                                                }
                                                else{
                                                showMessage("Please leave the result text box blank");
                                    }
                       
                        }
                        else if(e.getSource()==bteurous){
                                    if(!checkBlank(txteurous,txteuro, cbrateus))
                                                if(txteurous.getText().equals("")){ //Euro to USD conversion
                                                            rate=Double.parseDouble(cbrateus.getSelectedItem().toString());
                                                            euroamount=Double.parseDouble(txteuro.getText());
                                                            usdamount=euroToUSD(rate,euroamount);
                                                            txteurous.setText(""+usdamount);
                                                }

                                                else if(txteuro.getText().equals("")) { // USD to EURO conversion
                                                            rate=Double.parseDouble(cbrateus.getSelectedItem().toString());
                                                            usdamount=Double.parseDouble(txteurous.getText());
                                                            euroamount=usdToEuro(rate,usdamount);
                                                            txteuro.setText(""+euroamount);
                                                }
                                                else{
                                                showMessage("Please leave the result text box blank");
                                    }
                                   
                        }
           
            }



Currency Conversion program in Java



Code Explanation:
1 Set the title of the program window (Currency Conversion Program).
2 Let the program window close when the user clicks the close button.
3 Specify the width and height of the program window when it opens.
4 Disable program window resizing.
5 Create a container object to act as the controls placeholder.
6-27 Construct the interface for USD and Riel conversion. We need three labels, one combobox, two text boxes, and one button. These controls are grouped together in the panelt1 object. The combobox allows the user to select conversion rate. The text boxes are for inputting USD or Riel  amount. Inputting in both text boxes in one operation is not valid. If you input USD, the conversion will be made from USD currency to Riel  currency and vise versa. The two text boxed are registered to the key event so that restriction can be placed on its key typed. The button object is registered to the action event to enable button-click action.
28-49 The construction of  the interface for EURO and USD conversion is done in the same way as the interface for USD and Riel  conversion except different names of controls.
50 Create a JTabbedPane object tbpane.
51 Place the panelt1 to the first tab.
52 Place the panelt2 tot the second tab.
53 Set title of the first tab to "USD and Riel " text.
54 Set title of the second tab to "EURO and USD" text.
55 Add the tbpane oject to the container.
56 Make user the program window is visible.
57 The actionPerformed method is rewritten to enable the button-click action. When the user clicks btuskh from the first tab, the conversion is made from USD amount to Riel  amount or vise versa. If you want to convert from USD to Riel , input the USA amount and leave the Riel  text box blank and vise versa.
If the user clicks the bteurous button from the second tab, the conversion is for EURO to USA currency. It works the same way as the previous button click except for different currencies conversion.
 58 The KeyList class extends the KeyAdapter class to rewrite the keyTyped method. This method defines code to restrict key typed in all text boxes. Only number, dot, backspace, and delete keys are accepted.
59 The checkBlank method is implemented to check whether the combobox conversion rate or the two text boxes are not blank before conversion can be made.
60 The usdToKH method is invoked to convert from USD amount to Riel  amount.
61 The khToUSD method is invoked to convert from Riel  amount to USD amount.
62 The euroToUSD method is invoked to convert from EURO amount to USD amount.
63 The usdToEURO method is invoked to convert from USD amount to EURO amount.
65 The showMessage method is invoked to show a message dialog to inform the user when an invalid action is being made.
66 The CurrencyConversion Program has the main method to start the program.

49 comments:

  1. Hello Sir/Maam....how can i add this code to your QuizProgram?
    if its possible.....
    please help me...thank you so much.

    ReplyDelete
  2. Superb article and valuable information because it’s really helpful for me, Thank you so much for share this wonderful information with us.
    Jeans Manufacturers in Delhi

    ReplyDelete
  3. Such a very superb article, Very interesting to read this article, I would like to thank you for useful had made for writing this awesome article.
    Top Commercial Vehicle Painters

    ReplyDelete
  4. Awesome article, Very interesting to read this article, I would like to thank you for useful had made for writing this superb article share with us.
    Web design company

    ReplyDelete
  5. Great article, Very interesting to read this article, I would like to thank you for writing this helpful article share with us.
    Motorcycle Tour in Mumbai

    ReplyDelete
  6. Good article, Very interesting to read this article, I would like to thank you for writing this helpful article share with us.
    Lifestyle Magazine

    ReplyDelete
  7. Thanks for this awesome article Very interesting to read this article, Thank you sharing with us.
    Shipping Company in India

    ReplyDelete
  8. That was such an awesome content to read and going through it.Thanks for such a good information.our product related for Servo Stabilizer Manufacturer in india and transformer.

    ReplyDelete
  9. Nice Blog, keep it up for more updates about this type of blog.Carolina Classics is the manufacturer of best F-100 Classic Ford Truck Parts| Buy Ford F100 truck parts online at Carolina Classics.
    F-100 Ford Truck Parts

    ReplyDelete
  10. Nice Blog, keep it up for more updates about this type of blog.
    get the best Russian Belly Dancer in Delhi

    ReplyDelete
  11. I visit your website www.quickacrepair.in. I am very much impressed by the blogs on your website. I recently purchased a split ac and your blog provides me great insight into AC Repair and Services .It also allows me to enrich my knowledge about various types of ac. I am a big fan of yours and find your website highly informative. Thank you for posting such unique and amazing blogs.

    ReplyDelete
  12. Excellent article. I actually think this site needs a lot more attention. I’ll probably be back again to read more, thanks for the advice!
    Ready To Repair
    Refrigerator repair
    Appliance Repair
    AC Repair and Service
    Gas Stove Repair
    Laptop Repair
    Washing Machin Repair

    ReplyDelete
  13. Satta King Guru Have the Leaked Number of the Gali and Deshwar, Which Has to Cover Your Loss, Call Immediately. the Game Will Pass With 101% Guarantee by Satta King Guru.
    SattaKing
    SattaKing
    SattaKing
    SattaKing

    ReplyDelete
  14. I read your post, it was really a very good topic and the content that is in it is even better. I thank you very much for sharing this post with us and I would like to read your post even further.

    Escort Girls Service
    Delhi Escort Service
    Chennai Call Girls
    Call Girls in Bangalore
    Mumbai Escort Service

    ReplyDelete
  15. FURNISHYA, a Home Furnishing brand, Furnishya have many traditional designs on bed, bath and kitchen. Our products are not only elegant, eclectic and eccentric, but are also thoughtfully crafted to make your experience of living a comfortable lifestyle, more comfortable.
    With thoughtful living at heart, we create products to give you an experience. And how do we do it? We do it with love, care and innovation to make, better.
    Home Kitchen

    ReplyDelete
  16. Best Deals at Paincarecircle, an international prescription service provider, which contracts with International dispensaries and USA pharmacies, is a leader in referring orders for prescription and non-prescription medications on behalf of customers throughout the world.
    buy oxycontin online
    buy valium online
    buy codeine online
    buy tramadol online
    buy ambien online
    buy xanax online
    buy soma online
    buy adipex online
    buy phentermine online
    buy dilaudid online

    ReplyDelete
  17. Best Deals at Paincarecircle, an international prescription service provider, which contracts with International dispensaries and USA pharmacies, is a leader in referring orders for prescription and non-prescription medications on behalf of customers throughout the world.
    buy suboxone online
    buy subutex online
    buy flexeril online
    buy lexapro online
    buy wellbutrin online
    buy gabapentin online
    buy ativan online
    buy modafinil online

    ReplyDelete
  18. Currency conversion program is very good for the businessmen. I think they must not skip this golden opportunity and get benefits from it for fulfilling their tasks. Dissertation Writing Service

    ReplyDelete

  19. easycareshop

    Xanax belongs to the benzodiazepines drug, which is using to address anxiety, panic disorder, and stress by stimulating the disturbed and unbalanced chemicals in the brain. Xanax offers calming effects in the brain to enhance the productivity level.Butor’s consultation and guidelines.Buy Xanax Online

    ReplyDelete
  20. Buy Hydrocodone Online with Paypal, Order hydrocodone overnight delivery 10/325 MG 100 PILLS. Buy hydrocodone online for pain relief with a credit card.
    Order Cheap Pills now -
    Buy Hydrocodone Online
    Buy Generic Viagra Online

    ReplyDelete
  21. Thanks for your marvelous posting! I really enjoyed reading it, you happen to be a great author. I will remember to bookmark your blog and may come back in the foreseeable future.

    Read More:-

    Satta king

    ReplyDelete

  22. easycareshop

    Xanax is the newest development, serving clients online with their pharmaceutical needs. Our retail outlets are supported by an efficient Headquarter team based in United States.

    ReplyDelete
  23. Nice Blog. Ogen Infosystem one of the best Website Designing Company in Delhi and also get digital marketing services like SEO, PPC, and SMO Services at best price for your business.
    Website Designing Company in Delhi

    ReplyDelete
  24. Buy Levitra Online with Paypal, Order levitra overnight fedex delivery available. Buy Levitra online for pain relief with a credit card. Click here to order :
    Buy Levitra Online
    Buy Viagra Online

    ReplyDelete
  25. easycareshop is the newest development, serving clients online with their pharmaceutical needs. Our retail outlets are supported by an efficient Headquarter team based in United States.
    easycareshop

    ReplyDelete
  26. easycareshop is the newest development, serving clients online with their pharmaceutical needs. Our retail outlets are supported by an efficient Headquarter team based in United States.
    buy vicodin online
    buy adderall online

    ReplyDelete
  27. bestcartshop is the newest development, serving clients online with their pharmaceutical needs. Our retail outlets are supported by an efficient Headquarter team based in United States.
    buy hydrocodone online
    buy percocet online

    ReplyDelete
  28. Medical Pharmacy USA

    The main concentration of our company is to work according to the global standard of the business. We are working in this field for more than 10 years with thousands of satisfied customers so you can rely on our products. With over 10 years -experience in online pharmacy, we provide our customers with genuine products at affordable prices. Our company shall be the leading company in the online distribution of pharmacy in partnership with many national and international companies.

    Buy Adderall Online
    Buy Soma Online
    Buy Xanax Online
    Buy Adderall XR Online
    Buy Hydrocodone Online

    ReplyDelete
  29. Online Medz Pharmacy is a website that provides various types of medications. We help the persons who are suffering from some severe diseases like sleep disorder, ADHD, Weight loss, pain relief and anxiety disorder by providing them Xanax, Vicodin, Tramadol, and other drugs. We have all types of Generics and brands of medications with several strengths.

    Buy Oxycontin Online
    Buy Percocet Online
    Buy Adderall Online
    Buy Xanax Online

    ReplyDelete
  30. The use of Hydrocodone is to suppress moderate to severe pain conditions that tend to occur in a human body. This pill is only taken by the people who expect the need of this drug for acute pain treatments.
    Buy Hydrocodone Online

    ReplyDelete
  31. Our site provides information about prescribed medications. This medication is taken for the treatment of various kinds of diseases.

    Buy Adderall Online
    Buy Hydrocodone Online
    Buy Methadone online
    Buy Vicodin online
    Buy Norco online
    Buy Tramadol online

    ReplyDelete
  32. virtual edge. There is the potential to do amazing things with LinkedIn’s unbeatable B2B dataset of 752 million members and Before 2015 LinkedIn allowed for much more open access to its API and resulting in some impressive data visualizations. business luncheon invitation and email invitations examples

    ReplyDelete
  33. Hero ngobrol games berbentuk panda ini dikenal dengan mobile legend-nya, baik pemain pemula maupun profesional. Build Akai Mobile Legends akai. Hal ini dikarenakan variabel seperti bentuk ekor panda dan ada yang mengetahui bentuknya karena muncul seperti karakter khusus dalam film Kungfu Panda. Kemudian yang kedua, tidak seperti banyak hero yang membutuhkan battle point untuk membelinya, rute untuk Akai cukup mudah, cukup dengan modal seratus tiket. Akai diceritakan di Mobile Legends sebagai panda dari kota timur. Dia terus-menerus bermimpi setiap malam menjadi seorang gerilyawan yang luar biasa. Namun, nyatanya, Akai hanyalah panda paling pemalu dan pemalu untuk ukuran panda. Banyak orang mengklaim itu hanya bergulir hampir sepanjang waktu. games orbit

    ReplyDelete
  34. If you are going for most excellent contents like me, simply go to see
    this site every day for the reason that it presents quality contents, thanks

    Please Visit My homepage ➤ 오피사이트
    (freaky)

    ReplyDelete
  35. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    Scan to BIM in Minnesota
    Dimension Control Services in Birmingham
    Plant Engineering Services in Bayern Germany
    Reverse Engineering Services in Bayern Germany

    ReplyDelete
  36. Soma (carisoprodol) is a muscle relaxer that blocks pain sensations between the nerves and the brain. Soma is used together with rest and physical therapy to treat skeletal muscle conditions such as pain or injury. Soma should only be used for short periods (up to two or three weeks) because there is no evidence of its effectiveness in long term use and most skeletal muscle injuries are generally of short duration.
    Check it out:- Buy Soma Online At Cheapest Rate

    Contact us:- https://pharmacyorderonline.com/contact-us/

    ReplyDelete
  37. We do best office interior in most economical with best designing and pricing. Office Interior in Delhi NCR
    Our vision is to design the cult, making innovative and artistic interiors for the office. Being the finest interior Designers in Delhi, we follow a systematic process and protocol. office Interiors

    ReplyDelete