Question: Fix my java code please! Pretty much I have 3 classes MyCalenderTester, MyCalendar, and Events. The events class has all of the important code in

Fix my java code please! Pretty much I have 3 classes MyCalenderTester, MyCalendar, and Events. The events class has all of the important code in it. So far I have Load working, View By does NOT work I need this fixed please, Create is bugged (when I create the second event it glitches out, GoTo works, EventList works but the events are not sorted in order, Delete works, and Quit works by saving the file but it makes it look wierd. Thank you so much for your help!

In this assignment, you will design and implement a calendar similar to one you can find in your phone. The calendar is going to be implemented as a console application.

The initial screen shows the current month looking like this. It also highlights today's date, for example, using a pair of brackets. (It is not straightforward to highlight on console. It is fine to use a pair of brackets for this purpose.)

 February 2018 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [27] 28 <--- it is ok if [27] is sticking out. 

The initial screen comes with a main menu with following options: View by, Create, Go to, Event list, Delete, and Quit. After the function of an option is done, the main menu is displayed again for the user to choose the next option.

Select one of the following options: [L]oad [V]iew by [C]reate, [G]o to [E]vent list [D]elete [Q]uit 

The user may enter one of the letter highlighted with a pair of bracket to choose an option. For example,

V 

will choose the View by option.

[L]oad The system loads events.txt to populate the calendar. If there is no such file because it is the first run, the load function prompts a message to the user indicating this is the first run. You may use Java serialization this function.

[V]iew by User can choose a Day or a Month view. If a Day view is chosen, the calendar displays the current date. If there is an event(s) scheduled on that day, display them in the order of start time of the event. With a Month view, it displays the current month and highlights day(s) with a pair of brackets {} if any event scheduled on that day. After a view is displayed, the calendar gives the user three options: P, N, and M, where P, N, and M stand for previous, next, and main menu, respectively. The previous and next options allow the user to navigate the calendar back and forth by day if the calendar is currently in a day view or by month if it is in a month view. If the user selects M, navigation is done, and the user gets to access the main menu again.

[D]ay view or [M]view ? 

If the user selects D, then

Wednesday, Feb 21, 2018 Dr. Kim's office hour 9:15 - 10:15 [P]revious or [N]ext or [M]ain menu ? 

If the user selects M, then

 February 2018 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 {21} 22 23 24 25 26 27 28 [P]revious or [N]ext or [M]ain menu ? 

[C]reate This option allows the user to schedule an event. The calendar asks the user to enter the title, date, starting time, and ending time of an event. For simplicity, we consider one day event only. Your program should check if if a new event is conflict with existing events. Please stick to the following format to enter data:

Title: a string (doesn't have to be one word)

date: MM/DD/YYYY

Starting time and ending time: 24 hour clock such as 06:00 for 6 AM and 15:30 for 3:30 PM. The user may not enter ending time if an ending time doesn't make sense for the event (e.g. leaving for Korea event may have a starting time but no ending time.)

[G]o to With this option, the user is asked to enter a date in the form of MM/DD/YYYY and then the calendar displays the Day view of the requested date including any event scheduled on that day in the order of starting time.

[E]vent list The user can browse scheduled events. The calendar displays all the events scheduled in the calendar in the order of starting date and starting time. An example presentation of events is as follows:

2018 Wednesday Feb 21 9:15 - 10:15 Dr. Kim's office hour Saturday March 17 13:15 - 14:00 Dentist Thursday April 26 15:00 - 16:00 Job Interview 2019 Friday June 2 17:00 Leave for Korea 

[D]elete User can delete an event from the Calendar. User will be first asked to enter a specific date, e.g. 06/03/2018. And then will be asked to choose from two options: [S]elected and [A]ll.

[S]elected: selected event will be deleted from the specified date.

[A]ll: all the events scheduled on the specified date will be deleted.

[Q]uit saves all the events scheduled in a text file called "events.txt" in the order of starting date and starting time. (If you know how to use Java serialization, use it. Otherwise, get the data using accessors and write them to the file.

The main menu will be displayed after each option is done. It is crucial to have a user friendly interface for the user to enter input. For example, if the calendar needs a date from the user, suggest a specific format of the date for the user to use. Our class grader will be the user to operate your calendar, and you don't want to frustrate the user with a confusing interface.

import java.util.GregorianCalendar;

public class MyCalendarTester extends MyCalendar {

public static void main(String[] args) {

GregorianCalendar cal = new GregorianCalendar(); // capture today

System.out.print("Today: ");

printCalendar(cal);

doAction();

}

}

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Scanner;

enum MONTHS

{

Jan, Feb, March, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec;

}

enum DAYS

{

Sun, Mon, Tue, Wed, Thur, Fri, Sat ;

}

public class MyCalendar extends Events{

public static void printCalendar(Calendar c)

{ MONTHS[] arrayOfMonths = MONTHS.values();

DAYS[] arrayOfDays = DAYS.values();

System.out.print(arrayOfDays[c.get(Calendar.DAY_OF_WEEK)-1]);

System.out.print(" ");

System.out.print(arrayOfMonths[c.get(Calendar.MONTH)]);

System.out.print(" ");

System.out.print(c.get(Calendar.DAY_OF_MONTH));

System.out.println(" ");

GregorianCalendar temp = new GregorianCalendar(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1);

System.out.println("The first day of this month is " + arrayOfDays[temp.get(Calendar.DAY_OF_WEEK)-1]);

System.out.println(" " + arrayOfMonths[c.get(Calendar.MONTH)] + " " + c.get(Calendar.YEAR));

System.out.println("Su Mo Tu We Th Fr Sa");

int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);

for(int i = 0; i < temp.get(Calendar.DAY_OF_WEEK -1 ) % 7; i++){

System.out.print(" ");

}

for(int i = 1; i <= days; i++){

if( i == c.get(Calendar.DAY_OF_MONTH)){

System.out.print("[" + i + "]");

}

else{

System.out.printf("%3d ", i);

}

if((i + temp.get(Calendar.DAY_OF_WEEK -1) ) % 7 == 0 || i == days){

System.out.println();

}

}

}

public static void doAction(){

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

Scanner sc = new Scanner(System.in);

String choose = " ";

Events eventAction = new Events();

while (!("Q".equalsIgnoreCase(choose))) {

if (!("Q".equalsIgnoreCase(choose))

&& !("L".equalsIgnoreCase(choose))

&& !("V".equalsIgnoreCase(choose))

&& !("C".equalsIgnoreCase(choose))

&& !("G".equalsIgnoreCase(choose))

&& !("E".equalsIgnoreCase(choose))

&& !("D".equalsIgnoreCase(choose))

&& !(" ".equalsIgnoreCase(choose))) {

System.out.println("Not an option please select: L,V,C,G,E,D, or Q");

}

choose = sc.nextLine();

if ("L".equalsIgnoreCase(choose)) {

eventAction.load();

}

if ("V".equalsIgnoreCase(choose)) {

try {

eventAction.view();

} catch (Exception e) {

e.printStackTrace();

}

}

if ("C".equalsIgnoreCase(choose)) {

try {

eventAction.create();

} catch (Exception e) {

e.printStackTrace();

}

}

if ("G".equalsIgnoreCase(choose)) {

try {

eventAction.goTo();

} catch (Exception e) {

e.printStackTrace();

}

}

if ("E".equalsIgnoreCase(choose)) {

eventAction.printAll();

}

if ("D".equalsIgnoreCase(choose)) {

try {

eventAction.delete();

} catch (Exception e) {

e.printStackTrace();

}

}

}

eventAction.saveQuit();

System.out.println("You have quit!");

}

}

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.List;

public class Events {

private static BufferedReader fileReader;

private static BufferedWriter filewriter;

private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

private static int year;

private static int month;

private static int day;

private static int date;

static {

int[] arr = getDayDate(new Date());

year = arr[0];

month = arr[1];

day = arr[2];

date = arr[3];

}

GregorianCalendar cal = new GregorianCalendar(); // capture today

MONTHS[] arrayOfMonths = MONTHS.values();

DAYS[] arrayOfDays = DAYS.values();

private static int[] getDayDate(Date dateObj) {

Calendar cal = Calendar.getInstance();

cal.setTime(dateObj);

int[] arr = new int[4];

arr[0] = cal.get(Calendar.YEAR);

arr[1] = cal.get(Calendar.MONTH) + 1;

arr[2] = cal.get(Calendar.DAY_OF_MONTH);

arr[3] = cal.get(Calendar.DATE);

return arr;

}

public List eventsList = new ArrayList();

/**

* The system loads events.txt to populate the calendar. If there is no such file because

* it is the first run, the load function prompts a message to the user indicating this

* is the first run.

*/

public void load() {

if (fileReader == null) {

System.out.println("First run. File is empty");

try {

File file = new File("event.txt");

file.createNewFile();

fileReader = new BufferedReader(new FileReader(file));

filewriter = new BufferedWriter(new FileWriter(file));

} catch (Exception e) {

System.out.println("Empty");

}

} else {

eventsList.clear();

String line = null;

try {

while ((line = fileReader.readLine()) != null) {

System.out.println(line);

eventsList.add(line);

}

System.out.println("Events are loaded");

} catch (Exception e) {

e.printStackTrace();

}

}

}

public void view() throws Exception {

int count = 0;

System.out.println("[D]ay view or [M]view ?");

String one = reader.readLine();

String searchStr = null;

if ("D".equalsIgnoreCase(one)) {

System.out.println(getCurrentDay(count));

}

if ("M".equalsIgnoreCase(one)){

System.out.println(getCurrentMonth(count));

}

String two = "";

while(!("M".equalsIgnoreCase(two))){

System.out.println("[P]revious or [N]ext or [M]ain menu ?");

two = reader.readLine();

if("D".equalsIgnoreCase(one) && "P".equalsIgnoreCase(two)){

count--;

System.out.println(getCurrentDay(count));

searchStr += getDate(count);

printEvents(searchStr);

}

if("D".equalsIgnoreCase(one) && "N".equalsIgnoreCase(two)){

count++;

System.out.println(getCurrentDay(count));

searchStr += getDate(count);

printEvents(searchStr);

}

if("M".equalsIgnoreCase(one) && "P".equalsIgnoreCase(two)){

count--;

System.out.println(getCurrentMonth(count));

searchStr += getMonth(count);

printEvents(searchStr);

}

if("M".equalsIgnoreCase(one) && "N".equalsIgnoreCase(two)){

count++;

System.out.println(getCurrentMonth(count));

searchStr += getMonth(count);

printEvents(searchStr);

}

}

}

public void printEvents(String searchStr) {

for (int i = 0; i < eventsList.size(); i++) {

if (eventsList.get(i).contains(searchStr)) {

System.out.println(eventsList.get(i));

}

}

}

public void printAll(){

if(eventsList.isEmpty()){

System.out.println("No events planned");

}

for (int i = 0; i < eventsList.size(); i++) {

System.out.println(eventsList.get(i));

}

}

public String getCurrentDay(int count) {

return arrayOfDays[cal.get(Calendar.DAY_OF_WEEK)-1] + " " + arrayOfMonths[cal.get(Calendar.MONTH)] + " " + cal.get(Calendar.DAY_OF_MONTH)

+ " " + getYear();

}

public String getCurrentMonth(int count){

return arrayOfMonths[cal.get(Calendar.MONTH)] + " " + getYear();

}

public void create() throws Exception {

if (filewriter == null) {

try {

File file = new File("event.txt");

filewriter = new BufferedWriter(new FileWriter(file));

} catch (Exception e) {

System.out

.println("This is first time run!. Nothing is here!");

}

}

boolean noTimeConflict = true;

System.out.println("Enter a Name: ");

String title = reader.readLine();

System.out.println("Enter a date: ");

String date = reader.readLine();

System.out.println("Enter a Timing: ");

int time = reader.read();

ArrayList times = new ArrayList();

for (int i = 0; i < times.size(); i++) {

if(time == times.get(i)){

System.out.println("This time is already taken up! ");

noTimeConflict = false;

}

}

times.add(time);

SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yyyy");

Date d = sdf.parse(date);

int[] arr = getDayDate(d);

int yearLocal = arr[0];

String monthLocal = getMonthName(arr[1]);

int dateLocal = arr[3];

date = yearLocal + " " + monthLocal + " " + dateLocal

+" " + title+ " " + time;

if(noTimeConflict == true){

eventsList.add(date);

filewriter.write(date);

}

}

public void saveQuit() {

try {

File file = new File("events.txt");

FileWriter writer = new FileWriter(file.getAbsoluteFile());

BufferedWriter buffer = new BufferedWriter(writer);

for (int i = 0; i < eventsList.size(); i++) {

buffer.write(eventsList.get(i));

System.out.println();

}

buffer.close();

} catch (IOException ex) {

System.out.println("Error writing");

}

}

public void goTo() throws Exception {

String date;

System.out.println("Enter a date: ");

date = reader.readLine();

SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yyyy");

Date d = sdf.parse(date);

int[] arr = getDayDate(d);

String monthLocal = getMonthName(arr[1]);

int dateLocal = arr[3];

String searchStr = monthLocal + " " + dateLocal;

printEvents(searchStr);

}

public void delete() throws Exception {

System.out.println("[S]elected or [A]ll ?");

String op = reader.readLine();

if ("S".equalsIgnoreCase(op)) {

System.out.println("Enter date : ");

String date = reader.readLine();

SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yyyy");

Date d = sdf.parse(date);

int[] arr = getDayDate(d);

String month = getMonthName(arr[1]);

int day = arr[3];

String searchStr = month + " " + day;

for (int i = 0; i < eventsList.size(); i++) {

if (eventsList.get(i).contains(searchStr)) {

eventsList.remove(i);

}

}

}

if ("A".equalsIgnoreCase(op)) {

for (int i = 0; i < eventsList.size(); i++) {

eventsList.remove(i);

}

}

}

private static int getYear() {

return year;

}

private static int getDate(int count) {

date += count;

return date;

}

private static int getMonth(int count){

month += count;

return month;

}

static String getMonthName(int month) {

String monthName = null;

switch (month) {

case 1:

monthName = "January";

break;

case 2:

monthName = "February";

break;

case 3:

monthName = "March";

break;

case 4:

monthName = "April";

break;

case 5:

monthName = "May";

break;

case 6:

monthName = "June";

break;

case 7:

monthName = "July";

break;

case 8:

monthName = "August";

break;

case 9:

monthName = "September";

break;

case 10:

monthName = "October";

break;

case 11:

monthName = "November";

break;

case 12:

monthName = "December";

}

return monthName;

}

}

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!