Question: Please help with a calendar in java. I'm trying to implement a command menu in my calendar however am having a bit of trouble with

Please help with a calendar in java.

I'm trying to implement a command menu in my calendar however am having a bit of trouble with coding it.

my code :

1 // Java program to display graphical calendar 2 3 import java.util.*; 4 5 public class Assignment1 { 6 7 // draw Month calendar 8 public static void drawMonth(int month){ 9 10 System.out.println(); 11 System.out.println(" || ||"); 12 System.out.println(" ( o l o )"); 13 System.out.println("O|Calender|O"); 14 System.out.println(" ( ___ )"); 15 System.out.println(" U U "); 16 17 System.out.println("\t\t\t\t\t\t"+month+"\t\t\t"); 18 19 for(int i=0;i<5;i++){ 20 drawRow(i); 21 } 22 23 System.out.println("============================================"); 24 25 } 26 27 // draw a row for the calendar 28 public static void drawRow(int row) { 29 30 int startday=7*row + 1; 31 32 System.out.println("==========================================="); 33 34 for(int i=0;i<7;i++){ 35 36 System.out.print("|\t"+startday+"\t"); 37 38 startday++; 39 40 } 41 42 System.out.print("|\t"); 43 44 System.out.println(" |\t\t|\t\t|\t\t|\t\t|\t\t|\t\t|\t\t| |\t\t|\t\t|\t\t|\t\t|\t\t|\t\t|\t\t|"); 45 46 } 47 48 // display date 49 public static void displayDate(int month,int day){ 50 51 System.out.println("Month: "+ month); 52 53 System.out.println("Day: "+ day); 54 55 } 56 57 // get month from date 58 public static int monthFromDate(String date) { 59 60 int month = Integer.parseInt(date.substring(0,date.indexOf("/"))); 61 62 return month; 63 64 } 65 66 // get day from date 67 public static int dayFromDate(String date) { 68 69 int day = Integer.parseInt(date.substring(date.indexOf("/")+1)); 70 71 return day; 72 73 } 74 75 //this tells the user which commands to use 76 public static void menu() { 77 System.out.println("Please type a command"); 78 System.out.println(" 'e' to enter a date and display the month"); 79 System.out.println(" 't' to display today"); 80 System.out.println(" 'n' to display next month"); 81 System.out.println(" 'p' to display past month"); 82 System.out.println(" 'q' to quit"); 83 84 } 85 86 public static void main(String[] args) { 87 Scanner console = new Scanner(System.in); 88 String command = console.next(); 89 while(command != console.nextInt()){ 90 menu(); 91 92 //gets the month of a date the user inputs in 93 if(command.equals("e")){ 94 String userDate; 95 System.out.println(" What day would you like to look at?(mm/dd)"); 96 97 userDate = console.nextLine(); 98 99 int userDay = dayFromDate(userDate); // get day from user input 100 101 int userMonth = monthFromDate(userDate); // get month from user input 102 103 drawMonth(userMonth); // draw month calendar 104 105 displayDate(userMonth,userDay); // display date 106 } 107 108 //t displays current month 109 if(command.equals("t")){ 110 111 System.out.println(" This month"); 112 113 Date date = new Date(); // get current date 114 115 Calendar cal = Calendar.getInstance(); 116 117 int month = cal.get(Calendar.MONTH); // get current month 118 119 drawMonth(month+1); // since months index start from 0 i.e january =0, february = 1,.. so adding 1 120 121 displayDate(month+1,1); 122 123 console.close(); 124 125 } 126 127 //p displays past month 128 if(command.equals("p")){ 129 130 System.out.println(" Past month"); 131 132 drawMonth(month);//gets past month 133 134 displayDate(month, 0 ); //displays past month 135 136 } 137 //q quits the program 138 if(command.equals("q")){ 139 while(!command.equals("q")) { 140 141 } 142 } 143 } 144 } 145 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!