[Java Tutorial #7] - How to create Converter Calculator Aplication for STEEM and SBD using Netbeans

in #utopian-io6 years ago

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

  • You will learn How to create GUI form for this aplication
  • You will learn API to get Concerter Data of SBD and STEEM
  • You will learn how to get data from user input and multiply it with the price from API URL
  • You will learn how to display last data to the form

Requirements

  • Basic knowledges about java programing
  • Basic Knowledges about API
  • Need to Install Netbeans for easy pratice

Difficulty

  • Basic

Tutorial Contents

in this tutorial, i will try to explain how to make a calculator app to convert steem and sbd. In the application we will make this, the user can convert from steem to sbd and sbd to steem. You can see the image bellow :



Let's start the tutorial :

  • Open Netbeans IDE software and create new project. Choose Java Aplication and click next. Then Rename the project and click finish.

  • Add new jFrameForm in your Source Package

  • Rename the form

  • Click on panel component and drag it to the form

  • Add Label, TextField and Button on Panel, just follow the components on picture bellow :

  • We already have a form, Now open source and start to edit the code.

  • Create The method to get Data from URL. This method in java.net.* method. So before your start use this method import the package first

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
  • Then Create getApi() method.
public String getApi(String url) throws Exception{
        URL oracle=new URL(url);
        HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
        BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
        String inputLine;
         while ((inputLine = in.readLine()) != null){
             return inputLine;
         }  
        in.close();
        return null;   
    }


CodeExplanation
getApi()Method Name
String urlThe Parameter
throws ExceptionSome method to catch error
URL oracle=new URL(url);Decrlarating new URL
HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();class initiation to get URL connection.
httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");Add browser property, Here I add Mozilla 5.-0
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));method Read input from http connection.
return inputLine;return the output
in.close();closie input stream reader



  • Now create the method to convert SBD to STEEM
 public void sbdToSteem() throws Exception{ 
       String Data = getApi("https://uploadbeta.com/api/steemit/converter/?cached");
       JSONObject ObjData=new JSONObject(Data);
       steemResult.setText(""+Double.parseDouble(sbdInput.getText())*ObjData.getDouble("sbd_to_steem"));
    }


CodeExplanation
sbdToSteem()Method Name
getApiwe call the previous method to get Data from URL. and the parameter is URL from https://helloacm.com/tools/steemit/
JSONObject ObjData=new JSONObject(Data);convert the result from string to JSON Object. Here we need use java-json library. Dowload it here : http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm . Extract and add to project Libraries. Then import it to your file, then import org.json.JSONObject;
steemResult.setTextsetText method for steemResult Component
Double.parseDouble(sbdInput.getText())get double value from user input in sbdInput component
ObjData.getDouble("sbd_to_steem")sbd in steem price based the data from URL API. ObjData is the Object Name, getDouble some method to get Double value from some element, And ("sbd_to_steem") the element name in object
*to multiply amount of user input and price
""+The Variable that will be set as text in componen, because the variabel type is double we need combine with a string. So I use ""+.
  • Call the sbdToSteem() method in steem to sbd convert button. Open the aplication design form and double click on button. then add this code.
        try {
            sbdToSteem();
        } catch (Exception ex) {
            Logger.getLogger(converter.class.getName()).log(Level.SEVERE, null, ex);
        }
  • Now try to run the aplication, and input some amount of SBD then press convert button in SBD to STEEM part

  • Succesfull . Now create steemToSbd() method to convert steem to sbd

 public void steemToSbd() throws Exception{ 
       String Data = getApi("https://uploadbeta.com/api/steemit/converter/?cached");
       JSONObject ObjData=new JSONObject(Data);
       sbdResult.setText(""+Double.parseDouble(steemInput.getText())*ObjData.getDouble("steem_to_sbd"));
    }


CodeExplanation
sbdResultComponent name of the textfield that we will display the result.
"steem_to_sbd"the component value of Data from URL api that explore the price of steem in sbd
steemInput.getText()get Text from user input and convert it to double
  • Now call the steemToSbd() method in convert button on STEEM to SBD form part. just double click on button and add this code
        try {
            steemToSbd();
        } catch (Exception ex) {
            Logger.getLogger(converter.class.getName()).log(Level.SEVERE, null, ex);
        }
  • and now try to run the app and input the value of steem then press the convert button.
  • Finish, the aplication is running rightly. You can get the full code on my gist. The link is bellow

Curriculum

Proof of Work Done

https://gist.github.com/team2dev/fa52954601a372eb6a8f6f1ebe8f2fe2

Sort:  

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Tutorial content: There are parts of the code that have little explanation, try to explain as much as possible.
  • Resources: Put more extra resources.
  • Screenshots: Provide better quality, clearer, and more professional screenshots
  • Structure of the tutorial: Improve the structure of the tutorial.
  • Avoid repetition: Frequent use of words or phrases makes reading the tutorial more annoying.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Congratulations @team2dev! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard!


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Hey @team2dev
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.032
BTC 60870.66
ETH 2917.09
USDT 1.00
SBD 3.62