Model.Perda.java Source code

Java tutorial

Introduction

Here is the source code for Model.Perda.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import org.joda.time.DateTime;
import org.joda.time.Interval;

/**
 *
 * @author Joo
 */
public class Perda {

    private String tipoDeFlor;
    private LinkedList<Semana> semanas;

    public Perda(String tipoDeFlor, Mes mes, int ano) {
        this.tipoDeFlor = tipoDeFlor;
        this.semanas = geraSemanas(mes, ano);
    }

    private LinkedList<Semana> geraSemanas(Mes mes, int ano) {
        semanas = new LinkedList<>();

        DateTime monthStart = new DateTime(getFirstDayOfMonth(mes, ano));
        DateTime monthEnd = new DateTime(getLastDayOfMonth(mes, ano));
        Interval monthInterval = new Interval(monthStart, monthEnd);

        ///////////////////////
        String firstWeekNumber = new SimpleDateFormat("w").format(monthStart);
        String tempWeek = "";

        ///////////////////////
        // Variaveis temporarias
        DateTime newWeekStart = new DateTime();
        DateTime newWeekEnd = new DateTime();
        Interval newInterval = new Interval(newWeekStart, newWeekEnd);

        DateTime oldWeekStart = new DateTime();
        DateTime oldWeekEnd = new DateTime();
        Interval oldInterval = new Interval(oldWeekStart, oldWeekEnd);

        String month = new SimpleDateFormat("MM").format(newWeekStart);
        ///////////////////////

        //TODO: gerar as semanas dependendo do numero de semanas com o seu intervalo de cada mes
        //while....
        return semanas;
    }

    private Date getFirstDayOfMonth(Mes mes, int ano) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.MONTH, parseMonth(mes));
        calendar.set(Calendar.YEAR, ano);
        return calendar.getTime();
    }

    private Date getLastDayOfMonth(Mes mes, int ano) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.MONTH, parseMonth(mes));
        c.set(Calendar.YEAR, ano);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    private int parseMonth(Mes mes) {
        switch (mes) {

        case Janeiro:
            return 1;
        case Fevereiro:
            return 2;
        case Marco:
            return 3;
        case Abril:
            return 4;
        case Maio:
            return 5;
        case Junho:
            return 6;
        case Julho:
            return 7;
        case Agosto:
            return 8;
        case Setembro:
            return 9;
        case Outubro:
            return 10;
        case Novembro:
            return 11;
        case Dezembro:
            return 12;
        default:
            return 0;
        }
    }

    public String getTipoDeFlor() {
        return tipoDeFlor;
    }

    public LinkedList<Semana> getSemanas() {
        return semanas;
    }

}