Elaborato_IS/Elaborato_IS/src/Calendar/Year.java

70 lines
1.4 KiB
Java

package Calendar;
import java.util.Iterator;
import java.util.TreeSet;
public class Year implements Comparable<Year>,Iterable<Year> {
private int year;
private Days firstDay;
private TreeSet<Month> YMonths = new TreeSet<>();
public Year (int year, Days firstDay) {
this.year = year;
this.firstDay = firstDay;
Month tmpMonth = new Month(year,1,firstDay);
YMonths.add(tmpMonth);
while (tmpMonth.getName().ordinal() < Months.December.ordinal()) {
tmpMonth = tmpMonth.iterator().next();
YMonths.add(tmpMonth);
}
}
@Override
public Iterator<Year> iterator() {
// TODO Auto-generated method stub
return new Iterator<Year>() {
@Override
public boolean hasNext() {
// there's always a next year
return true;
}
@Override
public Year next() {
// TODO Auto-generated method stub
return new Year(nextYear(),Days.values()[(YMonths.last().getWeeks().last().getWeekDays().last().getName().ordinal()+1)%7]);
}
};
}
@Override
public int compareTo(Year o) {
// TODO Auto-generated method stub
if (year-o.year < 0) {
return -1;
} else if (year-o.year > 0) {
return 1;
} else return 0;
}
public int nextYear() {
return year+1;
}
public String toString() {
String out = year + " :\n";
for (Month M : YMonths) {
out += "\t" + M.toString()+ "\n";
}
return out;
}
public TreeSet<Month> getMonths() {
return YMonths;
}
public int getYear() {
return year;
}
}