From 3a73f0bf881cbffba48d013b787c91f5df4765f9 Mon Sep 17 00:00:00 2001 From: Paul Rauser <paulr@Pauls-MacBook-Air.local> Date: Tue, 3 Sep 2024 13:16:34 +0200 Subject: [PATCH] chore: create clean task --- src/nextday/.gitkeep | 0 src/nextday/Date.java | 122 --------------------------------- src/nextday/DateException.java | 11 --- src/nextday/Month.java | 10 --- src/nextday/NextDay.java | 10 --- 5 files changed, 153 deletions(-) create mode 100644 src/nextday/.gitkeep delete mode 100644 src/nextday/Date.java delete mode 100644 src/nextday/DateException.java delete mode 100644 src/nextday/Month.java delete mode 100644 src/nextday/NextDay.java diff --git a/src/nextday/.gitkeep b/src/nextday/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/nextday/Date.java b/src/nextday/Date.java deleted file mode 100644 index 19f49cc..0000000 --- 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 e981e44..0000000 --- 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 503191b..0000000 --- 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 d6b481f..0000000 --- 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); - } -} -- GitLab