Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, December 3, 2014

Show Resultset as HTML table

There are times you simply want to show some result set as a table on the screen.
You do not need anything fancy, just show some tabular data on the screen.

In order to solve this issue I wrote a very small and lightweight Java class named: ResultSetToHtmlTable. Even through this class is doing a quite simple task it still have some nice qualities:

  • It is very fast. It doesn't construct any string. It simply writes the table directly to a writer.
  • It doesn't use any external libraries. All plain and simple Java.
  • It is only a single class.
  • It knows to format the data in each column. It supports the following formats:
    • Integer - Show 1000 as 1,000
    • Float - Show 0.5145235 as 0.51
    • Currency - Show 5.2 as $5.2
    • Percentage - Show 0.5 as 50%
    • Date - Show 12/5/2014 as 5 Dec 2014
    • Date and time - Show 12/5/2014 12:00:00 as 5 Dec 2014 12:00:00
    • Time - Show only the time part of a date. For example: 13:22:15
  • It allows to align the data to the left or right and add titles to columns.
  • If no strict column information is supplied is knows to auto detect:
    • Column types.
    • Column titles - titles are named as column names returned in query.
    • Column alignment - Numbers are aligned to the right.
The code of this class is quite simple and straightforward. Here is how it looks like:
package com.todacell.util.sql;

import java.io.IOException;
import java.io.Writer;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Bashan
 *         Date: 18/11/2014 15:07
 */
public class ResultSetToHtmlTable {

  public void writeTable(ResultSet rs, Writer writer, Column[] columns) throws IOException, SQLException {
    writer.write("<table>");
    // Write titles
    writer.write("\t<tr>\n");
    for (Column column : columns) {
      writer.write("\t\t<th>" + column.title + "</th>");
    }
    writer.write("\t</tr>\n");

    // Write data
    int cols = columns.length;
    while (rs.next()) {
      writer.write("\t<tr>\n");
      for (int i = 0; i < cols; i++) {
        ColumnType columnType = columns[i].columnType;
        String columnAlign = columnAlignToColumnCss.get(columns[i].columnAlign);
        writer.write("\t\t<td" + columnAlign + ">" + format(columnType, rs.getObject(i + 1)) + "</td>");
      }
      writer.write("\t</tr>");
    }

    writer.write("</table>");
  }

  private static Map<Integer, ColumnType> sqlTypeToColumnType = new HashMap<Integer, ColumnType>();
  private static Map<ColumnType, ColumnAlign> columnTypeTpColumnAlign = new HashMap<ColumnType, ColumnAlign>();
  private static Map<ColumnAlign, String> columnAlignToColumnCss = new HashMap<ColumnAlign, String>();

  static {
    // Map SQL types to column types

    // integer
    sqlTypeToColumnType.put(Types.INTEGER, ColumnType.INTEGER);
    sqlTypeToColumnType.put(Types.BIGINT, ColumnType.INTEGER);
    sqlTypeToColumnType.put(Types.SMALLINT, ColumnType.INTEGER);
    sqlTypeToColumnType.put(Types.TINYINT, ColumnType.INTEGER);
    sqlTypeToColumnType.put(Types.NUMERIC, ColumnType.INTEGER);

    // float
    sqlTypeToColumnType.put(Types.FLOAT, ColumnType.INTEGER);
    sqlTypeToColumnType.put(Types.DOUBLE, ColumnType.INTEGER);
    sqlTypeToColumnType.put(Types.DECIMAL, ColumnType.INTEGER);

    // date
    sqlTypeToColumnType.put(Types.TIME, ColumnType.DATE);
    sqlTypeToColumnType.put(Types.TIMESTAMP, ColumnType.DATE);

    // Map column type to column alignment
    columnTypeTpColumnAlign.put(ColumnType.INTEGER, ColumnAlign.RIGHT);
    columnTypeTpColumnAlign.put(ColumnType.FLOAT, ColumnAlign.RIGHT);
    columnTypeTpColumnAlign.put(ColumnType.CURRENCY, ColumnAlign.RIGHT);
    columnTypeTpColumnAlign.put(ColumnType.DATE, ColumnAlign.LEFT);
    columnTypeTpColumnAlign.put(ColumnType.STRING, ColumnAlign.LEFT);

    // Map column alignment to css style
    columnAlignToColumnCss.put(ColumnAlign.LEFT, "");
    columnAlignToColumnCss.put(ColumnAlign.RIGHT, " style=\"text-align:right\"");
    columnAlignToColumnCss.put(ColumnAlign.CENTER, " style=\"text-align:center\"");
  }

  public void writeTable(ResultSet rs, Writer writer) throws IOException, SQLException {
    ResultSetMetaData meta = rs.getMetaData();
    int cols = meta.getColumnCount();

    Column[] columns = new Column[meta.getColumnCount()];
    for (int i = 0; i < cols; i++) {
      ColumnType columnType = sqlTypeToColumnType.get(meta.getColumnType(i + 1));
      ColumnAlign columnAlign = columnTypeTpColumnAlign.get(columnType);
      columns[i] = new Column(
          columnType != null ? columnType : ColumnType.STRING,
          meta.getColumnName(i + 1),
          columnAlign != null ? columnAlign : ColumnAlign.LEFT);
    }

    writeTable(rs, writer, columns);
  }

  public enum ColumnType {
    STRING, INTEGER, FLOAT, DATE, DATE_TIME, TIME, CURRENCY, PERCENTAGE
  }

  public enum ColumnAlign {
    LEFT, RIGHT, CENTER
  }

  private static NumberFormat integerFormat = NumberFormat.getInstance();
  private static NumberFormat floatFormat = NumberFormat.getInstance();
  private static final NumberFormat percentFormat = NumberFormat.getPercentInstance();
  private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
  private static final DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
  private static final DateFormat dateTimeFormat = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
  private static final DateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");

  static {
    floatFormat.setMaximumFractionDigits(2);
    integerFormat.setMaximumFractionDigits(0);
  }

  public String format(ColumnType columnType, Object value) {
    String result;
    if (value != null) {
      if (columnType == ColumnType.STRING) {
        result = value.toString();
      } else {
        Format format;
        if (columnType == ColumnType.FLOAT) {
          format = floatFormat;
        } else if (columnType == ColumnType.CURRENCY) {
          format = currencyFormat;
        } else if (columnType == ColumnType.PERCENTAGE) {
          format = percentFormat;
        } else if (columnType == ColumnType.DATE) {
          format = dateFormat;
        } else if (columnType == ColumnType.DATE_TIME) {
          format = dateTimeFormat;
        } else if (columnType == ColumnType.TIME) {
          format = timeFormat;
        }else {
          format = integerFormat;
        }

        result = format.format(value);
      }
    } else {
      result = "N/A";
    }

    return result;
  }

  public static class Column {
    ColumnType columnType;
    String title;
    ColumnAlign columnAlign;

    public Column(ColumnType columnType, String title, ColumnAlign columnAlign) {
      this.columnType = columnType;
      this.title = title;
      this.columnAlign = columnAlign;
    }
  }
}

And here is a simple example of how to use this code in a JSP file:
 <%@ page import="com.todacell.management.SessionSingleton" %>  
 <%@ page import="com.todacell.util.sql.ResultSetToHtmlTable" %>  
 <%@ page import="org.hibernate.Session" %>  
 <%@ page import="java.sql.Connection" %>  
 <%@ page import="java.sql.ResultSet" %>  
 <%@ page import="java.sql.Statement" %>  
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
 <html>  
 <head>  
   <title>Daily Deposits</title>  
   <style type="text/css">  
     table {  
       border-collapse: collapse;  
     }  
     table, th, td {  
       border: 1px solid black;  
     }  
     td, th {  
       padding: 5px;  
     }  
   </style>  
 </head>  
 <body>  
 <jsp:include page="k2_menu.jsp" />  
 <h1>Daily Deposits</h1>  
 <%  
   Session hSession = SessionSingleton.instance();  
   Connection conn = hSession.connection();  
   try {  
     Statement stmt = conn.createStatement();  
     ResultSet rs = stmt.executeQuery("select * from k2_deposits(60)");  
     ResultSetToHtmlTable resultSetToHtmlTable = new com.todacell.util.sql.ResultSetToHtmlTable();  
     resultSetToHtmlTable.writeTable(rs, out,  
         new com.todacell.util.sql.ResultSetToHtmlTable.Column[] {  
             new ResultSetToHtmlTable.Column(ResultSetToHtmlTable.ColumnType.DATE, "Date", ResultSetToHtmlTable.ColumnAlign.LEFT),  
             new ResultSetToHtmlTable.Column(ResultSetToHtmlTable.ColumnType.CURRENCY, "Deposits", ResultSetToHtmlTable.ColumnAlign.RIGHT),  
             new ResultSetToHtmlTable.Column(ResultSetToHtmlTable.ColumnType.CURRENCY, "Allocated", ResultSetToHtmlTable.ColumnAlign.RIGHT),  
             new ResultSetToHtmlTable.Column(ResultSetToHtmlTable.ColumnType.CURRENCY, "Deallocated", ResultSetToHtmlTable.ColumnAlign.RIGHT),  
             new ResultSetToHtmlTable.Column(ResultSetToHtmlTable.ColumnType.CURRENCY, "Ad Spend", ResultSetToHtmlTable.ColumnAlign.RIGHT),  
             new ResultSetToHtmlTable.Column(ResultSetToHtmlTable.ColumnType.CURRENCY, "Potential Spend", ResultSetToHtmlTable.ColumnAlign.RIGHT),  
             new ResultSetToHtmlTable.Column(ResultSetToHtmlTable.ColumnType.INTEGER, "New Users", ResultSetToHtmlTable.ColumnAlign.RIGHT)  
         });  
   } finally {  
     if (hSession != null) {  
       hSession.close();  
     }  
   }  
 %>  
 </body>  
 </html>  
And here is how it looks on the browser:
DOWNLOAD: You can download the ResultSetToHtmlTable here

Sunday, October 10, 2010

Java Currency Converter using Yahoo Finance API – Currency Matrix – Visual Presentation

On this post we constructed a class that can get currency information from Yahoo Finance API. We saw how we can easily construct a neat currency matrix that summarizes currency information for several currencies.
Let’s see how we can visualize the currency matrix using a Java JSP and AJAX. The currency matrix will have the following properties:
  • The currency matrix will be show as an HTML table.
  • Currency information will be refreshed every 2 seconds.
  • If currency rate is increased the updated value will be marked in greed, if currency rate is decreased the updated value will be marked in red.
  • We will some popular currencies on our currency matrix. These currencies can be easily changed to have more/less/other currencies.
Since we would like to refresh currency information every 2 seconds (or any other fixed interval), and we don’t want the whole screen to be refreshed, only our currency matrix, we will need to use AJAX. We will use JSON in order to pass currency information from server to the client. JSON is a textual way of representing data.
The file that generates the JSON currency data is called: “currency_json.jsp”. It does the following things:
Let’s see how the file “currency_json.jsp” looks like:
<%
  YahooCurrencyConverter ycc = new YahooCurrencyConverter();
  String[] currencies = new String[]{"USD", "EUR", "GBP", "JPY", "CHF", "CAD", "AUD", "MXN", "ILS"};
  CurrencyPair[][] currencyPairs = ycc.getConversionMatrix(currencies);
  int size = currencyPairs.length;
  JSONArray currencyArray = new JSONArray();
  for (int i = 0; i < size; i++) {
    JSONArray currencyRow = new JSONArray();
    for (int j = 0; j < size; j++)
    {
      CurrencyPair currencyPair = currencyPairs[i][j];
      currencyRow.add(currencyPair != null ? currencyPair.getPrice() : null);
    }
    currencyArray.add(currencyRow);
  }
  out.print(currencyArray.toJSONString());
%>
<%@ page import="com.bashan.currency.currency.CurrencyPair" %>
<%@ page import="com.bashan.currency.currency.YahooCurrencyConverter" %>
<%@ page import="org.json.simple.JSONArray" %>
<%@ page contentType="application/json;charset=UTF-8" language="java" %>
Note, that this code converts the data to JSON using the open source project named: json simple.

Let’s move the the main file: “currency.jsp”. This file does the following things:
  • Make an AJAX request to the “currency_json.jsp” file in order to get the latest currency matrix information.
  • When AJAX response is completed, get the JSON data received from server and build the currency matrix table. During the process of constructing the currency matrix, compare the currency data to the previous data. If there was a change in the currency, paint the table cell in red or green depending if currency raised or dropped.
Note, that in order to make the JSON AJAX request, we use MooTools JavaScript library. MooTools is a great and concat JavaScript framework, quite simplar to jQuery.
Let’s look at the “currency.jsp” code:
<%@ page import="org.json.simple.JSONArray" %>
<%!
 
  private static String[] currencies = new String[]{"USD", "EUR", "GBP", "JPY", "CHF", "CAD", "AUD", "MXN", "ILS"};
  private static JSONArray currencyArray = new JSONArray();
 
  static {
    for (int i = 0; i < currencies.length; i++) {
      currencyArray.add(currencies[i]);
    }
  } 
 
%>
<html>
<head>
<title>Currency Exchange Rates</title>
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
<h1>Currency Exchange Rates</h1>
<table id="tableCurrency" class="tableBorder" cellspacing="0" cellpadding="5">
<thead>
<tr>
<td class="tdBorder" style="font-weight:bold; text-align:center;">Currencies</td>
<%
  for (String currency : currencies) {
%>
<td style="font-weight:bold; text-align:center;" class="tdBorder"><%= currency %></td>
<%
  }
%>
</tr>
</thead>
<tbody id="tableBodyCurrency">
<tr>
<td class="tdBorder" colspan="<%= currencies.length + 1 %>">
Loading currencies data...
</td>
</tr>
</tbody>
</table>
<script type="text/javascript" src="js/mootools.js"></script>
<body>
<script type="text/javascript">
 
  var currencyCodes = <%= currencyArray.toJSONString() %>;
  var oldCurrencies = null;
 
  function getJSON()
  {
    var jsonRequest = new Request.JSON({url: "currency_json.jsp",
    onComplete: function(currencies) {
    if (typeof(currencies) == "undefined")
    {
      return;
    } 
 
    var table = $("tableBodyCurrency");
    var data = "";
    for (var i = 0; i < currencies.length; i++)
    {
      data = data + "<tr><td style='font-weight:bold; text-align:center;' class='tdBorder'>" + currencyCodes[i] + "</td>";
      var currencyRow = currencies[i];
      for (var j = 0; j < currencyRow.length; j++)
      {
        var currency = currencyRow[j];
        if (oldCurrencies == null || currency == null)
        {
          data = data + "<td class='tdBorder' style='text-align:center;'>" + (currency != null ? currency : "&nbsp;") + "</td>";
        }
        else
        {
          var oldCurrency = oldCurrencies[i][j];
          if (currency > oldCurrency)
          {
            data = data + "<td style='background-color:green; text-align:center;n' class='tdBorder'>" + currency + "</td>";
          }
          else if (currency < oldCurrency)
          {
            data = data + "<td style='background-color:red; text-align:center;' class='tdBorder'>" + currency + "</td>";
          }
          else
          {
            data = data + "<td class='tdBorder' style='text-align:center;'>" + currency + "</td>";
          }
        }
      }
      
      data = data + "</tr>";
    }
   
    table.set('html', data);
    oldCurrencies = currencies;
  }}).get();
}
 
window.addEvent('domready', function() {
getJSON();
getJSON.periodical(2000);
 
});
</script>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Also note, that the we use a style shit file named: “css.css” to make the table a bit nicer.
Now, let’s have a look how the currency matrix looks like when the page is first loaded:

And after we receive currency information, we can see the full matrix. Some of the cells are painted in green or red, indicating if currency increased or decreased:



You can see the currency matrix in action by simply dropping the currency.war file on Tomcat webapps folder. The war file also contains the java source files.

Thursday, October 7, 2010

Show Java Quartz Scheduler information

Quartz is a great open source project that allows to do Job Scheduling. Quartz is a production stage open source project that has been around long enough to be trusted. You can integrate it in your applications in a very short time and benefit from simple to complex scheduling requirements.
After you integrate Quartz to your project, you don’t really know what is going on. Which job is currently running/not running, when was the last time a job fired, when will the job will fire again.
In order to know the state of your jobs, Quartz provides several informative methods. We will use these methods in order to visually show Quartz scheduler information. The information will be shown using a JSP page that will dynamically gather Quartz information and build a table of all the current existing jobs.
This is an example of how this page looks like:
 
The Quartz data that is shown in the table is:
  • Group – The group of the job (in Quartz every job belongs to a group).
  • Job – The name of the job.
  • Cron Expression – An expression that describes the times in which the job should run. Note, that not all Quartz jobs use cron expressions for their execution. It is also possible to schedule jobs to run according specific intervals.
  • Previous fire time – The last time in which job was executed.
  • Next fire time – The next time a job is scheduled to run.
  • Stateful – An indication if a job is Stateful. When a job is Stateful, no more than a single instance of that job will run at any given time.
  • Interruptable – Indication if the job can be interrupted. A job that can be interrupted should implement InterruptableJob interface.
  • Running – An indication if a job is currently running or not. In parenthesis will be shown the number of instances of the job that are currently running.
  • Actions – This column allows to do 2 actions with Jobs:
    • Exec – Allows to immediately execute a job without waiting for it to be fired.
    • Pause/Resume – Pause/Resume a job. This will prevent the job from being executed by the scheduler.
In addition to this data, note that a running job is marked as bold and a paused job is marked in red bold.
Let’s have a look at the JSP file that creates this table. It mainly contains 2 parts:
  • The first part is a Java code that handles the 2 actions: Exec and Pause/Resume. It is responsible of executing a job if the Exec action was pressed or Pausing/Resuming a job if a job was paused/resumed.
  • The second part, is the part that actually gets all the jobs information from Quartz and shows it as a table.
The JSP file that creates this table, gets an instance of Quartz Scheduler class from a singleton named: SchedulerSingleton. You can replace this line of code with whatever way you use to get access to your Scheduler instance.
And here is how the code looks like:
<%@ page import="com.todacell.management.SchedulerSingleton" %>
<%@ page import="org.quartz.*" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
try
{
  Scheduler scheduler = SchedulerSingleton.instance();
  String action = request.getParameter("action");
  if (action != null)
  {
    String groupParam = request.getParameter("group");
    String jobParam = request.getParameter("job");
    if (groupParam != null && jobParam != null) 
    {
      if ("exec".equals(action))
      {
        scheduler.triggerJob(jobParam, groupParam);
      }
      else if ("pause".equals(action))
      {
        if (scheduler.getTriggerState(jobParam, groupParam) == Trigger.STATE_PAUSED)
        {
          scheduler.resumeTrigger(jobParam, groupParam);
        }
        else
        {
          scheduler.pauseTrigger(jobParam, groupParam);
        }
      }
      response.sendRedirect("jobs.jsp");
      return;
    }
  }
%>
<head>
<title>Management Jobs</title>
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body>
<h1>Jobs Information</h1>
Current Time: <b><%= new Date() %></b><br/>
Scheduler State: <b><%= scheduler.isStarted() ? "Running" : "Not Running" %></b><br/><br/>
<table class="tableBorder" cellpadding="4" cellspacing="0">
<tr>
<td class="tdBorder bold center">
Group
</td>
<td class="tdBorder bold center">
Job
</td>
<td class="tdBorder bold center">
Cron Expression
</td>
<td class="tdBorder bold center">
Previous Fire Time
</td>
<td class="tdBorder bold center">
Next Fire Time
</td>
<td class="tdBorder bold center">
Stateful
</td>
<td class="tdBorder bold center">
Interruptable
</td>
<td class="tdBorder bold center">
Running
</td>
<td class="tdBorder bold center">
Actions
</td>
</tr>
<%
  String[] groups = scheduler.getJobGroupNames();
  Arrays.sort(groups);
  List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
  for (String group : groups)
  {
    String[] jobs = scheduler.getJobNames(group);
    Arrays.sort(jobs);
    int i = 0;
    for (String job : jobs)
    {
      Trigger trigger = scheduler.getTrigger(job, group);
      JobDetail jobDetail = scheduler.getJobDetail(job, group);
      int numInstances = 0;
      for (JobExecutionContext jobExecutionContext : executingJobs)
      {
        JobDetail execJobDetail = jobExecutionContext.getJobDetail();
        if (execJobDetail.getKey().equals(jobDetail.getKey()))
        {
          numInstances++;
        }
      }
      boolean isPaused = scheduler.getTriggerState(job, group) == Trigger.STATE_PAUSED;
%>
<tr>
<%
if (i == 0)
{
%>
<td class="tdBorder" rowspan="<%= jobs.length %>">
<%= group %>
</td>
<%
}
%>
<td class="tdBorder <%= isPaused ? "error bold" : "" %>">
<%= job %>
</td>
<td class="tdBorder">
<%= trigger != null && trigger instanceof CronTrigger ? ((CronTrigger)trigger).getCronExpression() : "N/A" %>
</td>
<td class="tdBorder">
<%= trigger != null ? (trigger.getPreviousFireTime() != null ? trigger.getPreviousFireTime()  : "N/A") : "N/A" %>
</td>
<td class="tdBorder">
<%= trigger != null ? trigger.getNextFireTime() : "&nbsp;" %>
</td>
<td class="tdBorder center">
<%= StatefulJob.class.isAssignableFrom(jobDetail.getJobClass()) ? "YES" : "NO" %>
</td>
<td class="tdBorder center">
<%= InterruptableJob.class.isAssignableFrom(jobDetail.getJobClass()) ? "YES" : "NO" %>
</td>
<td class="tdBorder center">
<%= numInstances > 0 ? "<b>YES (" + numInstances + ")</b>" : "NO" %>
</td>
<td class="tdBorder center">
<a href="jobs.jsp?action=exec&group=<%= group %>&job=<%= job %>">Exec</a>&nbsp;|&nbsp;<a
href="jobs.jsp?action=pause&group=<%= group %>&job=<%= job %>"><%= isPaused ? "Resume" : "Pause" %></a>
</td>
</tr>
<%
i++;
}
}
%>
</table>
</body>
<%
}
catch (SchedulerException se) {
  se.printStackTrace();
}
%>
</html>

Note that this JSP file uses a style shit file named “css.css”. This css file gives a bit nicer look to the Jobs information page. You can download the “jobs.jsp” file as well as the “css.css” file from here.

Thursday, June 3, 2010

Java interview question: Fibonacci Series

Fibonacci series is a sequence of numbers, in which, each number is the sum of it’s previous 2 numbers. The first 2 numbers in the sequence are by definition: 0 and 1.
For some reason, it seem like writing a Java function that returns Fibonacci number is favorited by interviewers. I really find it difficult to understand why an interviewer would ask this kind of question, since it is not a question that implies on any practical knowledge or experience and can be easily memorized.
But, anyway, let’s see how it is done:
There are 2 ways of writing this function: recursive and none recursive. Interviewers may ask to write both versions.
Let’s start with the recursive version, which is surprisingly simple. well… simple if do not try to debug it in your head, but think of the process in a more mathematical way. In general, I believe that solving recursion problems is much simpler if you think of the solution in an abstract way, rather than trying to figure what exactly going on on each iteration.
Well, this is how the recursive function looks like:
package com.bashan.blog.interview;
/**
* @author Bashan
*/
public class Fibonacci {
  public static int fibonacci1(int n)
  {
    if (n == 0)
    {
      return 0;
    }
    else if (n == 1)
    {
      return 1;
    }
    else
    {
      return fibonacci1(n - 1) + fibonacci1(n - 2);
    }
}
 
public static void main(String[] args)
  {
    System.out.println(fibonacci1(12));
  }
}
Note, that both 2 first numbers in the series are handled separately. The output of the test program is of course: 144.
This function can be shorten to a single line of code:
package com.bashan.blog.interview;
/**
* @author Bashan
*/
public class Fibonacci {
  public static int fibonacci2(int n)
  {
    return n == 0 ? 0 : (n == 1 ? 1 : (fibonacci1(n - 1) + fibonacci1(n - 2)));
  }
}
The none recursive version should always save the current and the previous number in order to calculate the next number in the series. Let’s see how it looks in Java code:
package com.bashan.blog.interview;
/**
* @author Bashan
*/
public class Fibonacci {
  public static int fibonacci3(int n)
  {
    int prevN = 0, currentN = 1;
    for (int i = 0; i < n; i++)
    {
      int saveCurrentN = currentN;
      currentN = prevN + currentN;
      prevN = saveCurrentN;
    }
   
    return prevN;
  }
}
Note, that we return prevN rather than currentN. This saves us for writing a logic to handle the case of n = 0.
Here is a Java class that contains all 3 functions.

Check out this cool infographic to learn more about Fibonacci series.