-1

I'm new to programming (doing a course on Computer Science) and one of the exercises is to have the program read a date and then print the next 30 days over and over until the end of the year.

Problem is, there are restrictions. I cannot use Date/Calendar classes, only the Scanner class. So I'm having some trouble getting the dates right... so the only way I've found is to use Switch and have a case for each month, but then there's the issue with the 30/31-day months and leap years. So the days are not the same.

Is there a simpler way to do it?

2 Answers2

0

If you can't use date/calendar classes, then the question is aimed at getting you to do the kind of things that date/calendar classes do internally. I wouldn't be surprised to see switch statements as part of your answer. You will need to teach your implementation knows how many days are in a month, which years are leap years etc.

Stochastically
  • 7,616
  • 5
  • 30
  • 58
0

You could use something like this,

Main class:

public class AppMain {

  public static void main(String[] args) {
    MyDate initDate = new MyDate(14, 1, 2012);
    printMyDate(initDate);

    MyDate newDate = initDate;

    do{
      newDate = DateIncrementator.increaseDate(newDate, 30);
      printMyDate(newDate);
    }while(newDate.getYear() == initDate.getYear());

  }

  private static void printMyDate(MyDate date){
    System.out.println("[Day: "+ date.getDay() + "][" + "[Mont: "+ date.getMonth() + "][" + "[Year: "+ date.getYear() + "]");
  }
}

DateIncrementator class:

public class DateIncrementator {
  private static final int[] MONTH_DAYS_NOP_LEAP_YEAR = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  public static MyDate increaseDate(MyDate date, int numberOfDays){
    if(numberOfDays < 1 || numberOfDays > 30){
      throw new IllegalArgumentException("numberOfDays must have a value between 1 and 30");
    }

    int numberDaysCurrentMonth = MONTH_DAYS_NOP_LEAP_YEAR[date.getMonth() - 1];

    if(date.getMonth() == 2 && isLeapYear(date.getYear())){
      numberDaysCurrentMonth++;
    }

    int newDay = date.getDay();
    int newMonth = date.getMonth();
    int newYear = date.getYear();

    newDay += numberOfDays;

    if(newDay > numberDaysCurrentMonth){
      newDay = newDay % numberDaysCurrentMonth;
      newMonth++;
    }

    if(newMonth > 12){
      newMonth = 1;
      newYear++;
    }

    return new MyDate(newDay, newMonth, newYear);
  }

  private static boolean isLeapYear(int year){
    if(year % 4 != 0){
      return false;
    }

    if(year % 100 != 100){
      return true;
    }

    if(year % 400 == 0){
      return true;
    }else{
      return false;
    }
  }
}

MyDate class:

public class MyDate {
  private int day; // 1 to 31
  private int month; // 1 to 12
  private int year; // 0 to infinite


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

  public int getDay() {
    return day;
  }

  public int getMonth() {
    return month;
  }

  public int getYear() {
    return year;
  }  
}