Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the last part of my calendar. I cannot figure out how to print the events that are stored in the array

I need help with the last part of my calendar. I cannot figure out how to print the events that are stored in the array to the calendar. I will attach the assignment as well as my code and the text document to go with it. The most important feature I need help with is printing the events from the array to the calendar by either printing a marker on the days that have an event or printing the name of the event on the day.

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.

Text Document:

11/27 Cyber_Monday

08/01 Colorado_Day

12/31 New_Year's_Eve

01/03 J.R.R._Tolkien's_Birthday

02/7 e_Day

02/27 Pokemon_Day

05/05 Revenge_of_the_5th

06/23 Alan_Turning's_Birthday

11/10 Marine_Corps_Birthday

04/03 Take_Your_Robot_to_Work_Day

06/03 Cat_Video_Remembrance_day

05/01 Law_Day

09/28 Ask_a_Stupid_Question_Day

My program so far:

import java.io.*; import java.util.Scanner; import java.util.Calendar; import java.util.GregorianCalendar;

class calendarPartThree{

public static final int SIZE = 10; public static boolean eventMark = false; public static void main(String[] args)throws Exception{ String[][] events = new String[12][31]; boolean validDate = false; int daysInMonth = 0; int month = 13; int day = 32; int year = 1575; boolean mark = true; fileReading(events, year, month, day); // Scanner fileScan = new Scanner(new File("calendarEvents.txt")); // while(fileScan.hasNextLine()){ // String line = fileScan.nextLine(); // Scanner format = new Scanner(line); // format.useDelimiter("/| "); // month = format.nextInt(); // day = format.nextInt(); // String eventLine = format.next(); // events[month - 1][day - 1] = eventLine; // } // int menuRun = 1; while(menuRun > 0){ userMenu(validDate); Scanner userChoice = new Scanner(System.in); String command = userChoice.nextLine(); if(command.equals("e")){ Scanner enteredDate = new Scanner(System.in); System.out.print("What date would you like to look at?(mm/dd/yyyy):"); String date = enteredDate.nextLine(); Scanner lineScan = new Scanner(date); lineScan.useDelimiter("/"); month = lineScan.nextInt(); day = lineScan.nextInt(); year = lineScan.nextInt(); validDate = true; eCommand(events, validDate, year, month, day, daysInMonth, mark); }else if(command.equals("t")){ validDate = true; Calendar today = Calendar.getInstance(); year = today.get(Calendar.YEAR); month = today.get(Calendar.MONTH) + 1; day = today.get(Calendar.DATE); daysInMonth = maxDate(validDate, year, month, day); drawMonth(events, year, month, day, daysInMonth, mark);

}else if(command.equals("n") && validDate){ if(month == 12){ month = 1; year++; }else{ month++; } daysInMonth = maxDate(validDate, year, month, day); mark = false; drawMonth(events, year, month, day, daysInMonth, mark); }else if(command.equals("p") && validDate){ if(month == 1){ month = 12; year--; }else{ month--; } daysInMonth = maxDate(validDate, year, month, day); mark = false; drawMonth(events, year, month, day, daysInMonth, mark); }else if(command.equals("ev")){ evCommand(events, month, day); }else if(command.equals("fp")){ fpCommand(); }else if(command.equals("q")){ System.out.println("User has quit the program."); break; }else{ System.out.println("Please enter a valid command."); } } } public static void fileReading(String[][] events, int year, int month, int day)throws Exception{ Scanner fileScan = new Scanner(new File("calendarEvents.txt")); while(fileScan.hasNextLine()){ String line = fileScan.nextLine(); Scanner format = new Scanner(line); format.useDelimiter("/| "); month = format.nextInt(); day = format.nextInt(); String eventLine = format.next(); if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){ events[month - 1] = new String[31]; }else if(month == 4 || month == 6 || month == 9 || month == 11){ events[month - 1] = new String[30]; }else if(month == 2){ if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)){ events[month - 1] = new String[29]; }else{ events[month - 1] = new String[28]; } } events[month - 1][day - 1] = eventLine; } } /* userMenu prints out a menu for the user to select a command from. The userMenu method displays a calendar with an e, t, q command for the user to select from. If the e or t command is selected, the n and p command become available to the user. The e, t, p, and n commands print a calendar with the corresponding month/date and the q command ends the program. @param validDate A boolean that marks whether the date the user entered is valid or not. */ public static void userMenu(boolean validDate){ System.out.println("Please type a command."); System.out.println(" \"e\" to enter a date and display the corresponding calendar."); System.out.println(" \"t\" to get today's date and display today's calendar."); if(validDate){ System.out.println(" \"n\" to display the next month."); System.out.println(" \"p\" to display the previous month."); } System.out.println(" \"ev\" to access the event planning command."); System.out.println(" \"fp\" to access the file printing command."); System.out.println(" \"q\" to quit the program."); } /* An int return method that returns the number of days in the specified month. maxDate is an int return method that takes in the month entered by the user and returns the number of days in the given month. @param validDate A boolean that marks whether the date the user entered is valid or not. @param year An int representing the year the user entered. @param month An int representing the month the user entered. @param day An int representing the day the user entered. */ public static int maxDate(boolean validDate, int year, int month, int day){ int daysInMonth = 0; boolean leapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){ daysInMonth = 31; }else if(month == 4 || month == 6 || month == 9 || month == 11){ daysInMonth = 30; }else if(month == 2){ if(leapYear){ daysInMonth = 29; }else{ daysInMonth = 28; } } return daysInMonth; } /* eCommand is the method for when the user selects e from the menu. This method gets the date from the user and prints out a calendar with the corresponding date. @param validDate A boolean that marks whether the date the user entered is valid or not. @param year An int representing the year the user entered. @param month An int representing the month the user entered. @param day An int representing the day the user entered. @param daysInMonth An int representing the number of days in the given month. @param mark A boolean that represents whether or not there should be an asterik printed by the date. */ public static void eCommand(String[][] events, boolean validDate, int year, int month, int day, int daysInMonth, boolean mark){ daysInMonth = maxDate(validDate, year, month, day); if(month < 1 || month > 12 || day < 1 || day > daysInMonth || year < 1582){ System.out.println("Invalid date. Please try again and enter a valid date."); System.exit(0); } mark = true;

drawMonth(events, year, month, day, daysInMonth, mark); } public static void evCommand(String[][] events, int month, int day){ System.out.println("Please enter an event. (MM/DD event_title)"); Scanner evPlanner = new Scanner(System.in); // String line = evPlanner.nextLine(); // System.out.println(line); String eventLine = evPlanner.nextLine(); Scanner userScan = new Scanner(eventLine); userScan.useDelimiter("/| "); month = userScan.nextInt(); day = userScan.nextInt(); String input = userScan.next();

if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){ events[month - 1] = new String[31]; }else if(month == 4 || month == 6 || month == 9 || month == 11){ events[month - 1] = new String[30]; }else if(month == 2){ events[month - 1] = new String[28]; }else{ System.out.println("Please enter a valid date."); } events[month - 1][day - 1] = input; // System.out.println(events[month - 1][day - 1]); } public static void fpCommand(){ System.out.println(); } /* drawMonth is a method that prints out a calendar with the given parameters. The drawMonth method takes in the date that the user enters or the date for today and prints out a corresponding calendar. When the e or t command is pressed, the method prints an asterik by the date. If the n or p command is pressed, the method does no print an asterik by the date. @param year An int representing the year the user entered. @param month An int representing the month the user entered. @param day An int representing the day the user entered. @param daysInMonth An int representing the number of days in the given month. @param mark A boolean that represents whether or not there should be an asterik printed by the date. */ public static void drawMonth(String[][] events, int year, int month, int day, int daysInMonth, boolean mark){ Calendar cal = new GregorianCalendar(year, month, day); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); Calendar cal2 = new GregorianCalendar(year, month - 1, 1); int firstDay = cal2.get(Calendar.DAY_OF_WEEK); for(int x = 0; x < (SIZE * 7) / 2; x++){ System.out.print(" "); } System.out.print(month); System.out.println(); int start = 1; int end = 8; for(int b = 0; b < 5; b++){ for(int i = 0; i < SIZE * 7; i++){ System.out.print("="); } System.out.println(); for(int f = start; f < end; f++){ int r = f - firstDay + 1; if(r > 0 && r < 10){ if(day == r){ // if(mark && markEvent){ // System.out.print("| " + r + "*" + " " + "#"); // for(int a = 0; a < SIZE - 6; a++){ // System.out.print(" "); // } if(mark){ System.out.print("| " + r + "*"); for(int a = 0; a < SIZE - 4; a++){ System.out.print(" "); } }else{ System.out.print("| " + r); for(int a = 0; a < SIZE - 3; a++){ System.out.print(" "); } } // }else if(markEvent){ // System.out.print("| " + r + "#"); // for(int a = 0; a < SIZE - 4; a++){ // System.out.print(" "); // } }else{ System.out.print("| " + r); for(int a = 0; a < SIZE - 3; a++){ System.out.print(" "); } } }else if(r >= 10 && r <= daysInMonth){ if(day == r){ // if(mark && markEvent){ // System.out.print("| " + r + "*" + " " + "#"); // for(int a = 0; a < SIZE - 7; a++){ // System.out.print(" "); // } if(mark){ System.out.print("| " + r + "*"); for(int a = 0; a < SIZE - 5; a++){ System.out.print(" "); } }else{ System.out.print("| " + r); for(int a = 0; a < SIZE - 4; a++){ System.out.print(" "); } } // }else if(markEvent){ // System.out.print("| " + r + "#"); // for(int a = 0; a < SIZE - 5; a++){ // System.out.print(" "); // } }else{ System.out.print("| " + r); for(int a = 0; a < SIZE - 4; a++){ System.out.print(" "); } } }else{ System.out.print("| "); for(int a = 0; a < SIZE - 3; a++){ System.out.print(" "); } } } start = start + 7; end = end + 7; System.out.print("|"); System.out.println(); for(int d = 0; d < (SIZE - 1) / 2 - 1; d++){ for(int j = 1; j < 8; j++){ System.out.print("| "); // if(event[month - 1][day - 1] != null){ // System.out.println(); for(int a = 0; a < SIZE - 2; a++){ System.out.print(" "); } } System.out.print("|"); System.out.println(); }

} for(int k = 0; k < SIZE * 7; k++){ System.out.print("="); } System.out.println(); } }

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

Database Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions

Question

What is the relationship between humans and nature?

Answered: 1 week ago