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;
}
}
}
Aucun commentaire:
Enregistrer un commentaire