/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Häbät
*/
import java.io.*;
import java.util.Date;
import java.util.*;
import java.util.ArrayList;
public class LearningDay implements Serializable {
final String datname = "Learning Times.lt";
private ArrayList<LearningDay> data;
private int counter;
private Date before = null;
private Date after;
Double timeLearnedHours;
ArrayList<Double> dayList = new ArrayList<Double>();
private Calendar presentDay;
public LearningDay(){
this.counter = 0;
presentDay = new GregorianCalendar();
this.presentDay.setTime(new Date());
data = new ArrayList<LearningDay>();
this.load();
}
private Calendar getpresentDay(){
return presentDay;
}
private void addToList(ArrayList<Double> list){
this.dayList.addAll(list);
}
private double getTimeDifference(int timesize){ //timesize: 0=seconds / 1 = minutes / 2 = hours
double buffer;
buffer = this.after.getTime() - this.before.getTime();
switch(timesize){
case 0: return buffer/1000;
case 1: return buffer/1000/60;
case 2: return buffer/1000/60/60;
default: return 0;
}
}
public void startLearning(){
this.before = new Date();
System.out.println("Begin to learn at "+this.before);
}
public void stopLearning(){
this.after = new Date();
System.out.println("Stopped learning at "+this.after);
timeLearnedHours = getTimeDifference(2);
dayList.add(timeLearnedHours);
}
public void removeWholeHistory(){
data.clear();
File file = new File(datname);
if(file.exists()){
file.delete();
}
}
public void removeDay(int day){
data.remove(day);
}
public void showPresentDay(){
Double buffer = 0.0;
for(int i = 0; i < dayList.size(); i++){
buffer += dayList.get(i);
System.out.println("Learning Period Number "+i+" in hours: "+dayList.get(i));
}
System.out.println("On this day learned : "+buffer+" hours");
}
private void showLessons(){
for(int i = 0; i < dayList.size(); i++){
counter++;
System.out.println("Learning Period Number "+counter+" in hours: "+dayList.get(i));
}
}
public void showData(){
System.out.println("************************");
for(int i = 0; i < data.size(); i++){
System.out.println("--------------");
System.out.println("Learning Times of "+data.get(i).getpresentDay().getTime());
data.get(i).showPresentDay();
System.out.println("--------------");
}
System.out.println("************************");
}
public void save(){
data.add(this);
//save
try{
FileOutputStream fos = new FileOutputStream(datname);
ObjectOutputStream output = new ObjectOutputStream(fos);
output.writeObject(this.data);
output.flush();
output.close();
} catch (Exception e){e.printStackTrace();}
}
public final void load(){
try{
FileInputStream fin = new FileInputStream(datname);
ObjectInputStream input = new ObjectInputStream(fin);
this.data = (ArrayList<LearningDay>)input.readObject();
input.close();
} catch (Exception e){e.printStackTrace();}
}
public void onThisDayLearned(int day, int month){
System.out.println("called");
for (int j = 0; j < this.data.size();j++) {
//System.out.println("Day: "+this.data.get(j).getpresentDay().get(Calendar.DATE));
//System.out.println("Month: "+this.data.get(j).getpresentDay().get(Calendar.MONTH));
if (this.data.get(j).getpresentDay().get(Calendar.DATE) == day && this.data.get(j).getpresentDay().get(Calendar.MONTH) == month-1) {
this.data.get(j).showLessons();
}
}
}
}