package pl.dmcs.springbootjsp_iwa;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        ArrayList<Integer> sequence = new ArrayList<>();
        Scanner s = new Scanner(System.in);
        try {
            System.out.print("Which fibonacci number to calculate: ");
            final int index = s.nextInt();
            if (index < 1) {
                System.out.println("Enter a positive integer");
                return;
            }
            sequence.add(0);
            sequence.add(1);
            for (int i = 2; i < index; i++) {
                sequence.add(sequence.get(sequence.size() - 1) + sequence.get(sequence.size() - 2));
            }
            System.out.printf("#%d Fibonacci number is %d\n", index, sequence.get(index - 1));
        } catch (InputMismatchException e) {
            System.out.println("Invalid input, enter a positive integer");
        }
    }

}
