// Building and Room classes
import java.util.*;
import java.io.*;

public class Building {

// nested class
static public class Room {
  private Hashtable meetings = new Hashtable();
  public Enumeration elements() { return meetings.elements(); }
  public String name = new String();
  public int capacity;
// Nested class: Room
  public Room(String s, int cap) {
    name=s;
    capacity=cap;
    }
// add a meeting to this room
  public void add(Meeting m) { meetings.put(m.name,m); }
// Check room availability
  public boolean Available(Date start,Date end,int people)
    {
    Enumeration e = meetings.elements();
    if (people>capacity) return false;
    while (e.hasMoreElements()) {
      Meeting m = (Meeting)e.nextElement();
      if (m.conflict(start,end)) return false;
      }
    return true;
    }
// Print yourself
   public void print(PrintStream ps){
     ps.println("Room: " + name);
     Enumeration e= meetings.elements();
     while (e.hasMoreElements()) {
       Meeting m=(Meeting)e.nextElement();
       m.print(ps);
       }
     }
  }

// Back to building class

  private Hashtable rooms = new Hashtable();
  public void add(Room r) { rooms.put(r.name,r); }
// Schedule a meeting
  public boolean schedule(Meeting m)
    {
    Room prospect = null;
// simple schedule algorithm
// Try to find the smallest room that is open
    Enumeration e=rooms.elements();
    while (e.hasMoreElements()) {
     Room r = (Room)e.nextElement();
     if (r.Available(m.start,m.end,m.attendees)) {
       if (prospect==null) prospect=r;
       if (prospect.capacity>r.capacity) prospect=r;
       }
     }
// at this point, prospect is null if no open rooms
// otherwise it is the smallest available room
    if (prospect==null) return false;
    prospect.add(m);
    return true;  
    }
  public void printSchedule(PrintStream ps) {
    ps.println("Schedule:");
    Enumeration e = rooms.elements();
    while (e.hasMoreElements()) {
      Room r = (Room)e.nextElement();
      r.print(ps);
      }
    }
  public Enumeration meetings()
    {
    return new Enumeration()  // anon class
      {
      private Hashtable table = new Hashtable();
      int n=0;
        {
        Enumeration e=rooms.elements();
        while (e.hasMoreElements()) {
          Room r=(Room)e.nextElement();
          Enumeration er=r.elements();
          while (er.hasMoreElements()) {
            table.put(new Integer(n++),er.nextElement());
            }
          }
        }
        public boolean hasMoreElements() { return n!=0; }
        public Object nextElement() 
         { 
          return table.get(new Integer(--n)); 
            }
       } ; // end of anon class 
     } // end of meetings()
  }