/** Driver.java for schedule program. * Read the class schedlue, and create a list of rooms. * For each room, keep track of when it is used between 8 and 2. * I'm looking to see which rooms are available during which hours * on any day. For example, I need to find a room that is always * empty at 10am. */ import java.io.*; import java.util.*; public class Driver { public static void main(String [] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("spr07.txt")); PrintWriter out = new PrintWriter(new FileWriter("spr07.out")); ArrayList roomList = new ArrayList(); // Read input. while (true) { String line = in.readLine(); if (line == null) break; if (line.equals("")) continue; if (line.charAt(0) == ' ') continue; // course section [8,21) variable size "CS-12-A" // begin time [61,68) fixed size "10:00AM" // end time [72,79) fixed size "10:50AM" // room location [89,99) variable size "RLY 1000" String section = line.substring(8,21).trim(); String begin = line.substring(61,68); String end = line.substring(72,79); String location = line.substring(89,99).trim(); // Some classes are missing a time and/or location. // To be helpful, I need to know both time & place. if (begin.length() == 0 || location.length() == 0 || begin.charAt(0) == ' ' || location.charAt(0) == ' ') continue; //System.out.println(section + "," + begin + "," + end + "," + location); // To make this easier, let's assume discrete hours. If a class // meets 10:15 to 11:30, let's say it occupies the 10 and 11 hours. // I'm only interested in the 6 hours 8,9,10,11,12,1. // When we read a line of section info (section, begin, end, location) // 1. See if room already exists. If not, create a new room. // 2. Add this section to the room's time reservations. The section // string needs to be inserted into the room's array of times. // Usually a class occupies one time slot, but it could be more. // And it's possible for 2 classes to be in same slot (on diff days), // so it needs to be an append if there is already a class scheduled // in that room at that time. // * One minor difficulty is the AM/PM switch and 1:00 = 13:00 // When done reading in, print all rooms (row), // and sections by hour (column) // This can be done by room's toString(). Room r = null; for (int i = 0; i < roomList.size(); ++i) if (roomList.get(i).getName().equals(location)) { r = roomList.get(i); break; } if (r == null) { r = new Room(location); roomList.add(r); } int beginHour = Integer.parseInt(begin.substring(0,2)); if (beginHour == 1) beginHour = 13; int endHour = Integer.parseInt(end.substring(0,2)); if (endHour == 1) endHour = 13; // Ignore all hours outside the 6-hour 8-1 inclusive. for (int i = beginHour; i <= endHour; ++i) { if (i < 8 || i > 13) continue; r.addClass(section, i); } } // Alphabetize rooms. Collections.sort(roomList, new RoomComparator()); // output out.println("Room 8:00 9:00 10:00 11:00 12:00 1:00"); out.println("------------------------------------------------------------------------"); for (int i = 0; i < roomList.size(); ++i) out.println(roomList.get(i)); in.close(); out.close(); } }