Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please someone please fix my code I'm having issues with the [ private static void load() throws FileNotFoundException ] method. I can't get this method

Please someone please fix my code

I'm having issues with the [ private static void load() throws FileNotFoundException ] method. I can't get this method to properly parse through my txt file. I have asked people on chegg so many times but it never works so please copy this code and test it yourself before answering so that I can get the right answer.

This is my events.txt file that I'm using when running this code:

**AGAIN PLEASE RUN THE CODE YOURSELF BEFORE ANSWERING TO ENSURE CORRECTNESS**

CS151 TR 9:00 10:15 1/25/23 5/20/23 CS146 MW 12:00 13:15 1/25/23 5/20/23 Math39 MW 9:00 10:15 1/25/23 5/22/23 Music TR 12:00 13:15 1/26/23 5/19/23 Anthropology F 12:00 13:15 1/25/23 5/15/23 Interview R 9:30 11:30 9/28/23 9/28/23 Dentist T 16:15 17:00 10/3/23 10/2/23 Warriors A 18:00 22:00 03/04/23 03/04/23 Dinner F 16:00 18:00 03/24/23 03/24/23 Haircut A 15:00 16:00 02/24/23 02/24/23 Event 1 MW 10:00 11:30 2/21/23 5/16/23 Event 2 T 13:00 15:00 3/1/23 5/10/23 

MyCalendarTester

import java.io.File; import java.io.FileNotFoundException; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.*; public class MyCalendarTester { static LocalDate cal = LocalDate.now(); static MyCalendar myCal = new MyCalendar(); static Scanner sc = new Scanner(System.in); static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("M/d/yy"); static final DateTimeFormatter fullYearFormatter = DateTimeFormatter.ofPattern("M/d/y"); public static void main(String[] args) throws FileNotFoundException{ printCalendar(cal); System.out.println(); boolean check = true; while (check) { printMenu(); char input = sc.next(".").charAt(0); switch (input) { case 'L': load();break; case 'V': viewBy();break; case 'C': create();break; case 'G': goTo();break; case 'E': eventList();break; case 'D': delete();break; case 'Q': check = false;quit();break; default: System.out.println("Please input the correct option!");break; } } } private static void printMenu() { String[] options = {"[L]oad", "[V]iew by", "[C]reate", "[G]o to", "[E]vent list", "[D]elete", "[Q]uit"}; System.out.println("Select one of the following options:"); for (int i = 0; i < options.length; i++) { System.out.print(options[i] + " "); } System.out.println(); } private static void printCalendar(LocalDate a){ String[] weekdays = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}; Month month = a.getMonth(); System.out.println("\t" + month + " " + a.getYear()); for (int i = 0; i < weekdays.length; i++) { System.out.print(weekdays[i] + "\t"); } System.out.println(); LocalDate firstDay = LocalDate.of(a.getYear(), a.getMonth(), 1); int order = firstDay.getDayOfWeek().getValue(); for (int j = 0; j < order; j++) { System.out.print("\t"); } for (int i = 1; i <= month.length(a.isLeapYear()); i++) { if ((i + order - 1)%7 == 0) { System.out.println(); } LocalDate iteratedDay = LocalDate.of(a.getYear(), month, i); if (i == cal.getDayOfMonth() && month == cal.getMonth() && myCal.hasEvent(iteratedDay)) { System.out.print("[{" + i + "}] "); } else if (i == cal.getDayOfMonth() && month == cal.getMonth()) { // case 2 System.out.print("[" + i + "] "); } else if (myCal.hasEvent(iteratedDay)) { System.out.print("{" + i + "} "); }else{ if (i < 10) System.out.print(" " + i + " "); else System.out.print(i + " "); } } } private static void load() throws FileNotFoundException { 
 Scanner fin = new Scanner(new File("events.txt")); // create a map to map day abbreviations to DayOfWeek values Map dayMap = new HashMap<>(); dayMap.put('M', DayOfWeek.MONDAY); dayMap.put('T', DayOfWeek.TUESDAY); dayMap.put('W', DayOfWeek.WEDNESDAY); dayMap.put('R', DayOfWeek.THURSDAY); dayMap.put('F', DayOfWeek.FRIDAY); dayMap.put('A', DayOfWeek.SATURDAY); dayMap.put('S', DayOfWeek.SUNDAY); while (fin.hasNextLine()) { String name = fin.nextLine(); String dayString = fin.next(); String timeString = fin.nextLine().trim(); String[] dayParts = dayString.split(" "); String[] timeParts = timeString.split(" "); // parse the start and end times LocalTime startTime = LocalTime.parse(timeParts[0], DateTimeFormatter.ofPattern("HH:mm")); LocalTime endTime = LocalTime.parse(timeParts[1], DateTimeFormatter.ofPattern("HH:mm")); TimeInterval interval = new TimeInterval(startTime, endTime); // loop through each date in the range specified in the input LocalDate startDate = LocalDate.parse(dayParts[2], DateTimeFormatter.ofPattern("M/d/yy")); LocalDate endDate = LocalDate.parse(dayParts[3], DateTimeFormatter.ofPattern("M/d/yy")); for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) { // create an event for this date char dayChar = dayParts[0].charAt(0); DayOfWeek dayOfWeek = dayMap.get(dayChar); LocalDate eventDate = date.with(dayOfWeek); Event event = new Event(name, eventDate, interval); myCal.add(event); } } } 
 private static void viewBy() { System.out.println("[D]ay view or [M]onth view ?"); char decision = sc.next(".").charAt(0); LocalDate flexDay = cal; if (decision == 'D') { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d YYYY"); while (decision != 'G') { System.out.println(" " + formatter.format(flexDay)); if (myCal.getEvents(flexDay)!=null) { for (Event event : myCal.getEvents(flexDay)) { System.out.println(event.getName() + " : " + event.getTime()); } } System.out.println("[P]revious or [N]ext or [G]o back to main menu ?"); decision = sc.next(".").charAt(0); if (decision == 'P') { flexDay = flexDay.minusDays(1); } else if (decision == 'N') { flexDay = flexDay.plusDays(1); } } } else if (decision == 'M') { while (decision != 'G') { printCalendar(flexDay); System.out.println(); System.out.println("[P]revious or [N]ext or [G]o back to main menu ?"); decision = sc.next(".").charAt(0); if (decision == 'P') { flexDay = flexDay.minusMonths(1); } else if (decision == 'N') { flexDay = flexDay.plusMonths(1); } } } } private static void create() { System.out.print("Enter title: "); sc.nextLine(); String name = sc.nextLine(); System.out.print("Enter date MM/DD/YYYY: "); String date = sc.next(); LocalDate day = LocalDate.parse(date, fullYearFormatter); System.out.print("Enter start time: "); String start = sc.next(); System.out.print("Enter end time: "); String end = sc.next(); TimeInterval timeInterval = new TimeInterval(LocalTime.parse(start, DateTimeFormatter.ISO_LOCAL_TIME), LocalTime.parse(end, DateTimeFormatter.ISO_LOCAL_TIME)); if (myCal.getEvents(day) == null) { Event event = new Event(name, day, timeInterval); myCal.add(event); } else { if (myCal.hasEvent(day)) { boolean conflict = false; for (Event events : myCal.getEvents(day)) { boolean correct = events.getTime().conflict(timeInterval); if (correct) { System.out.println("CONFLICT! UNABLE TO ADD THE EVENT."); conflict = true; break; } } if (!conflict) { Event event = new Event(name, day, timeInterval); myCal.add(event); } } } } private static void goTo() { System.out.print("Enter the date [mm/dd/yyyy]: "); String date = sc.next(); LocalDate day = LocalDate.parse(date, fullYearFormatter); for (Event event : myCal.getEvents(day)) { System.out.println(event.getName() + ": " + event.getTime()); } } private static void eventList() { DateTimeFormatter specialFormat = DateTimeFormatter.ofPattern("eeee MMMM d"); LocalDate start = LocalDate.of(cal.getYear(), 1, 1); LocalDate end = LocalDate.of(cal.getYear(), 12, 31); for (LocalDate day = start; day.isBefore(end); day = day.plusDays(1)) { if (myCal.hasEvent(day)) { for (Event event : myCal.getEvents(day)) { System.out.println(specialFormat.format(day) + " " + event.getTime() + " " + event.getName()); } } } } private static void delete() { System.out.println("[S]elected or [A]ll?"); char decision = sc.next(".").charAt(0); if (decision == 'S') { System.out.println("Enter the date [mm/dd/yyyy]"); sc.nextLine(); String dayInput = sc.nextLine(); LocalDate day = LocalDate.parse(dayInput, fullYearFormatter); if (myCal.hasEvent(day)) { for (Event event : myCal.getEvents(day)) { System.out.println(event.getTime() + " " + event.getName()); } } System.out.print("Enter the name of the event to delete:"); String nameInput = sc.nextLine(); Event target = null; for (Event event : myCal.getEvents(day)) { if (nameInput.equals(event.getName())) { target = event; break; } } if (target != null) { myCal.getEvents(day).remove(target); System.out.println("The event is deleted. Here is the current scheduled event: "); System.out.println(dayInput); for (Event event : myCal.getEvents(day)) { System.out.println(event.getTime() + " " + event.getName()); } } } else if (decision == 'A') { System.out.println("Enter the date [mm/dd/yyyy]"); sc.nextLine(); String dayInput = sc.nextLine(); LocalDate day = LocalDate.parse(dayInput, fullYearFormatter); if (myCal.hasEvent(day)) { myCal.getEvents(day).clear(); System.out.println("All the events on " + dayInput + " has been deleted."); } else { System.out.println("No events available to delete."); } } } private static void quit(){ System.out.println("Good Bye!"); sc.close(); } } MyCalendar  
import java.time.LocalDate; import java.util.*; public class MyCalendar { private final Map> eventsOrder = new HashMap<>(); public void add(Event e) { LocalDate eventDay = e.getDate(); if (!eventsOrder.containsKey(eventDay)) { TreeSet events = new TreeSet<>(); eventsOrder.put(eventDay, events); } eventsOrder.get(eventDay).add(e); } public TreeSet getEvents(LocalDate day) { return eventsOrder.get(day); } public boolean hasEvent(LocalDate day) { TreeSet s = eventsOrder.get(day); if (s == null) return false; else if (s.isEmpty()) return false; return true; } }

Event

import java.time.LocalDate; public class Event implements Comparable { public String name; public LocalDate date; public TimeInterval time; public Event(String name, LocalDate date, TimeInterval time) { this.name = name; this.date = date; this.time = time; } public String getName() { return name; } public LocalDate getDate() { return date; } public TimeInterval getTime() { return time; } @Override public int compareTo(Event o) { return time.compareTo(o.time); } }

TimeInterval

import java.time.LocalTime; import java.time.LocalTime; public class TimeInterval implements Comparable { private LocalTime start; private LocalTime end; public TimeInterval(LocalTime start, LocalTime end) { this.start = start; this.end = end; } public boolean conflict(TimeInterval other) { if (end.isBefore(other.start) || start.isAfter(other.end)) { return false; } else { return true; } } @Override public String toString() { return start + " - " + end; } @Override public int compareTo(TimeInterval o) { return start.compareTo(o.start); } }

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions