Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

** Could someone please fix my code** I'm having issues with actually parsing through the txt file. If you run the code and run the

**Could someone please fix my code**

I'm having issues with actually parsing through the txt file. If you run the code and run the loadEvents() method, you will see that it can NOT parse the data correctely and I'm unsure if it is because of the way I have my txt file written or the code itself. Please fix and help. If you do change the loadEvents() method, I need it to be able to read a file that is in the form or whatever form you choose but if you do change it, please provide an example of what it should look like.

As seen here:

CS146

MW 12:00 13:15 1/25/23 5/20/23

M = monday

T = tuesday

W = wednesday

R = thrusday

F = Friday

A = Satruday

S = sunday

**There can be recurring events just FYI** Thats why its MW in the example

**AGAIN IF YOU CHANGE THE LOADEVENTS() METHOD PLEASE PROVIDE AN EXAMPLE OF WHAT THE TXT FILE SHOULD LOOK LIKE BECAUSE I WILL HAVE TROUBLE PARSING IT**

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")); while (fin.hasNextLine()) { String nameEvent = fin.nextLine().trim(); String eventRead = fin.nextLine().trim(); String[] separation = eventRead.split(" "); LocalTime startTime = LocalTime.parse(separation[1], DateTimeFormatter.ISO_LOCAL_TIME); LocalTime endTime = LocalTime.parse(separation[2], DateTimeFormatter.ISO_LOCAL_TIME); TimeInterval timeInterval = new TimeInterval(startTime, endTime); int firstDay1 = LocalDate.of(cal.getYear(), cal.getMonth(), 1).getDayOfWeek().getValue(); try { LocalDate date = LocalDate.parse(separation[0], dateTimeFormatter); Event event = new Event(nameEvent, date, timeInterval); myCal.add(event); } catch (DateTimeParseException e) { for ( char a : separation[0].toCharArray()) { DayOfWeek dw; switch (a) { case 'M': dw = DayOfWeek.MONDAY; break; case 'T': dw = DayOfWeek.TUESDAY; break; case 'W': dw = DayOfWeek.WEDNESDAY; break; case 'R': dw = DayOfWeek.THURSDAY; break; case 'F': dw = DayOfWeek.FRIDAY; break; case 'A': dw = DayOfWeek.SATURDAY; break; default: dw = DayOfWeek.SUNDAY; break; } int firstSearchDay = ( (dw.getValue() - firstDay1 +7) % 7 ) +1; while (firstSearchDay <= cal.getMonth().length(cal.isLeapYear())) { Event event = new Event(nameEvent, LocalDate.of(cal.getYear(), cal.getMonth(), firstSearchDay), timeInterval); myCal.add(event); firstSearchDay += 7; } } } } } 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); } }

Events.txt file

CS151 TR 2023-01-25 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 

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