Skip to content
Snippets Groups Projects
Commit 3a73f0bf authored by Paul Rauser's avatar Paul Rauser
Browse files

chore: create clean task

parent c1336b94
No related branches found
No related tags found
No related merge requests found
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;
}
}
package nextday;
public class DateException extends Exception {
public DateException() {
super("The specified date is invalid!");
}
public DateException(String errorDescription) {
super(errorDescription);
}
}
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];
}
}
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment