Question: Need lots of help with my java programming, we need to make a calendar with a user menu, now i have the user menu made
Need lots of help with my java programming, we need to make a calendar with a user menu, now i have the user menu made and working however i just need some help with getting the months to show the correct number of days for each month, need each month to start on the correct day and the last thing i need is to have some type of mark pop up into the day that te user either inputs to look at or for todays date. my code that i have for the calendar is below, i want to keep the layout the same just have the minors tweaks in effect.
import java.util.Calendar;
import java.util.Scanner;
public class Assignment1 {
//sets the width of each cell in the calendar
private static int SIZE = 10;
private static char HORIZONTAL = '=';
private static char VERTICAL = '|';
private static boolean showLine = true;//to show the string date and month at end
public static void drawMonth(int month){
//calculate the near center where to display month number
int center = SIZE * 3;
String space = " ";
//display blanks upto center
System.out.println();
for(int i = 0; i< center; i++)
System.out.print(space);
System.out.println(" ");
//This is my ASCII art that i tried to make
System.out.println("#####################################################################################################################");
System.out.println("######################################## #######################################################################");
System.out.println("## ################# #### #### ## #### ######## ########################### ####### ####### ##");
System.out.println("## ### ### ############ ### ##### #### #### # ####### # ########################### ######## ##### ###");
System.out.println("## ### ################# ## ######## ## ##### ## ##### ## ############ ## ###### ######### ### ####");
System.out.println("## ### #### ## # ########## ###### ## ##### ## ## ## ## ##### # #####");
System.out.println("## ###### ### ####### ########## ####### ### ### ### ## #### ## ############ ########### ######");
System.out.println("## # ##### ## ######## # ######## ## # ### ### ### ### ## #### ## ############# ########### #######");
System.out.println("## ## #### ## ######## ## ###### #### ### #### # #### ## #### ## ############# ########### #######");
System.out.println("## ### ### ### ####### ### ##### ## #### #### # #### ## #### ## ############# ########### #######");
System.out.println("## #### ## #### ## #### ##### ## ### ##### ##### ## ## ############# ########### #######");
System.out.println("#####################################################################################################################");
System.out.println("#####################################################################################################################");
//now display month number
System.out.print(month + " ");
//display each of the 5 rows
for(int row = 1; row <= 5; row++){
drawRow(row);
}
//draw the final horizontal line with HORIZONTAL character
int width = SIZE * 7 ;
String str = "";
for(int i = 0; i <= width; i++)
str += HORIZONTAL;
System.out.println(str);
}
public static void drawRow(int row){
//total width of the row
String str = "";
int width = SIZE * 7 ;
//1st line: make string full of horizontal characters for total width and display it
for(int i = 0; i <= width; i++)
str += HORIZONTAL;
System.out.println(str);
//2nd line consist of vertical character followded by day number followed by padding spaces to match the size of cell
//this pattern of
int day;
String space = " ";
System.out.print("|");
for(int cell = 1; cell <= 7; cell++){
str = "";
//calculate the day that will displayed in cell
day = (row - 1) * 7 + cell;
if(day <= 31)
str += day;
//pad it with extra spaces to match SIZE
while(str.length() < SIZE - 1)
str += space;
str += VERTICAL;
System.out.print(str);
}
//now remaining lines will be similar to above but the day number is not there... only spaces padded to match cell SIZE
// the pattern for each cell is
int height = SIZE/2;
for(int h = 2; h <= height; h++){
System.out.print(" |");
for(int cell = 1; cell <= 7; cell++){
str = "";
while(str.length() < SIZE - 1)
str += space;
str += VERTICAL;
System.out.print(str);
}
}
System.out.print(" ");
}
public static void displayDate(int month, int day){
if(showLine == true){
System.out.println("Month: " + month);
System.out.println("Day: " + day);
}
}
public static int monthFromDate(String date){
//split the date into 2 based on / delimiter
String tokens[] = date.split("/");
//convert 1st token and return as month
return Integer.parseInt(tokens[0]);
}
public static int dayFromDate(String date){
//split the date into 2 based on / delimiter
String tokens[] = date.split("/");
//convert 2nd token and return as day
return Integer.parseInt(tokens[1]);
}
public static void drawCalendar(int month, int day){
drawMonth(month);
displayDate(month, day);
}
// method to correctly return the next month
public static int nextGoodMonth(int month){
int mt = month;
if(month == 12)
return 1;
return mt + 1;
}
// method to correctly return the previous month
public static int previousGoodMonth(int month){
int mt = month;
if(month == 1)
return 12;
return mt - 1;
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
String cmd = "";
int day = -1;
int month = -1;
while(true){
System.out.println("Please type a command");
System.out.println("\t\"e\" to enter a date and display the corresponding calendar.");
System.out.println("\t\"t\" to get todays date and display the todays calendar");
System.out.println("\t\"n\" to display the next month");
System.out.println("\t\"p\" to display the previous month");
System.out.println("\t\"q\" to quit the progrram");
cmd = keybd.nextLine();
if(cmd.equalsIgnoreCase("e")){
//get user input date
System.out.print("Enter a date (m/d): ");
String date = keybd.nextLine();
day = dayFromDate(date);
month = monthFromDate(date);
showLine = true;
drawCalendar(month, day);
continue;
}
if(cmd.equalsIgnoreCase("t")){
Calendar cal = Calendar.getInstance();
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH) + 1;
System.out.println(" Displaying calendar for today's date " + month + "/" + day);
showLine = true;
drawCalendar(month, day);
}
if(cmd.equalsIgnoreCase("n")){
if(day!=-1 && month != -1){
Calendar cal = Calendar.getInstance();
day = 1;
month = nextGoodMonth(month);
System.out.println(" Displaying calendar for next month " + month );
showLine = false;
drawCalendar(month, day);
continue;
}
else{
System.out.println("Please display a calendar first. ");
continue;
}
}
if(cmd.equalsIgnoreCase("p")){
if(day!=-1 && month != -1){
Calendar cal = Calendar.getInstance();
day = 1;
month = previousGoodMonth(month);
System.out.println(" Displaying calendar for previous month " + month);
showLine = false;
drawCalendar(month, day);
continue;
}
else{
System.out.println("Please display a calendar first. ");
continue;
}
}
if(cmd.equalsIgnoreCase("q")){
System.out.println("Thank you for using the calendar app.");
System.out.println("Have a nice day.");
break;
}
else{
System.out.println("Please enter a valid command. ");
continue;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
