-2

Trying to write a code for my intro to java course but unable to reset int value.

For my intro course, I have to write a program that determines a due date based on the type of book the user is checking out. I am not allowed to use any date formats, just simply inputting month day and year as integers.

The main part of this program is that new books have a 14 day check out and non -new books have a 21 day check out limit. I have figured out how to add the specified number of days based on the type of book, but given that days, months and year are defined as integers, how would I reset the days if the inputted date of the check out exceeds the number of day within that particular month?

I know how to format it to display it continuing over to the next month. But say the user inputs the check out day is the 15 of February, how would my program know how to restart counting on the next month but also know when it's reaching its limit of either 14 or 21 days?

String releaseType;
int coMonth, coYear;
int coDays = 0;

// determing how many days added to due date
if (releaseType == "NEW"){
    coDays += 14;
}else{
    coDays += 21;
}
if (coDays > 30){
    coMonth +=1;
}
if (coMonth >12){
    coYear +=1;
}
System.out.println("You due date is " + coMonth + "/" + coDays + "/" + coYear);

Input data: NEW, 2 (month) 15 (day) 2019 (year) output: You due date is 3/36/2019

Anil
  • 655
  • 1
  • 11
  • 25
  • I'm a little bit confused by your question, are you asking how to increment the amount of months when needed? If so, you simply need to check if the amount of days in your month is greater than what it is supposed to be. If it's the case, then you can increment month by 1 and then set the amount of days in that month. – David Oct 07 '19 at 02:19
  • 1
    Possible related: [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Oct 07 '19 at 02:42
  • 1
    Problem 1) when `coDays` plus the checkout period exceeds 30, you properly bump up `coMonth`, but you also need to _subtract 30_ from `coDays`; problem 2) your computation assumes that months are 30 days long, so it only works correctly for checkout dates in April, June, September, or November. – Kevin Anderson Oct 07 '19 at 02:53

1 Answers1

0

Below is the outline for a class that should do what you want.

class Date
{
    Date(int InputDay, int InputMonth, int InputYear;
    {
         Day = InputDay;
         Month = InputMonth;
         Year = InputYear;
    }

    public final int GetDay()
    {
        return Day;
    }

    public final int GetMonth()
    {
        return Month;
    }

    public final int GetYear()
    {
        return Year;
    }

    // Increments the current date by NumDays. 
    public final Date AddDays(int NumDays)
    {
        int TempDay = Day;
        int TempMonth = Month;
        int TempYear = Year;

        while (NumDays > 0)
        {
            switch (Month)
            {
                case 1:  // Jan
                {
                    TempDay += NumDays;

                    if (TempDay > 31)
                    {
                        NumDays = TempDay - 31;
                        TempDay = 1;
                        TempMonth++;
                    }
                    else
                    {
                        NumDays = 0;
                    }
                } break;

                case 2:  // Feb
                {
                    TempDay += NumDays;

                    if (TempDay > 28 && IsLeapYear() == false)
                    {
                        NumDays = TempDay - 28;
                        TempDay = 1;
                        TempMonth++;
                    }
                    else if (TempDay > 29 && IsLeapYear() == true)
                    {
                        NumDays = TempDay - 29;
                        TempDay = 1;
                        TempMonth++;
                    }
                    else
                    {
                        NumDays = 0;
                    }

               } break;

               // Fill in the rest of the months. I've done the 
               // important ones.


                case 12:  // Dec
                {
                    TempDay += NumDays;

                    if (TempDay > 31)
                    {
                        NumDays = TempDay - 31;
                        TempDay = 1;
                        TempMonth = 1;
                        TempYear++;
                    }
                    else
                    {
                        NumDays = 0;
                    }

                } break;

            } // End switch 


        } // End while loop


        return new Date(TempDay, TempMonth, TempYear);
     }

     private boolean IsLeapYear()
     {
        boolean LeapYear = false;

        // Note: In the Gregorian calendar, it is also a leap year if the
        // year is divisible by 400 but it's not a leap year if it is
        // divisible by 100. 
        if ( (Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0)
        {
            LeapYear = true;
        }

        return LeapYear;
    }

    private final int Day;
    private final int Month; // Range: 1 - 12
    private final int Year;
}

It would be used as follows:

Date Today = new Date(7,10,2019), DueDate;

DueDate = Today.AddDays(21); 
hermit
  • 56
  • 6