package Calendar; import java.util.Iterator; import java.util.TreeSet; public class Year implements Comparable,Iterable { private int year; private Days firstDay; private TreeSet 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 iterator() { // TODO Auto-generated method stub return new Iterator() { @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 getMonths() { return YMonths; } public int getYear() { return year; } }