0

So I am able to print the Gregorian Calendar for the current month, but when I am trying to flip to the next or previous month, it just seems to reprint the current month again. Please note that by "printing" the calendar, I mean that I am actually formatting it to look like I full calendar (all the days like a Google calendar, etc). Also, this program is in very early stages. Ultimately, I want it to support adding events to days, printing the events, etc.

Anyway, here is some code that I have that might be relevant:

MyCalendar class:

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;


public class MyCalendar {

    GregorianCalendar calendar;
    String[] months;
    String[] dayOfWeek;
    int todayDay;
    int maxDays;
    static PrintMenu print = new PrintMenu();
    private HashMap<MyCalendar, Event> myCalHash = new HashMap<MyCalendar, Event>();

    MyCalendar(){
        calendar = new GregorianCalendar(); //capture today
        months = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        dayOfWeek = new String[]{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"};
        todayDay = calendar.get(Calendar.DAY_OF_MONTH);
        maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

    }

    public Calendar getCalendar(){
        return calendar;
    }

    public void setCalendar(GregorianCalendar cal){
        calendar = cal;
    }

    public Date getFirstDayOfMonth(){
        return calendar.getTime();
        //return calendar.get((Calendar.DAY_OF_WEEK) - 1);
    }

    public int getDay(){
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public int getMaximumDays(){
        return maxDays;
    }

    public int getTodayDay(){
        return todayDay;
    }

    public int getMonth(){
        return calendar.get(Calendar.MONTH);
    }

    public int getYear(){
        return calendar.get(Calendar.YEAR);
    }

    public void setNext(){
        calendar.add(Calendar.MONTH, 1);
    }
    public void setPrevious(){
        calendar.add(Calendar.MONTH, -1);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        print.printCalendar(false);
        print.printMenu();

        System.out.println("I think we're done here!");
    }

}

PrintMenu Class:

public class PrintMenu {

MenuHandler menu = new MenuHandler();
MyCalendar myCalendar = new MyCalendar();


void printCalendar(boolean withEvents){

     int count = 0; //for formatting 
     int day = myCalendar.getTodayDay();

     System.out.println(myCalendar.months[myCalendar.getMonth()] + " " + myCalendar.getYear());
     System.out.print(myCalendar.dayOfWeek[6] + " ");
     for(int i = 0; i < myCalendar.dayOfWeek.length - 1; i++){
         System.out.print(myCalendar.dayOfWeek[i] + " ");
     }

    // int daysInMonth = myCalendar.getMaximumDays(); // 28
    for(int i = 1; i <= myCalendar.dayOfWeek.length; i++){
        count++;
        if(!myCalendar.dayOfWeek[i].equals(myCalendar.getFirstDayOfMonth().toString().substring(0, 2))){
            System.out.print("  ");
        }else{
            count = 0;
            break;
        }
    }
     System.out.println();
    for(int i = 1; i <= myCalendar.getMaximumDays(); i++){
        if(!withEvents){
            if(i == day){
                System.out.print("[" + i + "]");
            }
            if(i < 10){
                System.out.print(" " + i + " ");

            }else{
                if(i != day){
                    System.out.print(i + " ");
                }
            }
        }
        else{
        if(i < 10){
            System.out.print(" " + i + " ");

        }else{
            System.out.print(i + " ");
        }
        }


        count++;
        if(count >= 7){
            System.out.println();
            count = 0; //reset back
        }
    }
}

void printMenu(){

    System.out.println("-------------------------------------------------------------------");

    System.out.println("Select one of the following options: ");
    System.out.println("[L]oad   [V]iew by  [C]reate, [G]o to [E]vent list [D]elete  [Q]uit");
    menu.handleChoice();
    printMenu();
}

}

ViewCalendar class (this is where I'm trying to navigate the calendar and failing)

import java.util.Calendar;
import java.util.Scanner;


public class ViewCalendar {
Scanner sc = new Scanner(System.in);
MyCalendar myCalendar = new MyCalendar();

public void whatView(){

    System.out.print("[D]ay view or [M]view? ");
    char userChoice = sc.next().charAt(0);
    if(Character.toUpperCase(userChoice) == 'D'){ dayView(); }
    else if(Character.toUpperCase(userChoice) == 'M'){ monthView(); }
    else{
        System.out.println("Invalid choice.");
        whatView();
    }

}
public void dayView(){
    //print day calendar
    System.out.print("[P]revious or [N]ext or [M]ain menu ? ");
    char userChoice = sc.next().charAt(0);
    if(Character.toUpperCase(userChoice) == 'P'){

    }
    else if(Character.toUpperCase(userChoice) == 'N'){

    }
    else if(Character.toUpperCase(userChoice) == 'M'){
        return;
    }
    else{
        System.out.println("Invalid choice.");
        dayView();
    }
}
public void monthView(){
    //print month calendar
    myCalendar.print.printCalendar(true);
    System.out.print("[P]revious or [N]ext or [M]ain menu ? ");
    char userChoice = sc.next().charAt(0);
    if(Character.toUpperCase(userChoice) == 'P'){
        myCalendar.setPrevious();
        myCalendar.print.printCalendar(true);
    }
    else if(Character.toUpperCase(userChoice) == 'N'){
        myCalendar.setNext();
        myCalendar.print.printCalendar(true);
    }
    else if(Character.toUpperCase(userChoice) == 'M'){
        return;
    }
    else{
        System.out.println("Invalid choice.");
        dayView();
    }
}

}

Anyway, I hope that's not too much information. I followed the calendar.add() syntax, but it doesn't seem to have any effect. I appreciate any insight you guys may have!

Here is the MenuHandler class:

import java.util.Scanner;


public class MenuHandler {

Scanner sc = new Scanner(System.in);
ViewCalendar view = new ViewCalendar();


    public void handleChoice(){

        char userChoice = sc.next().charAt(0);
        switch(Character.toUpperCase(userChoice)){
            case 'L':

            case 'V': view.whatView();
                    //  menu.printMenu();

            case 'C':

            case 'G':

            case 'E':

            case 'D':

            case 'Q': return;
        }


        }

}
  • where is the MenuHandler class ? – adrCoder Feb 28 '15 at 22:46
  • `myCalendar.setPrevious();` this feels like something is missing. `myCalendar = myCalendar.setPrevious();` could work. – WonderWorld Feb 28 '15 at 22:48
  • @adrCoder it's a separate class which scans in the users choice to the main menu (load, view by, etc...) and calls other methods in other classes accordingly. I can post it, but right now the only thing its doing is calling whatView() method in ViewCalendar class if user presses 'V'. – hotshotennis Feb 28 '15 at 22:54
  • @WonderWorld if I do that, then I need to give setPrevious a return of type MyCalendar and have it return that. Ok, I did that, but in this case, it throws a syntax error that says "Cannot return a void result". – hotshotennis Feb 28 '15 at 22:56
  • @hotshotennis since the menuhandler class is missing its impossible to debug the code because i have to comment out stuff and then it prints march 2015, select one of the following options, and then i think we're done here and the program is finished. Also the printmenu() i have to comment out, because the program goes in an infinite loop and crashes. – WonderWorld Feb 28 '15 at 23:15
  • @WonderWorld Ok, I updated my original post with the MenuHandler. Yeah, I definitely don't have infinity loop problems. Hopefully this will help. – hotshotennis Feb 28 '15 at 23:29

1 Answers1

0

@hotshotennis you can try to use the method set() instead of method add() to manipulate the date.

public class MyCalendar {

  .....
  ..... 

  public void setNext(){
     calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
  }
  public void setPrevious(){
     calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
  }

  .....    
  ..... 
}
Wender..
  • 16
  • 3
  • That sounds like a pretty good idea. Would there be complications further along the way when it comes to adding events to the calendar and such? From my understanding, set makes it believe that this is that current date today. – hotshotennis Mar 01 '15 at 05:40
  • I took a better look at your code and seems like the problem is that, the `PrintMenu` class has their own `MyCalendar` what is used in `printCalendar()` method. You should try to pass it as parameter for `printCalendar()` or maybe instead of have `PrintMenu` as static field have it as a private one and pass `MyCalendar` as a constructor parameter, the latter will not change the `printCalendar()` signature. – Wender.. Mar 02 '15 at 00:42