[Java Tutorial #7] - How to create Converter Calculator Aplication for STEEM and SBD using Netbeans
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;
}
Code | Explanation |
---|---|
getApi() | Method Name |
String url | The Parameter |
throws Exception | Some 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"));
}
Code | Explanation |
---|---|
sbdToSteem() | Method Name |
getApi | we 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.setText | setText 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"));
}
Code | Explanation |
---|---|
sbdResult | Component 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
- [Java Tutorial] - How to create some java aplication to display Live SBD price from bittrex API using Netbeans
- [Java Tutorial #2] - How to create GUI aplication for displaying Live SBD price using Netbeans
- [Java Tutorial #3] - How to create GUI java aplication for displaying Steemit account data using Netbeans
- [Java Tutorial #4] - How to create Calculator java aplication to convert STEEM to IDR using Netbeans
- [Java Tutorial #5] - How to create GUI java aplication for checking The Estimated Reward in Last Week using Netbeans
- [Java Tutorial #6] - How to create GUI java aplication for checking STEEM POWER DETAIL using Netbeans
Proof of Work Done
https://gist.github.com/team2dev/fa52954601a372eb6a8f6f1ebe8f2fe2
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:
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
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!