[Java Tutorial #4] - How to create Calculator java aplication to convert STEEM to IDR 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 how to get data from Bittrex API
  • You will learn how to get data from Indodax API
  • You will learn how to get Price from both API
  • You will learn how to display data on GUI aplication

Requirements

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

Difficulty

  • Basic

Tutorial Contents

on this occasion I will try to explain a tutorial how to create java application to check steem price in IDR (indonesian currency). To create this application we have to get data from Bittrex API to change BTC to IDR. Then we should get the BTC price in IDR from INDODAX API (an Exchanger in Indonesia).



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 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 get Data from url and count the price. Here I rename with count() method.
public void count() throws Exception{
}
  • Call get Data method with parameter is the API URL. to convert STEEM to BTC we can use Bittrex API and to convert BTC to IDR we can use Indodax API.
 JSONObject bittrex = new JSONObject(getApi("https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-sbd"));
 JSONObject indodax = new JSONObject(getApi("https://indodax.com/api/btc_idr/ticker"));
       JSONArray arrayBittrex=bittrex.getJSONArray("result");
        JSONObject objBittrex = arrayBittrex.getJSONObject(0);
        JSONObject objIndodax=indodax.getJSONObject("ticker");
        Double price =objBittrex.getDouble("Last")*objIndodax.getDouble("last");


CodeExplanation
bittrex.getJSONArray("result")Get Array Value from result index of bittrex API
arrayBittrex.getJSONObject(0Convert Array to Object
indodax.getJSONObject("ticker");Get Object Value from ticker index of bittrex API
objBittrex.getDouble("Last")get Last price of steem from bittrex in double
objIndodax.getDouble("last");get last price of BTC from indodax in double
Double pricethe steem price in IDR is the Steem price in BTC x BTC price in IDR
  • we already get STEEM price in IDR. SO, Now we need get the Amount STEEM that input by user.
double Amount = Double.parseDouble(steem.getText());


CodeExplanation
Double.parseDouble(steem.getText())All data from textfield input is in string. To be an multiply with the price we need convert it to double.
  • Now display the result in IDR textfield.
 idr.setText("IDR  "+Amount*price);
  • Call the count() method in button. Open the aplication design form and double click on button. then add this code.
        try {
            count();
            // TODO add your handling code here:
        } catch (Exception ex) {
            Logger.getLogger(Calculator.class.getName()).log(Level.SEVERE, null, ex);
        }
  • Now try to run the aplication, and input some amout in STEEM field the press 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

Proof of Work Done

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

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.
  • Structure of the tutorial: Improve the structure of the tutorial. Example: link
  • 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]

Thank's a lot for reviewing and best advice. I appreciate it and I will do my best in my next contribution.

Best Regard

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.33
TRX 0.11
JST 0.035
BTC 67020.94
ETH 3270.13
USDT 1.00
SBD 4.62