Fix the code

Assignment Help JAVA Programming
Reference no: EM131588912

1. Assignment detail

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 2017
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]oadThe 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 byUser 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) 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 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
Tuesday, Feb 27, 2017
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 2017
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]reateThis 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. Also, let's assume there is no conflict between events that user entered, and therefore your program doesn't have to check 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 toWith 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 listThe 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:
2017
Friday March 17 13:15 - 14:00 Dentist
Tuesday April 25 15:00 - 16:00 Job Interview
2017
Friday June 2 17:00 Leave for Korea

[D]eleteUser can delete an event from the Calendar. There are two different ways to delete an event: Selected and All. Other type of deletion will not be considered for simplicity.
[S]elected: all the events scheduled on the selected date will be deleted.
[A]ll: all the events scheduled on this calendar will be deleted.
[S]elected or [A]ll ?

If the user enters s, then the calendar asks for the date as shown below.
Enter the date.

06/03/2016

[Q]uit saves all the events scheduled in a text file called "events.txt" in the order of starting date and starting time. You may use Java serialization if you don't want to persist data in a text 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.

--------CODE---------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalenderAssignment {
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[] tmp = getDayDate(new Date());
year = tmp[0];
month = tmp[1];
day = tmp[2];
date = tmp[3];
}
private static int[] getDayDate(Date dateObj) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateObj);
int[] tmp = new int[4];
tmp[0] = cal.get(Calendar.YEAR);
tmp[1] = cal.get(Calendar.MONTH) + 1;
tmp[2] = cal.get(Calendar.DAY_OF_MONTH);
tmp[3] = cal.get(Calendar.DATE);
return tmp;
}
private static List<String> eventsList;
public static void main(String[] args) throws Exception {
String option = "";
while (!"Q".equalsIgnoreCase(option)) {
System.out
.println("Select one of the following options:n[L]oad [V]iew by [C]reate, [G]o to [E]vent list [D]elete [Q]uitn");
printMonth(year, month, date);
option = reader.readLine();
switch (option) {
case "L":
load();
break;
case "V":
view();
break;
case "C":
create();
break;
case "G":
System.out.println("Enter the date : ");
String s = reader.readLine();
go(s);
break;
case "E":
printEvents("N/A");
break;
case "D":
delete();
break;
case "Q":
return;
default:
System.out.println("Wrong option please select valid one!.");
break;
}
}
}
private static void load() {
if (fileReader == null) {
System.out
.println("nThis is first time run!. Nothing is there!n");
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("This is first time run!. Nothing is there!");
}
} else {
eventsList.clear();
String line = null;
try {
while ((line = fileReader.readLine()) != null) {
System.out.println(line);
eventsList.add(line);
}
System.out.println("nnEvents are loaded successfully!.n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void view() throws Exception {
System.out.println("[D]ay view or [M]view ?");
String op = reader.readLine();
String searchStr = getMonthName(month);
if ("D".equalsIgnoreCase(op)) {
System.out.println(getCurrentDateDetails());
searchStr += getDate();
}
printEvents(searchStr);
System.out.println("[P]revious or [N]ext or [M]ain menu ?");
return;
}
private static void printEvents(String searchStr) {
boolean check = true;
if (searchStr.equalsIgnoreCase("N/A")) {
check = false;
}
for (int i = 0; i < eventsList.size(); i++) {
if (check && eventsList.get(i).contains(searchStr)) {
System.out.println(eventsList.get(i));
}
}
}
private static String getCurrentDateDetails() {
return getDayName(day) + "," + getMonthName(month) + " " + getDate()
+ "," + getYear();
}
private static void create() throws Exception {
System.out.println("Enter a Title: ");
String title = reader.readLine();
System.out.println("Enter a date: ");
String date = reader.readLine();
System.out.println("Enter a Timing: ");
String timing = reader.readLine();
System.out.println("Enter a Event: ");
String event = reader.readLine();
SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yyyy");
Date d = sdf.parse(date);
int[] tmp = getDayDate(d);
int yearLocal = tmp[0];
String monthLocal = getMonthName(tmp[1]);
String dayLocal = getDayName(tmp[2]);
int dateLocal = tmp[3];
date = yearLocal + " " + dayLocal + " " + monthLocal + " " + dateLocal
+ " " + timing + " " + event;
eventsList.add(date);
filewriter.write(date);
}
private static void go(String date) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yyyy");
Date d = sdf.parse(date);
int[] tmp = getDayDate(d);
String monthLocal = getMonthName(tmp[1]);
int dateLocal = tmp[3];
String searchStr = monthLocal + " " + dateLocal;
printEvents(searchStr);
System.out.println("");
}
private static 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[] tmp = getDayDate(d);
String monthLocal = getMonthName(tmp[1]);
int dateLocal = tmp[3];
String searchStr = monthLocal + " " + dateLocal;
for (int i = 0; i < eventsList.size(); i++) {
if (eventsList.get(i).contains(searchStr)) {
eventsList.remove(i);
}
}
}
}
private static int getDay() {
return day;
}
private static int getMonth() {
return month;
}
private static int getYear() {
return year;
}
private static int getDate() {
return date;
}
private/** Print the calendar for a month in a year */
static void printMonth(int year, int month, int todayDate) {
// Get start day of the week for the first date in the month
int startDay = getStartDay(year, month);
// Get number of days in the month
int numOfDaysInMonth = getNumOfDaysInMonth(year, month);
// Print headings
printMonthTitle(year, month);
// Print body
printMonthBody(startDay, numOfDaysInMonth, todayDate);
}
/** Get the start day of the first day in a month */
static int getStartDay(int year, int month) {
// Get total number of days since 1/1/1800
int startDay1800 = 3;
long totalNumOfDays = getTotalNumOfDays(year, month);
// Return the start day
return (int) ((totalNumOfDays + startDay1800) % 7);
}
/** Get the total number of days since Jan 1, 1800 */
static long getTotalNumOfDays(int year, int month) {
long total = 0;
// Get the total days from 1800 to year -1
for (int i = 1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
// Add days from Jan to the month prior to the calendar month
for (int i = 1; i < month; i++)
total = total + getNumOfDaysInMonth(year, i);
return total;
}
/** Get the number of days in a month */
static int getNumOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if (month == 2)
if (isLeapYear(year))
return 29;
else
return 28;
return 0; // If month is incorrect.
}
/** Determine if it is a leap year */
static boolean isLeapYear(int year) {
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
return false;
}
/** Print month body */
static void printMonthBody(int startDay, int numOfDaysInMonth, int todayDate) {
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numOfDaysInMonth; i++) {
String tmp = String.valueOf(i);
String space = "";
if (i == todayDate) {
tmp = "[" + tmp;
}
if (i < 10) {
if (tmp.contains("[")) {
space = " ";
} else {
space = " ";
}
} else {
if (tmp.contains("[")) {
space = " ";
} else {
space = " ";
}
}
System.out.print(space + tmp);
if (i == todayDate) {
System.out.print("]");
}
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
/** Print the month title, i.e. May, 1999 */
static void printMonthTitle(int year, int month) {
System.out.println(" " + getMonthName(month) + ", " + year);
System.out.println("-----------------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/** Get the English name for the 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;
}
static String getDayName(int dayLocal) {
String dayName = null;
switch (dayLocal) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednessday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
}
return dayName;
}
}

Reference no: EM131588912

Questions Cloud

Has the criminal justice policy been modified over the years : Your initial discussion thread is due on Day 3 (Thursday) and you have until Day 7 (Monday) to respond to your classmates.
Future state of the us economy : Based on what you have learned in this course so far and what you have read from outside sources or other publicly available information
What would happen to consumption : What would happen to consumption, saving, and work if the government borrowed money to wage war and only paid the interest payments
Major features of monopolistic competition : 1. Describe the major features of monopolistic competition compared to pure competition and pure monopoly.
Fix the code : 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
Explain the criminal justice policy : Your initial discussion thread is due on Day 3 (Thursday) and you have until Day 7 (Monday) to respond to your classmates.
Equilibrium price of a weezil : The following are the equations for the supply and demand curves in the market for weezils:
Germany to the notion of moral hazard : Relate the options available to creditor countries like Germany to the notion of MORAL HAZARD.
Prepare a presentation on unconstrained array types : Prepare a presentation on given section. Unconstrained Array Types - The array types we have seen so far in this chapter are called constrained arrays

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd