user6941415
user6941415

Reputation:

How to use switch-statement correctly?

I need to write a Java program (class NextDay) that calculates and prints the date of the next day by entering the day, month, and year.

Sample call and output:

Actual Date 12.12.2004
The next day is the 13.12.2004.

The input should also be checked for correctness! If an erroneous date constellation (e.g., 32 12 2007) has been entered, the exception InvalidDateArgumentsException is to be thrown. Define this class in a suitable form as a subclass of the class java.lang.Exception.

I started to creating it but the problem is; i cant tell that < or > in switch statements.What should i do? Here are my classes:

public class Date {

    private int day;
    private int month;
    private int year;

    public Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public Date getNextDay() throws Exception {
        if (isLeapYear() == true) {
            switch (month) {

                case 1:
                    day = 31;
                    break;

                case 2:
                    day = 29;
                    break;

                case 3:
                    day = 31;
                    break;

                case 4:
                    day = 30;
                    break;

                case 5:
                    day = 31;
                    break;

                case 6:
                    day = 30;
                    break;

                case 7:
                    day = 31;
                    break;

                case 8:
                    day = 31;
                    break;

                case 9:
                    day = 30;
                    break;

                case 10:
                    day = 31;
                    break;

                case 11:
                    day = 30;
                    break;

                case 12:
                    day = 31;
                    break;
            }

        }
        return new Date(day + 1, month, year);

    }

    public int getDay() {
        return day;
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    public boolean isLeapYear() {
        if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0) {
            return true;
        }
        return false;
    }

    public String toString() {
        return this.day + "." + this.month + "." + this.year;
    }
}

...

public class NextDay {

    public static void main(String args[]) throws Exception {
        Date dateObj = new Date(20, 5, 2016);
        System.out.println("Old Date: " + dateObj.getDay() + "." + dateObj.getMonth() + "." + dateObj.getYear() + ".");
        System.out.println("The next day is " + dateObj.getNextDay().toString() + ".");
    }
}

Upvotes: 3

Views: 1060

Answers (2)

SpiralDev
SpiralDev

Reputation: 7321

You are saying that you want an "or" statement inside switch, right? If you write case labels line by line you get "or statement", like this:

switch(variable){
        case 1:
        case 2:
        case 3: {
            //.. when variable equals to 1 or 2 or 3
        }
    }

So that, you can write your getMaxDaysInMonth method like this:

int getMaxDaysInMonth()
{
    int daysInMonth = 0;
    switch(month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            daysInMonth = 31;
            break;
        case 2:
            if(isLeapYear())
            {
                daysInMonth = 29;
            }
            else
            {
                daysInMonth = 28;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            daysInMonth = 30;   
    }

    return daysInMonth;
}

Also, you are checking for the leap year incorrectly. Here's the correct way:

boolean isLeapYear(){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

And here's how you increment a day:

void incrementDate(){
    if((day + 1) > getMaxDaysInMonth())
    {
        day = 1;        
        if (month == 12)
        {
            month = 1;
            year++;
        }else{   
          month++;
    }
    } else {
       day++;
    }    
}

Upvotes: 1

Evgeny Veretennikov
Evgeny Veretennikov

Reputation: 4229

EDIT since we can't just use standard classes

If you have to use switch case, you should use it to set max day in current month and then check, if your current day more than this max day:

int maxDay;
switch (month) {
case 1: maxDay = 31; break;
case 2: maxDay = isLeapYear() ? 29 : 28; break;
case 3: maxDay = 31; break;
// ... other months ...
case 12: maxDay = 31; break;
default: throw new InvalidDateArgumentsException();
}
if (isLeapYear()) {
    maxDay = 29;
}
if (day > maxDay) {
    throw new InvalidDateArgumentsException();
}

Upvotes: 0

Related Questions