Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the last part of my calendar assignment that i am making in my intro programming class using java code in jGrasp. I

Need help with the last part of my calendar assignment that i am making in my intro programming class using java code in jGrasp. I will first post what needs to be added/changed to the code that i have already been working on. I would like for the calendar to have the same layout/look just have these features that are asked for to be added. Then i will post my actual code that i have so far.

Assignment

Task:

For the third part of the assignment, a few more pieces of functionality will be added. Each piece added will add a feature that applies one of the subjects covered lately in class. One such feature will be the ability for the calendar to store events for printing in the calendar squares. This feature will implement arrays. Another feature added will be functionality to read in events from a file and insert them into the events array. Finally, the calendar will be expanded to include the ability to print a month calendar draw to a file. This will use file output.

Task one:

Event Planning This task will involve adding a menu item to the menu of the calendar. When the command ev is entered, a new action should be started. The event planning action should prompt the user for an event. The event should be entered in the form of MM/DD event_title. After parsing the event, should be stored in a global array that will contain all events planned for that year. Event array should be a multidimensional array. It should be size 12(number of months in a year), with each sub array being large enough hold a single event for every day for the month. For example eventArray[11].length == 30 for year 2016. Once the event is parsed and stored, if the month calendar is drawn that contains scheduled events, those events should be presented in the calendar. If there is an event in a day, the title of the event should be placed within the blank space within the square of the day.

Task two:

File Reading Now that event planning is in place, if an event file exists, events should be read into the calendar when the calendar is first loaded. When the calendar starts, it will look for a file by the name calendarEvents.txt. If that file is in the same directory as the program, the calendar will read in the events in the file. Given the event that the event file does not exist, no events will be read into the events array. Either the given example events file or an events file by the same name of your creation can be used to populate the calendar events. The events in the file must be of the form as entered events from the keyboard (MM/DD event_title). Events from the file will be read into the same events array from the first task. Once the calendar is drawn, events for the month should be placed within the appropriate dates squares, just like in the first task.

Task three:

File printing For this task, the calendar will be printing the same thing that it might to the screen but this time it will be printing to a file. This includes the asci art for a month. Add a new command fp to the menu. Once this command has been entered, the user will be prompted to enter a month to print. After the month to print has been obtained, the program should proceed to ask the user for the name of a file to which it will print the calendar. The program will next proceed to print the calendar with the appropriate events into the file. Make sure the file is closed once writing is completed.

Style: It is important that you get used to writing code in good style. What is demonstrated in examples in class is considered good style. Additionally, you should look at the style guide located on Canvas. Badly styled code will lose points.

Here is the code i have so far:

import java.util.Calendar;

import java.util.Scanner;

public class Assignment2 {

// 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

private static String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// static value, will be set to find the user specified date.. otherwise will remain -1.

private static int dayValue = -1;

public static void drawMonth(int month, int daysToSkip, int maxDays) {

// calculate the near center where to display month number

int center = SIZE * 3;

String space = " ";

// 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("#####################################################################################################################");

// display blanks upto center

for (int i = 0; i < center; i++)

System.out.print(space);

// now display month number

System.out.print(month + " ");

// print week days

for(int i=0; i

System.out.printf("%-10s", days[i]);

}

System.out.println();

int start = 1;

// display the days

while (start <= maxDays) {

if(start == 1) {

// first row may need to skip some days

drawRow(start, maxDays, daysToSkip);

start += 7 - daysToSkip;

} else {

drawRow(start, maxDays, 0);

start += 7;

}

}

// 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 start, int maxDaysInMonth, int skip) {

// 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 will repeat for each

// cell

int day = start;

String space = " ";

System.out.print("|");

for (int cell = 1; cell <= 7; cell++) {

str = "";

if (cell > skip && day <= maxDaysInMonth) {

if(dayValue == day) {

str += " *" + day++ + "*";

} else {

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) {

int startingDay = getStartingDay(month);

int maxDays = getMaximumDays(month);

drawMonth(month, startingDay, maxDays);

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;

}

// get what is the starting day of the month

public static int getStartingDay(int month) {

Calendar cal = Calendar.getInstance();

cal.set(Calendar.DAY_OF_MONTH, 1);

// in calendar, month starts from 0

cal.set(Calendar.MONTH, month - 1);

return cal.get(Calendar.DAY_OF_WEEK) - 1;

}

// get what is the maximum no of days in the month

public static int getMaximumDays(int month) {

Calendar cal = Calendar.getInstance();

// in calendar, month starts from 0

cal.set(Calendar.MONTH, month - 1);

return cal.getActualMaximum(Calendar.DATE);

}

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();

dayValue = day = dayFromDate(date);

month = monthFromDate(date);

showLine = true;

drawCalendar(month, day);

continue;

} else if (cmd.equalsIgnoreCase("t")) {

Calendar cal = Calendar.getInstance();

dayValue = 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);

} else if (cmd.equalsIgnoreCase("n")) {

if (day != -1 && month != -1) {

day = 1;

dayValue = -1; // as user has not mentioned any value specifically

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;

}

} else if (cmd.equalsIgnoreCase("p")) {

if (day != -1 && month != -1) {

day = 1;

// as user has not mentioned any value specifically

dayValue = -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;

}

} else 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;

}

}

keybd.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions