Added a calendar class

This commit is contained in:
shaggy3007 2023-10-30 15:17:01 +01:00
parent 7c45335ce8
commit d583187651
4 changed files with 82 additions and 4 deletions

View File

@ -24,6 +24,11 @@ public class Date implements Comparable<Date> , Iterable<Date>{
this.month = O.month;
this.year = O.year;
}
public Date(int year) {
this.day = 1;
this.month = 1;
this.year = year;
}
private boolean isValid(int day, int month, int year) {
if (month > 0 && month < 13) {
if (day > 0 && day <= DaysInMonth[month-1]) {

View File

@ -3,8 +3,8 @@ package Calendar;
public class MainTest {
public static void main(String[] args) {
Month M = new Month(2023,10,Days.Sunday);
System.out.println(M.toString());
Year Y = new Year(2023, Days.Monday);
System.out.println(Y);
}

View File

@ -14,6 +14,7 @@ public class Month implements Iterable<Month>,Comparable<Month>{
public Month (int year, int ordinal, Days firstDay) {
this.First = new Date(1,ordinal,year);
this.ordinal = ordinal;
name = Months.values()[ordinal-1];
Date tmpDate = new Date(First);
Week tmpWeek = new Week(tmpDate,firstDay.ordinal()+1);
@ -27,6 +28,9 @@ public class Month implements Iterable<Month>,Comparable<Month>{
}while(tmpDate.getDay()<DaysInMonth[ordinal-1]);
}
public TreeSet<Week> getWeeks() {
return Weeks;
}
@Override
public int compareTo(Month o) {
@ -50,13 +54,18 @@ public class Month implements Iterable<Month>,Comparable<Month>{
if (ordinal == 12) {
return new Month(First.getYear()+1,1,Weeks.last().getWeekDays().last().getName());
} else {
return new Month(First.getYear(),ordinal+1,Weeks.last().getWeekDays().last().getName());
return new Month(First.getYear(),ordinal+1,Days.values()[(Weeks.last().getWeekDays().last().getName().ordinal()+1)%7]);
}
}
};
}
public Date getDate() {
return First;
}
public Months getName() {
return name;
}
@ -64,7 +73,7 @@ public class Month implements Iterable<Month>,Comparable<Month>{
String out = name.toString()+":\n";
for (Week W: Weeks) {
out += W.toString()+ "\n";
out +="\t" + W.toString()+ "\n";
}
return out;

View File

@ -0,0 +1,64 @@
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()) {
System.out.println(tmpMonth);
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++;
}
public String toString() {
String out = year + " :\n";
for (Month M : YMonths) {
out += "\t" + M.toString()+ "\n";
}
return out;
}
}