import java.io.*;
import java.util.Date;
import java.net.URL;

class Alter
{
String url = "";
Wertepaar[] werte;

//#########################
public Alter(String base)
//#########################
    {
    url = base;
    }


//************************************************
public void ladeWerte(String file)
//************************************************
    {
    try{

    BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url + file).openStream()));
    String line = null;
    int year, month, day;
    
    //länge testen und wertepaar initialisieren
    int length =0;
    while(in.readLine() != null)
        {
        //if(line != null){System.out.println("test: " + line);}
        length++;
        }
    //System.out.println("länge: " + length);

    werte = new Wertepaar[length];

    in = new BufferedReader(new InputStreamReader(new URL(url + file).openStream()));

    for(int i=0;true;i++)
        {
        line = in.readLine(); //System.out.println("read: " + line);
        if(line == null)
            break;

        day = Integer.parseInt(line.substring(0, 2)); //System.out.println(line.substring(0, 2));
        month = Integer.parseInt(line.substring(3, 5))-1;//System.out.println(line.substring(3, 5))-1;
        year = Integer.parseInt(line.substring(6, 10))-1900;//System.out.println(line.substring(6, 10));
        
        werte[i] = new Wertepaar(new Date(year, month, day), Integer.parseInt(line.substring(11, line.length())));
        
        }

    }catch(Exception e){System.err.println(e);}
    }

/**
Berechnet das Datum zu dem id gehört mit einem Polynom 1. Grades (Lineare Interpolation)
*/
//************************************************
public Date polynom1(int id)
//************************************************
    {
    int id0 = getLowerValue(id).getID();System.out.println("id0: " + id0);
    int id1 = getGreaterValue(id).getID();System.out.println("id1: " + id1);
    long date0 = getLowerValue(id).getDate().getTime();System.out.println("date0: " + date0);
    long date1 = getGreaterValue(id).getDate().getTime();System.out.println("date1: " + date1);
    
    System.out.println("date1-date0: " + (date1-date0));
    System.out.println("id1-id0: " + (id1-id0));
    System.out.println("id-id0: " + (id-id0));
    System.out.println("(date1-date0)/(id1-id0)*(id-id0): " + ((date1-date0)/(id1-id0)*(id-id0)));
    System.out.println("(double)(date1-date0)/(double)(id1-id0)*(id-id0): " + ((double)(date1-date0)/(double)(id1-id0)*(id-id0)));
    System.out.println("date0: " + (new Date(date0)).toString());
    
    long add = (long)((double)(date1-date0)/(double)(id1-id0)*(id-id0));
    
    System.out.println("ergebnis_long: " + (date0 + add));
    Date d = new Date(date0 + add);
    return d;
    }

/**
Berechnet das Datum zu dem id gehört mit einem Polynom 2. Grades
Formel in Lagrange-Form.
*/
//************************************************
public Date polynom2(int id)
//************************************************
    {
    double id0 = (double)getLowerValue(id).getID();System.out.println("id0: " + id0);
    double id1 = (double)getGreaterValue(id).getID();System.out.println("id1: " + id1);
    double id2 = (double)getGreaterValue((int)id1).getID();System.out.println("id2: " + id2);
    double date0 = (double)getLowerValue(id).getDate().getTime();
    double date1 = (double)getGreaterValue(id).getDate().getTime();
    double date2 = (double)getGreaterValue((int)id1).getDate().getTime();

    double l0 = ((id-id1)*(id-id2)) / ((id0-id1)*(id0-id2));System.out.println("l0: " + l0);
    double l1 = ((id-id0)*(id-id2)) / ((id1-id0)*(id1-id2));System.out.println("l1: " + l1);
    double l2 = ((id-id0)*(id-id1)) / ((id2-id0)*(id2-id1));System.out.println("l2: " + l2);

    Date d = new Date((long)(date0*l0 + date1*l1 + date2*l2));System.out.println("long: " + (date0*l0 + date1*l1 + date2*l2));
    return d;
    }

//************************************************
Wertepaar getLowerValue(int id)
//************************************************
    {
    for(int i = werte.length-1; i>=0 ;i--)
        {
        if(werte[i].getID() < id)
            return werte[i];
        }
    return werte[0];
    }

//************************************************
Wertepaar getGreaterValue(int id)
//************************************************
    {
    for(int i = 0; i<werte.length ;i++)
        {
        if(werte[i].getID() > id)
            return werte[i];
        }
    return werte[werte.length-1];
    }

public static void main(String[] args)
{
Alter a = new Alter("");
a.ladeWerte("werte.txt");
System.out.println(a.polynom1(8000000).toString());
System.out.println(a.polynom2(8000000).toString());
/*
for(int i=0;i<a.werte.length;i++)
    {
    System.out.println(a.werte[i].getDate().toString() + " " + a.werte[i].getID());
    }*/
}
}