Making A SteemBackedDollar Currency Converter On Java

in #utopian-io6 years ago (edited)

What Will I Learn?

Write here briefly the details of what the user is going to learn in a bullet list.

  • You will learn using many Java Commands
  • You will learn how to make a currency converter

Requirements

  • Eclipse and Java setup.
  • Knowlede Of Coding On Java Language
  • You must know how to use loops,define variables.

Difficulty

  • Intermediate

Tutorial Contents

Hello,i was trying to convert sbd to usd then i asked myself why not write a Java applet for that and use it everyday and this tutorial took me about 2,5 days to write but i am glad to share you my experience with you guys! Let's begin our program;

First of all i define my program without libraries,because i am going to use lots of Java commands.Then i use suppresswarnings method to block the error if needed and then i used "javax.swing.jfrarme" library with using "jframe" method we are getting allowed to use button,labels etc.Then i define my arrays for setting the current currencies of sbd,usd,pound,euro in "double" type.

package CurrencyConverting;

@SuppressWarnings("serial")
public class CurrencyConverter extends javax.swing.JFrame {
// Currencies {Sbd,Dollar,Pound,Euro} 
double Currency1[] = {0,1,5.7,5.10,5.24};
double Currency2[] = {0,1,5.7,5.10,5.24}; 

public CurrencyConverter() {
Components();
}              

1.png

Now in this step i will use javax.swing.label command which will allow us to create a form box.I will also use combobox command too to default a data model.I will equalize them all to variables that i am going to create below the Components class.Basically what i am doing over here is i am defining my buttons for the applets form.

private void Components() {
jLabel1 = new javax.swing.JLabel();
    Jc1 = new javax.swing.JComboBox<>();
    Jc2 = new javax.swing.JComboBox<>();
    jtxtAmount = new javax.swing.JTextField();
    jlblConversion = new javax.swing.JLabel();
    jbtnConvert = new javax.swing.JButton();
    jbtnReset = new javax.swing.JButton();
    jtxtConvert = new javax.swing.JTextField();
    jbtnExit = new javax.swing.JButton();

2.png

Now we will use "setDefaultCloseOperation" we use this method if the user wants to close the applet,this allows user to close the screen.Then use setfont and settext to set our programs name and font also the size.

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 20));
    jLabel1.setText("Drewie's Converter");

3.png

In this step i asked user to choose which currency they want to convert with using defaultcomboboxmodel method.Basically i created a menu for my user.Then i write on screen two more options that i defined at above which is "amount" and "conersion result".

Jc1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Choose One", "Steem Backed Dollars", "US Dollar", "Pound","Euro" }));

Jc2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Choose Other One", "Steem Backed Dollars", "US Dollar", "Pound","Euro" }));

jtxtAmount.setText("Amount");

jlblConversion.setText("Conversion Result");
       

4.png

In this part i am defining my buttons and quote them to screen on applet form.As you can see below i used function "actionlistener" to add reset and convert now buttons on the screen.Actionlistener is below to Java.awt package.We use this method to click the checkbox or button.

What we exactly do is here basically we use .settext to put my menus on the applet forms screen.You should be careful about what i did on "reset" menu.I used jbtnResetActionPerformed.This method is used if the user clicks reset button this method allows program to restart.

jbtnConvert.setText("CONVERT NOW");
jbtnConvert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent Y) {
jbtnConvertActionPerformed(Y);
 }
    });
jbtnReset.setText("RESET");
jbtnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtnResetActionPerformed(evt);
 }
});

5.png

Now i wil choose my font type with using java.awt.font then select my Fonts size.If you want to look up to font types on Java go to Oracle's website.

Oracle Website

After choosing my font type as "baghdad" i choose my foregrounds color with using .setforeground and java.awt.color and in the below line i print the menu using .settext then i create the exit menu with using "actionlistener" function.

jtxtConvert.setFont(new java.awt.Font("Baghdad", 1, 17)); 
jtxtConvert.setForeground(new java.awt.Color(0, 2, 208));
 jtxtConvert.setText("Converted Total Value");
  
jbtnExit.setText("Press Here To Exit");
jbtnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtnExitActionPerformed(evt);
}
});
       

6.png

Now this step is very important.We are creating the whole applet here actually.In the first step we use "grouplayout" this is actually a menu interface.We use the "contentpane" if the code is not the part of the frame.Then we use .setlayout method.CreateParallelGroup creates us a parallel group and returns it.

In the below lines i used "javax.swing.GroupLayout.Alignment.LEADING" and made it "false" this line means user cannot write anything on the lines that i don't want in my applet menu.What we do here is we nest methods and variables on these lines..addGap is used to make a space between the menu lines as you can see i choosed 15 in first then 155 on the other line.

We make make two settings for our menu one for vertical and one for horizontal groups.In the end of the code you will see i used "pack" method.This method helps my applet to fit my windows size whatever i choose on gaps.

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
 .addComponent(jLabel1)
 .addGroup(layout.createSequentialGroup()
.addComponent(jbtnConvert)
 .addGap(15, 15, 15)
 .addComponent(jbtnReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 .addComponent(Jc1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Jc2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jtxtAmount)
.addComponent(jlblConversion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   .addComponent(jtxtConvert))
  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 .addGroup(layout.createSequentialGroup()
 .addGap(155, 155, 155)
.addComponent(jbtnExit)
.addContainerGap(191, Short.MAX_VALUE))
    );
layout.setVerticalGroup(
   layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
 .addComponent(jLabel1)
.addGap(48, 48, 48)
.addComponent(Jc1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  .addGap(30, 30, 30)
 .addComponent(Jc2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(jtxtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addComponent(jlblConversion)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jtxtConvert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(jbtnConvert)
.addComponent(jbtnReset))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
addComponent(jbtnExit)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
 pack();
}                      

7.png

8.png

This can be seem very confusing if you are new on java coding but this is not hard as it seems.

Now in this step we create a new class for making the math problems.I defined a double and used parsedouble method to convert it to string to get a text from user.
Then i defined two more variables and equalize them to "Jc" which is the current currencies of the money types.

Using if loops to make sure that if the user chooses same currency we will warn them,if not we make the math down there with choosen variables,as you know we define these variables in double type because they are can be frictional.

private void jbtnConverting(java.awt.event.ActionEvent evt) {                                            

double amt = Double.parseDouble(jtxtAmount.getText());

int Var1 = Jc1.getSelectedIndex();
int Var2 = Jc2.getSelectedIndex();

if (Var1 == Var2){
String SameCurrency = "You cannot convert same currency!";
 jtxtConvert.setText(SameCurrency);
    }
else 
{
double res = (amt/Currency1[Var1])*Currency2[Var2];

String conv = String.format("%.2f",res);
jtxtConvert.setText(conv);
    }
}                                           

9.png

In the final steps we create a class for all of the options first for "reset" button,i use "null" if the user chooses to reset the program the values will reset to zero.If the user chooses to exit we use "system.exit(0)" to exit.The "zero" is for make the program return zero.In the finale step i use UI manager this manager finds out if there are any variable that haven't used that isn't notified.

Below that i used "Nimbus" if you are wondering what is it,it is a type of menus look.There are other options but nimbus is my favourite.

Finally; we user catch loop to make sure there is nothing wrong with the applet.For that we basically are evaluating every possibility,"setLookAndFeel" code in above is used exactly for this.

In the final step i create a new class for every variable to make UImanager check if there is anything wrong with the applet also to define them.

private void jbtnResetActionPerformed(java.awt.event.ActionEvent evt) {                                          

jtxtAmount.setText(null);
jtxtConvert.setText(null);
Jc1.setSelectedIndex(0);
Jc2.setSelectedIndex(0);
}                                         

private void jbtnExitActionPerformed(java.awt.event.ActionEvent evt) {                                         
System.exit(0);
}                                        

public static void main(String args[]) {
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(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CurrencyConverter().setVisible(true);
}
   });
}
                
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jbtnConvert;
private javax.swing.JButton jbtnExit;
private javax.swing.JButton jbtnReset;
private javax.swing.JComboBox<String> Jc1;
private javax.swing.JComboBox<String> Jc2;
private javax.swing.JLabel jlblConversion;
private javax.swing.JTextField jtxtAmount;
private javax.swing.JTextField jtxtConvert;           
}

11.png

Whole Program With The Screenshots

package CurrencyConverting;

@SuppressWarnings("serial")
public class CurrencyConverter extends javax.swing.JFrame {

// Currencies {Sbd,Dollar,Pound,Euro} 
double Currency1[] = {0,1,5.7,5.10,5.24};
double Currency2[] = {0,1,5.7,5.10,5.24}; 

public CurrencyConverter() {
Components();
}
                   
private void Components() {

jLabel1 = new javax.swing.JLabel();
Jc1 = new javax.swing.JComboBox<>();
Jc2 = new javax.swing.JComboBox<>();
jtxtAmount = new javax.swing.JTextField();
jlblConversion = new javax.swing.JLabel();
jbtnConvert = new javax.swing.JButton();
jbtnReset = new javax.swing.JButton();
jtxtConvert = new javax.swing.JTextField();
jbtnExit = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Baghdad", 1, 20));
jLabel1.setText("Drewie's Converter");

Jc1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Choose One", "Steem Backed Dollars", "US Dollar", "Pound","Euro" }));

Jc2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Choose Other One", "Steem Backed Dollars", "US Dollar", "Pound","Euro" }));

jtxtAmount.setText("Amount");

jlblConversion.setText("Conversion Result");

jbtnConvert.setText("CONVERT NOW");
jbtnConvert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent Y) {
jbtnConverting(Y);
 }
});
jbtnReset.setText("RESET");
jbtnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
  jbtnResetActionPerformed(evt);
 }
});
jtxtConvert.setFont(new java.awt.Font("Baghdad", 1, 17)); 
jtxtConvert.setForeground(new java.awt.Color(0, 2, 208));
jtxtConvert.setText("Converted Total Value");
    
  jbtnExit.setText("Press Here To Exit");
  jbtnExit.addActionListener(new java.awt.event.ActionListener() {
  public void actionPerformed(java.awt.event.ActionEvent evt) {
 jbtnExitActionPerformed(evt);
}
 });
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
 .addComponent(jbtnConvert)
.addGap(15, 15, 15)
.addComponent(jbtnReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(Jc1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Jc2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jtxtAmount)
.addComponent(jlblConversion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jtxtConvert))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(155, 155, 155)
.addComponent(jbtnExit)
.addContainerGap(191, Short.MAX_VALUE))
 );
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel1)
.addGap(48, 48, 48)
.addComponent(Jc1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(Jc2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(jtxtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addComponent(jlblConversion)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jtxtConvert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtnConvert)
.addComponent(jbtnReset))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jbtnExit)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}                      

private void jbtnConverting(java.awt.event.ActionEvent evt) {                                            

double amt = Double.parseDouble(jtxtAmount.getText());

int Var1 = Jc1.getSelectedIndex();
int Var2 = Jc2.getSelectedIndex();

    if (Var1 == Var2){
String SameCurrency = "You cannot convert same currency!";
jtxtConvert.setText(SameCurrency);
    }
 else {
double res = (amt/Currency1[Var1])*Currency2[Var2];

String conv = String.format("%.2f",res);
jtxtConvert.setText(conv);
}
}                                           
private void jbtnResetActionPerformed(java.awt.event.ActionEvent evt) {                                          

jtxtAmount.setText(null);
jtxtConvert.setText(null);
Jc1.setSelectedIndex(0);
Jc2.setSelectedIndex(0);
}                                         

private void jbtnExitActionPerformed(java.awt.event.ActionEvent evt) {                                         
System.exit(0);
}                                        

public static void main(String args[]) {
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(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
 java.util.logging.Logger.getLogger(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CurrencyConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CurrencyConverter().setVisible(true);
}
    });
}
                 
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jbtnConvert;
private javax.swing.JButton jbtnExit;
private javax.swing.JButton jbtnReset;
private javax.swing.JComboBox<String> Jc1;
private javax.swing.JComboBox<String> Jc2;
private javax.swing.JLabel jlblConversion;
private javax.swing.JTextField jtxtAmount;
private javax.swing.JTextField jtxtConvert;           
}       

a.png

bbb.png

4.png

5.png

6.png

7.png

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you very much Creon.

Hey @creon, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

@drewie, Upvote is the only thing I can support you.

Thanks for your contribution @drewie, you got a 20.80% upvote from @canalcrypto!
70% of your contribution goes to our delegators so if you'd like to help us push posts even harder while earning some extra steems please consider delegating to @canalcrypto. You can delegate your SP here: https://steembottracker.com/delegation.html (Delegator = your username, delegatee = canalcrypto), thanks in advance for your support.

Release the Kraken! You got a 0.97% upvote from @seakraken courtesy of @drewie!

You got a 4.36% upvote from @whalebuilder courtesy of @drewie. Join @whalebuilder family at our Discord Channel. Don't let your precious stake(SP) go stale...Make it do more so you have to do less. Deligate it to @whalebuilder by clicking on one of the ready to delegate links: 50SP | 100SP | 250SP | 500SP | 1000SP | 5000SP | custom amount.

You just rose by 9.8039% upvote from @therising courtesy of @drewie. Earn 43.8% APR & rise further by delegating SP to therising. For more details visit: https://steemit.com/budget/@therising/auto-daily-payout-of-43-8-apr-for-steem-power-delegations-starting-from-500-sp-only-limited-period-offer.

This post has received a 5.33 % upvote from @moneymatchgaming thanks to: @drewie. Upvote this Post to Support the MMG Community on Steemit! :)

Hey @drewie I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x