diff --git a/src/nextday/.gitkeep b/src/nextday/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/nextday/Date.java b/src/nextday/Date.java deleted file mode 100644 index 19f49ccd13de8557685a2ee7bc1df30818186c72..0000000000000000000000000000000000000000 --- a/src/nextday/Date.java +++ /dev/null @@ -1,122 +0,0 @@ -package nextday; - -public class Date { - private int day; - private Month month; - private int year; - - public Date(int day, Month month, int year) throws DateException { - validateDate(day, month, year); - this.day = day; - this.month = month; - this.year = year; - } - - public Date getNextDay() { - try { - Date newDate = new Date(day, month, year); - - if (day == 29 & month == Month.FEBRUARY) { - newDate.setDay(1); - newDate.setMonth(Month.MARCH); - return newDate; - } - - if (day == 28 & month == Month.FEBRUARY & isLeapYear(year)) { - newDate.setDay(29); - return newDate; - } - - if (day == 28 & month == Month.FEBRUARY & !isLeapYear(year)) { - newDate.setDay(1); - newDate.setMonth(Month.MARCH); - return newDate; - } - - if (!isLongMonth(month) && day == 30) { - newDate.setDay(1); - newDate.setMonth(month.getNextMonth()); - return newDate; - } - - if (day == 31) { - newDate.setDay(1); - if (month == Month.DECEMBER) { - newDate.setMonth(Month.JANUARY); - newDate.setYear(year + 1); - } else { - newDate.setMonth(month.getNextMonth()); - } - return newDate; - } - newDate.setDay(day + 1); - - return newDate; - - } catch (DateException e) { - throw new RuntimeException("If this is thrown that either means your algorithm or your validation is buggy."); - } - } - - private boolean isLeapYear(int year){ - if(year%400==0){ - return true; - } - if(year%4==0 & year%100!=0){ - return true; - } - return false; - } - - private boolean isLongMonth(Month month) { - switch (month) { - case FEBRUARY: - case APRIL: - case JUNE: - case SEPTEMBER: - case NOVEMBER: - return false; - default: - return true; - } - } - - private void validateDate(int day, Month month, int year) throws DateException { - if(day > 31 || day < 1){ - throw new DateException("Invalid day."); - } - - if(year < 0){ - throw new DateException("Invalid year."); - } - - if(!isLongMonth(month) & day > 30){ - throw new DateException("Invalid day in month."); - } - - if(!isLeapYear(year) & day > 28 & month == Month.FEBRUARY){ - throw new DateException("Invalid day in month."); - } - - if(isLeapYear(year) & day > 29 & month == Month.FEBRUARY){ - throw new DateException("Invalid day in month."); - } - } - - private void setMonth(Month month) { - this.month = month; - } - - private void setDay(int day) { - this.day = day; - } - - private void setYear(int year) { - this.year = year; - } - - @Override - public String toString() { - return "" + this.day + "." + (this.month.ordinal() + 1) + "." + this.year; - } -} diff --git a/src/nextday/DateException.java b/src/nextday/DateException.java deleted file mode 100644 index e981e444284f6a0fa464a5c2381bbac8f1ee0766..0000000000000000000000000000000000000000 --- a/src/nextday/DateException.java +++ /dev/null @@ -1,11 +0,0 @@ -package nextday; - -public class DateException extends Exception { - public DateException() { - super("The specified date is invalid!"); - } - - public DateException(String errorDescription) { - super(errorDescription); - } -} diff --git a/src/nextday/Month.java b/src/nextday/Month.java deleted file mode 100644 index 503191b689ffeec7353a7c8ce0cd0001d5c0195f..0000000000000000000000000000000000000000 --- a/src/nextday/Month.java +++ /dev/null @@ -1,10 +0,0 @@ -package nextday; - -public enum Month { - JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, - NOVEMBER, DECEMBER; - - public Month getNextMonth() { - return values()[(ordinal() + 1) % values().length]; - } -} diff --git a/src/nextday/NextDay.java b/src/nextday/NextDay.java deleted file mode 100644 index d6b481f8959c2f25b26675a64c53929c944c8691..0000000000000000000000000000000000000000 --- a/src/nextday/NextDay.java +++ /dev/null @@ -1,10 +0,0 @@ -package nextday; - -public class NextDay { - public static void main(String[] args) throws DateException { - Date date = new Date(31, Month.DECEMBER, 2024); - System.out.println("Old date: " + date); - Date newDate = date.getNextDay(); - System.out.println("New date: " + newDate); - } -}